17

我确实阅读了一些讨论内部类的主题,并且我的印象是内部类可以访问封闭类的变量和方法。下面我有一个外部类和一​​个内部类,在测试类中我创建一个外部类的实例,然后我创建一个内部类的实例。但是我无法通过内部类引用访问字符串变量 a 。帮助?

public class OuterClass {

    String a = "A";
    String b = "B";
    String c = "C";

    class InnerClass {
        int x;

    }

    public static class StaticInnerClass {
        int x;
    }

    public String stringConCat() {
        return a + b + c;

    }
}

public class TestStatic {

    public static void main(String args[]) {

        OuterClass.StaticInnerClass staticClass = new OuterClass.StaticInnerClass();
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();

        System.out.println(inner.a);// error here, why can't i access the string
                                    // variable a here?

    }
}
4

5 回答 5

22

内部类可以通过自己的方法访问外部类的方法和属性。看下面的代码:

class OuterClass {

    String a = "A";
    String b = "B";
    String c = "C";

    class InnerClass{
        int x;
        public String getA(){
            return a; // access the variable a from outer class
        }
    }

    public static class StaticInnerClass{
        int x;
    }

    public String stringConCat(){
        return a + b + c;    
    }
}


public class Test{

    public static void main(String args[]) {

        OuterClass.StaticInnerClass staticClass = new OuterClass.StaticInnerClass();
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();

        System.out.println(inner.getA()); // This will print "A"
    }
}
于 2012-09-03T17:31:38.320 回答
6

您似乎混淆了范围和访问权限。引用只能访问它们所引用的对象的属性和方法。所以你inner.a不能工作。

另一方面,外部类变量在各自内部类的方法范围内。所以你可以通过定义一个读访问器来做你想做的事。

public class OuterClass {  

    String a = "A";  
    String b = "B";  
    String c = "C";  

    class InnerClass{  
        int x;  
        String getA() {
             return a;
        }
    }  
}  


public class TestStatic {  

    public static void main(String args[]) {  

        OuterClass.StaticInnerClass staticClass = new OuterClass.StaticInnerClass();  
        OuterClass outer = new OuterClass();  
        OuterClass.InnerClass inner = outer.new InnerClass();  

        System.out.println(inner.getA());  //error should be gone now.   

    }  
}
于 2012-09-03T17:32:00.790 回答
1

inner是一个未定义的实例,因此无论如何它都OuterClass.InnerClass无法a访问它。

要以最简单的方式理解它...查看a创建的块和OuterClass.InnerClass定义的块。

public class OuterClass { // a is defined in this block

    String a = "A";

    class InnerClass{ // InnerClass starts here and an instance will access what is defined now
        int x;
    }
}
于 2012-09-03T17:24:45.823 回答
0

AnNon-static inner class式引用OuterClass...

像这样试试......

class OuterClass {

    String a = "A";
    String b = "B";
    String c = "C";

    class InnerClass {

            String x = a;    // Directly uses the instance variable a from Outer class


    }

    public static class StaticInnerClass {
        int x;
    }

    public String stringConCat() {
        return a + b + c;

    }
}

public class TestStatic {

    public static void main(String args[]) {

        OuterClass.StaticInnerClass staticClass = new OuterClass.StaticInnerClass();
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();

        System.out.println(inner.x);

    }
}
于 2012-09-03T17:32:12.800 回答
0
System.out.println(inner.a);

您试图将其视为 a您的内部类的属性,但事实并非如此。您应该定义一个方法来读取外部类的所需属性。因此,在您的内部课程中,您应该拥有:

 public String getA(){
     return a;
 }

System.out.println(inner.getA());

这应该有效。

于 2012-09-03T17:39:25.893 回答