我有2个不是主类的类,但我希望B类从类A中访问变量。我不知道该怎么做。
public class A (
public int humanCakes;
//This method is called by somewhere, when it is called it adds one
public void humanCakes() {
huamnCakes ++;
}
)
public class B {
public void addUp() {
Cakes = humanCakes + 4;
}
}
答案 0 :(得分:0)
由于A类中的变量是公开的,您可以执行以下操作
public class B{
int Cakes;
public void addUp(A obj)
{
Cakes = obj.humanCakes + 4;
}
}
请注意,您必须将A对象放入addUp方法才能获取对humanCakes变量的引用