这条线有什么问题?(内班和外类)

时间:2016-03-28 12:00:10

标签: java

public class Learning{
    private String temp = "Outter";
    private class Inner{
        private String temp = "Inner";
        public void print(){
            String temp = "Inner function";
            System.out.println(temp);
            System.out.println(this.temp);
            System.out.println(Learning.this.temp);
        }
    }
    public static void main(String args[]){
        Inner in = new Inner();//what's wrong with this line?
        in.print();
    }
}

1 个答案:

答案 0 :(得分:1)

这一行

Inner in = new Inner();//what's wrong with this line?

使用没有外类的Inner。有两种方法可以解决这个问题

Inner in = new Learning().new Inner();

或制作课程static

private static class Inner {

虽然@flkes表示,但使用了外部实例。