怎么才能让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){

}
}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【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  %                  
    敬礼!
      

  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 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();
        }
    }
    这样就行了
      

  3.   

    但是,根据3楼的代码,虽然能编译,但是在运行时是会出现一个异常,说是没有main()方法。
    难道就没其他办法把 readi() 、reada() 方法在static方法中调用吗??
      

  4.   

    5楼正解~~~,不过我倒是糊涂了,干嘛 static 方法再去调用呢,它应该在调用 main()方法后先初始化的嘛
      

  5.   

    没有问题啊,你再编译一次,不要打错字!
    i is a 10
    a is a I am Test2
    这个是我的编译出来的答案!
      

  6.   

    我晕我以为3楼只是将main()方法给删掉了,但是我的初衷是,在static方法里运行,而不是main()方法里。不过很谢谢3楼的解答
      

  7.   

    不大明白什么意思,好象是不想通过main()函数,在类加载的时候就先把内部类的代码执行了吧?

     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();
        }
    看看吧。我这里没编译器,也不清楚行不行