讨论:两种使用另外一个类的变量的方法。。。
gzyip 2004-04-24 12:23:11 有两种方法可以使用另外一个类的变量,用哪一种好?什么时候用哪一种???
一种是:
//way1.java
public class way1 {
public static void main(String[] args) {
test1 t=new test1();
System.out.println(test1.instance.i);
}
}
class test1 {
static test1 instance;
int i=10;
public test1(){
instance=this;
}
}
另一种方法:
//way2.java
public class way2 {
test2 test;
int i;
public static void main(String[] args) {
way2 way=new way2();
test2 test=new test2(way);
System.out.println(way.i);
}
}
class test2 {
way2 way;
public test2(way2 w){
way=w;
w.i=10;
}
}
欢迎各位讨论...一个星期结帖...