Basically I need to access variables from an object which is passed as a parameter inside of a method. Ill give some pseudo code instead of the real code I'm using right now, for an easier-to-understand example for other users.
基本上我需要从一个对象中访问变量,该对象作为方法内部的参数传递。我会给出一些伪代码而不是我现在正在使用的真实代码,以便为其他用户提供一个更容易理解的示例。
public class AClass{
Object x = null;
private void doSomething(){
method("something", new RandomClass());
x = *the object of new RandomClass()*
}
}
I'll change the tags and title if there is a need for me to post the specific example, but I think this is a question for multiple languages.
如果我需要发布具体示例,我会更改标签和标题,但我认为这是一个多语言的问题。
2 个解决方案
#1
Some languages let us use them with pass-by-reference. Some don't. Therefore, first you should know whether the language you use has this kind of option or not. For example C++ can allow us to use it by pass-by-reference. In C++ , there are pointers and references. And we can use it like that:
有些语言允许我们使用pass-by-reference。有些人没有。因此,首先您应该知道您使用的语言是否具有此类选项。例如,C ++可以允许我们通过引用传递来使用它。在C ++中,有指针和引用。我们可以像这样使用它:
int main()
{
Object r ;
Object k ;
r.data=1;
k.data=2;
cout<<r.data<<" "<<k.data<<endl;
method(r,k);
cout<<r.data<<" "<<k.data<<endl;
return 0;
}
void method(Object & x, Object & y){ // pass-by-reference
x.data=4;
y.data=5;
}
Output will be:
输出将是:
1 2
4 5
Also it is important that the variables of the object that you want to access are private or public.If it is not a friend function or not a function inside of that class, you cannot access that variables. Hope it helps.
同样重要的是,您要访问的对象的变量是私有的或公共的。如果它不是友元函数或不是该类内部的函数,则无法访问该变量。希望能帮助到你。
#2
After thinking about it for a few minutes I figured out how to fix this specific problem.
在考虑了几分钟之后,我想出了如何解决这个具体问题。
public class AClass{
Object x = null;
private void doSomething(){
Object y = new RandomClass();
method("something", y);
x = y;
}
}
#1
Some languages let us use them with pass-by-reference. Some don't. Therefore, first you should know whether the language you use has this kind of option or not. For example C++ can allow us to use it by pass-by-reference. In C++ , there are pointers and references. And we can use it like that:
有些语言允许我们使用pass-by-reference。有些人没有。因此,首先您应该知道您使用的语言是否具有此类选项。例如,C ++可以允许我们通过引用传递来使用它。在C ++中,有指针和引用。我们可以像这样使用它:
int main()
{
Object r ;
Object k ;
r.data=1;
k.data=2;
cout<<r.data<<" "<<k.data<<endl;
method(r,k);
cout<<r.data<<" "<<k.data<<endl;
return 0;
}
void method(Object & x, Object & y){ // pass-by-reference
x.data=4;
y.data=5;
}
Output will be:
输出将是:
1 2
4 5
Also it is important that the variables of the object that you want to access are private or public.If it is not a friend function or not a function inside of that class, you cannot access that variables. Hope it helps.
同样重要的是,您要访问的对象的变量是私有的或公共的。如果它不是友元函数或不是该类内部的函数,则无法访问该变量。希望能帮助到你。
#2
After thinking about it for a few minutes I figured out how to fix this specific problem.
在考虑了几分钟之后,我想出了如何解决这个具体问题。
public class AClass{
Object x = null;
private void doSomething(){
Object y = new RandomClass();
method("something", y);
x = y;
}
}