使用子类中的super关键字访问超类私有字段 [英] Access to superclass private fields using the super keyword in a subclass

查看:518
本文介绍了使用子类中的super关键字访问超类私有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于编码项目,我有一个包含嵌套类的类。嵌套类在同一外部类中进行子类化。目的是让外部类包含嵌套类的一些实例,它可以将它交给外部类的其他实例。

For a coding project I have a class containing a nested class. The nested class is subclassed within the same outer class. The intention is for the outer class to contain some instances of the nested class which it can hand to other instances of the outer class.

嵌套子类允许外部类修改内容,而其超类允许读取内容并调用某些方法。因此,超类对象被传递给其他对象以链接链中的外部类对象。

The nested subclass allows the outer class to modify the contents while its superclass allowes the contents to be read and some methods invoked. The superclass objects are thus handed to other objects to links the outer class objects in a chain.

我遇到的问题涉及访问修饰符。这是一个极简主义的代码示例:

The question I have concerns the access modifiers. Here is a minimalist code example:

abstract class OuterClass {


    protected class NestedSuperClass<T> {
        private T data;

        public NestedSuperClass (T t) {
            this.data = t;
        }

        public T getOutput() {
            return data;
        }
    }

    protected class NestedSubClass<T> extends NestedSuperClass<T> {
        public NestedSubClass (T t) {
            super(t);
        }

        protected void setOutput(T t) {
            super.data = t;
        }
    }
}

查找一些文档我被访问超级类的私有字段的能力搞糊涂了。是否有任何资源解释为什么允许子类以这种方式修改超类的私有字段?

When looking up some documentation I was confused by the ability to access the private field of the superclass not being mentioned anywhere. Is there any resource explaining why the subclass is allowed to modify the private field of the superclass in this way?

我完全没有这个工作。我还注意到它似乎与数据被标记为受保护而不是私有而不使用超级关键字。我最感兴趣的是提到super关键字的这种能力的任何文档。在此先感谢。

I am completely fine with this working. I also noticed that it seems to work with data being marked as protected instead of private and not using the super keyword. I am mostly interested in any documentation mentioning this ability of the super keyword. Thanks in advance.

推荐答案

根据 Java语言规范


例6.6-5。访问私有字段,方法和构造函数

Example 6.6-5. Access to private Fields, Methods, and Constructors

私有类成员或构造函数只能在顶级类(第7.6节)的主体内访问,该类包含声明成员或构造函数。它不是由子类继承的。

A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. It is not inherited by subclasses.

所以会发生什么是内部类可以直接看到非私有字段,因为它继承了它。

So what happens is the inner class can see a non-private field directly since it inherits it.

但是,对于私有字段,内部类必须使用 super.field 来访问它,因为它不是继承的(否则会出现编译器错误 field 不可见)。即使它没有被继承,它仍然是可访问的,因为内部类在外部类中,并且私有字段可以被顶级类主体内的任何内容访问。

However, for private fields the inner class has to use super.field to access it since it is not inherited (otherwise you get a compiler error "field is not visible"). Even though it is not inherited it is still accessible because the inner class is inside the outer class and private fields are accessible to anything inside the body of the top level class.

这篇关于使用子类中的super关键字访问超类私有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆