怎么才能让2个内部类的方法打印结果?
代码:
public class Test{
class Test1{
int i = 0;
Test1(int a){i = a;}
void readi(){System.out.println("i is a " + i);}
//String readi(){return Integer.toString(i);} //这里注释的是另一个方法,但是仍然不起作用
}
class Test2{
String a = null;
Test2(String b){a = b;}
void reada(){System.out.println("a is a " + a);}
}
public static void mytest(){
Test test = new Test();
Test.Test1 test1 = test.new Test1(10);
test1.readi();
//System.out.println(test1.readi()); //同样试的另一种方法,但是不起作用
Test.Test2 test2 = test.new Test2("I am Test2");
test2.reada();
}
public static void main(String[] args){
}
}
代码:
public class Test{
class Test1{
int i = 0;
Test1(int a){i = a;}
void readi(){System.out.println("i is a " + i);}
//String readi(){return Integer.toString(i);} //这里注释的是另一个方法,但是仍然不起作用
}
class Test2{
String a = null;
Test2(String b){a = b;}
void reada(){System.out.println("a is a " + a);}
}
public static void mytest(){
Test test = new Test();
Test.Test1 test1 = test.new Test1(10);
test1.readi();
//System.out.println(test1.readi()); //同样试的另一种方法,但是不起作用
Test.Test2 test2 = test.new Test2("I am Test2");
test2.reada();
}
public static void main(String[] args){
}
}
楼主【llm0528】截止到2008-07-26 20:17:34的历史汇总数据(不包括此帖):
发帖的总数量:60 发帖的总分数:1240 每贴平均分数:20
回帖的总数量:52 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:60 结贴的总分数:1240
无满意结贴数:0 无满意结贴分:0
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:0.00 % 无满意结分率:0.00 %
敬礼!
public class Test{
class Test1{
int i = 0;
Test1(int a){i = a;}
void readi(){System.out.println("i is a " + i);}
//String readi(){return Integer.toString(i);} //这里注释的是另一个方法,但是仍然不起作用
}
class Test2{
String a = null;
Test2(String b){a = b;}
void reada(){System.out.println("a is a " + a);}
}
public static void main(String[] args){
Test test = new Test();
Test.Test1 test1 = test.new Test1(10);
test1.readi();
//System.out.println(test1.readi()); //同样试的另一种方法,但是不起作用
Test.Test2 test2 = test.new Test2("I am Test2");
test2.reada();
}
}
这样就行了
难道就没其他办法把 readi() 、reada() 方法在static方法中调用吗??
i is a 10
a is a I am Test2
这个是我的编译出来的答案!
把
public static void mytest(){
Test test = new Test();
Test.Test1 test1 = test.new Test1(10);
test1.readi();
//System.out.println(test1.readi()); //同样试的另一种方法,但是不起作用
Test.Test2 test2 = test.new Test2("I am Test2");
test2.reada();
}
改成
static{
Test test = new Test();
Test.Test1 test1 = test.new Test1(10);
test1.readi();
//System.out.println(test1.readi()); //同样试的另一种方法,但是不起作用
Test.Test2 test2 = test.new Test2("I am Test2");
test2.reada();
}
看看吧。我这里没编译器,也不清楚行不行