class outer{
int c,
void values() {
inner.a=3;
inner.b=4;
}
void addvalues() {
**inner.a += c;** // **How can I access the value of a=3 from the values() method?**
}
class inner{
int a,b;
}
}
答案 0 :(得分:3)
1)您需要inner
的实例来执行此操作
或
2)您可以将inner
声明为static
并使其变量a
也为静态
答案 1 :(得分:0)
您想在Outer类中创建Inner类的实例。此外,与任何其他类定义一样,只需在外部类中使用内部类的getter和setter。像这样:
public class Outer {
private int c;
private Inner inner = new Inner();
public void values() {
inner.setA(3);
inner.setB(4);
}
public void addValues()
{
inner.setA(inner.getA() + inner.getB());
}
public int getA() {
return inner.getA();
}
public static void main(String args[]) {
Outer outer = new Outer();
outer.values();
outer.addValues();
System.out.println(outer.getA());
}
class Inner {
int a;
int b;
public int getA() {
return a;
}
public int getB() {
return b;
}
public void setA(int a) {
this.a = a;
}
public void setB(int b) {
this.b = b;
}
}
}