请尊重java中的命名约定,并且尽量不要直接访问类的变量(除了从类本身或其内部类之一)
使用 setter 和 getter,例如:
public class Foo {
// private variables
private ChildFoo[] cFoo;
// setter
public void setCFoo(Foo.ChildFoo[] cFoo) {
this.cFoo = cFoo;
}
// getter
public Foo.ChildFoo[] getCFoo() {
return cFoo;
}
public void initCFoo(int size) {
cFoo = new ChildFoo[size];
for (int i = 0; i < cFoo.length; i++) {
cFoo[i] = new ChildFoo();
}
}
public class ChildFoo {
// private variables
private int a, b, c;
// setter
public void setA(int a) {
this.a = a;
}
// getter
public int getA() {
return a;
}
public void setB(int b) {
this.b = b;
}
public int getB() {
return b;
}
public void setC(int c) {
this.c = c;
}
public int getC() {
return c;
}
}
}
Foo foo = new Foo();
foo.initcFoo(1);
foo.getCFoo()[0].setA(1);