简体   繁体   English

无法访问匿名嵌套子类的成员(实现接口)

[英]Can't access members of an anonymous, nested subclass (implementing an interface)

I have problem here. 我在这里有问题。 I write a piece of code like this: 我写了一段这样的代码:

package vh.Static;

public class Nesting {
    //static class Nested{}
    class Inner{}
    void method(){
        Inner inner = new Inner(){
            public int z =2;
            public int getZ(){
                return z;
            }
        };
        System.out.println(inner);
    }
    public static void main(String args[]){
        Nesting ne  =new Nesting();
        Inner inner = ne.new Inner(){
            public int z =1;
            public int getZ(){
                return z;
            }
        };
        System.out.println(inner);
        ne.method();
   }
}

I don't know how to get the var z definding in the Inner constructor block. 我不知道如何在内部构造函数块中获得var z定义。 help me,please! 请帮帮我! and tell me where is z local? 告诉我z在哪里? in Inner? 在内在? or Nesting 或嵌套

The answer to the original title of your question is really broad - you'll need to do some research: 问题原始标题的答案确实很广泛-您需要进行一些研究:

  • Interface is contract definition only, it has no implementation 接口仅是合同定义,没有实现
  • Class can have implementation 类可以实现
  • A nested class is visible only to its containing class 嵌套类仅对其包含的类可见

But to address your specific example, in the code: 但是要解决您的特定示例,请在代码中:

void method(){
    Inner inner = new Inner(){
        public int z =2;
        public int getZ(){
            return z;
        }
    };
}

This instantiates a new anonymous subclass of the nested class Inner , and then extending the anonymous subclass with a public field and a method. 这将实例化嵌套类Inner的新匿名子类,然后使用公共字段和方法扩展匿名子类。

The reason why the extended field + method can't be accessed is that the variable inner is of the base type Inner (to which nothing has been added - this is defined simply as class Inner{} ), and is not of the type of the anonymous subclass. 之所以无法访问扩展字段+方法,是因为变量inner是基本类型Inner (没有添加任何内容,它简单地定义为class Inner{} ),而不是基本类型。匿名子类。 As it stands you would need to resort to nasties like reflection to access the fields + properties in the anonymous subclass, outside of the anonymous subclass itself. 就目前情况而言,您将需要使用诸如反射之类的方法来访问匿名子类本身之外的匿名子类中的字段+属性。

 System.out.println(inner.getZ()); // Does not compile

What can be done instead is to define abstract methods on Inner and then override these in the anonymous class: 相反,可以做的是在Inner上定义抽象方法,然后在匿名类中重写它们:

abstract class Inner{
    public abstract int getZ();
}

void method(){
    Inner inner = new Inner(){
        private int z =2; // Private fields plz, use get / setters
        public int getZ(){
            return z;
        }
    };

You can now access the defined Inner methods anywhere within the Nesting class 现在,您可以在Nesting类中的任何位置访问已定义的Inner方法

Just one note on conventions - the docs refer to the inner class (your Inner ), as the nested class and the outer class (your Nesting ) as Outer . 只是有关约定的注释- 文档将内部类(您的Inner )称为nested class ,将外部类(您的Nesting )称为Outer

Here's the same thing defining Inner as an interface which is then implemented anonymously: 这是将Inner定义为接口的同一件事,然后匿名实现该接口:

class Nesting {
    interface Inner{
        int getZ();
    }
    void method(){
        Inner inner = new Inner(){
            private int z =2;
            public int getZ(){
                return z;
            }
            private int anotherInnerMethod(){
                // Can access non-interface items as it is part of the anon subclass
                return z * z;
            }
        };
        System.out.println(inner);
        // Access getZ() through the interface
        System.out.println(inner.getZ());
    }
    // Because this method is in class Nesting, it may still access Inner
    public static void main(String args[]){
        Nesting ne = new Nesting();
        ne.method();
        Inner inner = new Inner(){
            private int z =1;
            public int getZ(){
                return z;
            }
        };
        System.out.println(inner.getZ());
        ne.method();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM