简体   繁体   English

如果一个方法返回一个匿名类,它返回一个子类还是一个内部类?

[英]If a method returns an anonymous class, is it returning a subclass or an inner class?

Perhaps it makes no difference, but I want to know anyways. 也许没关系,但是我还是想知道。 I want to know what it means to return an anonymous class such as in the following: 我想知道返回匿名类的含义,例如:

public class Module extends BaseModule {

    private BaseInitializable initializeModule() {
        return new BaseInitializable() {
            public void initialize(InitializationContext context) {
                helperA.initialize();
                helperB.initialize();
                s_logger.log(LogLevel.INFO,"something");
            }

            public void shutdown(InitializationContext context) {
            }
        };
    }
}

Post 1 发布1

Post 2 发布2

Based on post 1, it seems the code is returning an anonymous inner class. 根据帖子1,似乎代码正在返回匿名内部类。 But doesn't that go against the principle of an inner class in that they are only relevant to the parent class in which they reside (according to post 2)? 但这是否违反内部类的原则,因为它们仅与它们所驻留的父类相关(根据文章2)? The accepted answer in post 1 says the inner class is also sub class of the parent class. 在帖子1中接受的答案说,内部类也是父类的子类。 If that's true, then it makes sense to return this inner/sub class. 如果是这样,则返回此内部/子类是有意义的。 But according to Post 2, inner classes are not subclasses. 但是根据Post 2,内部类不是子类。 So what am I to make of this? 那我该怎么办呢? Does this method return an instance of an inner class or a subclass? 此方法是否返回内部类或子类的实例?

This statement "[inner classes] are only relevant to the parent class in which they reside" is simply wrong. 这句话“ [内部类]仅与它们所驻留的父类相关”,这是完全错误的。

The inner class has a hidden reference to the outer class, and it has permission to access the private members of the outer class. 内部类具有对外部类的隐藏引用,并且有权访问外部类的私有成员。 That's basically all that makes it special. 基本上就是所有这些使它与众不同。

The method in your question does indeed return an instance of the anonymous inner class derived from or implementing BaseInitializable (is that an interface or class?). 您问题中的方法的确确实返回了源自或实现BaseInitializable(是接口还是类?)的匿名内部类的实例。

Note that if BaseInitializable is a class then this anonymous inner class is also a sub-class of BaseInitializable. 请注意,如果BaseInitializable是一个类,则此匿名内部类也是 BaseInitializable的子类。 If BaseInitializable is an interface then technically it is not a sub-class. 如果BaseInitializable是接口,则从技术上讲,它不是子类。

The inner class, in this case, is NOT a subclass of the outer class. 在这种情况下,内部类不是外部类的子类。 I think that is what Post 2 is talking about. 我认为这就是Post 2所说的。

I want to know what it means to return an anonymous class such as in the following: 我想知道返回匿名类的含义,例如:

    return new BaseInitializable() {
        ...
    };

That depends on what BaseInitializable is. 这取决于BaseInitializable是什么。 If it is a class , then the anonymous class is a subclass of BaseInitializable . 如果它是一个 ,则匿名类是BaseInitializable的子类。 If it is an interface , then the anonymous class is a subclass of Object and it implements BaseInitializable . 如果它是接口 ,则匿名类是Object的子类,它实现BaseInitializable

Also, remember that anonymous classes by definition are inner classes. 另外,请记住,匿名类按定义是内部类。 So says JLS 15.9.5. JLS 15.9.5这样说 Anonymous Class Declarations : 匿名类声明

An anonymous class is always an inner class. 匿名类始终是内部类。

Which means, to answer your question: 这意味着要回答您的问题:

Does this method return an instance of an inner class or a subclass? 此方法是否返回内部类或子类的实例?

Both. 都。 The anonymous class is an inner class and a subclass (of BaseInitializable or of Object ). 匿名类是一个内部类一个子类(属于BaseInitializableObject )。

Or, if you don't consider classes with Object as their superclass to be subclasses: 或者,如果您不将Object作为其超类的类视为子类:
It depends. 这取决于。 The anonymous class is an inner class. 匿名类是内部类。 It is also a subclass if it implements a class , not an interface . 如果它实现一个而不是一个接口 ,它也是一个子

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

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