gpt4 book ai didi

java - java中无法引用类

转载 作者:太空宇宙 更新时间:2023-11-04 07:43:04 25 4
gpt4 key购买 nike

class MyClass {
int value;
}
public MyClass test(){
MyClass mc;
myMethod(mc, 100);
if (mc!=null) {
mc = mc.value;
return mc;
}
return mc;
}
public int myMethod(MyClass mc, int a) {
mc.value = a + 10;
return mc;
}

我想把mc作为引用。如果 Objective-c 与此类似:

public MyClass test(){
MyClass *mc;
myMethod(&mc, 100);

}

请帮助我。任何答案都很感激。提前谢谢您。

最佳答案

在java中我们使用引用变量而不是指针。请参阅thisthis

class MyClass 
{
int value;

/*All methods have to belong to a class*/

public MyClass test()
{
/*MyClass mc; you don't need to pass this to assign value the value will be
assigned to the data members of the object(reference variable) which calls the
function*/

MyClass mc = myMethod(100);
return mc;
}

public MyClass myMethod(int a)
{
value = a + 10;
return this; //This 'this' will return the current object
}
}

关于java - java中无法引用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15741260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com