java中的继承私有字段 [英] inheritance private field in java

查看:207
本文介绍了java中的继承私有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果子类不能从超类继承私有成员,但它继承了可以访问未继承的私有成员的超类的公共方法,如此处所述

If a subclass can't inherit private members from a super-class, but it inherits public methods from the super-class that have access to the private members that are not inherited, as mentioned here


http: //docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

哪里是由存储的子类的继承成员修改的超类的私有成员吗?它们存在于何处?

推荐答案

将私有成员变量视为存在于声明它们的类中。如果子类调用其父类中更改成员变量的方法,请将该更改视为父类中发生的更改。这是一种在头脑中建模的有用方法,因为只有那个父类可以拥有读取或写入该变量值的代码。

Think of the private member variables as living in the class in which they are declared. If a subclass calls a method in its parent class that changes the member variable, think of that change as happening in the parent class. This is a useful way to model it in your head because only that parent class can have code that reads or writes that variable's value.

子类必须发出请求然后,父类对在那里声明的成员变量执行某些操作。

The subclass has to make a request to the parent class, then, to do something with the member variable declared up there.

如果使用子类中的代码覆盖父类中的方法,则覆盖方法无法访问私有成员变量,即使父进程中的重写方法有效。子类中的重写方法可以调用父类中的重写方法。

If you override a method in a parent class with code in the subclass, that override method can not access the private member variable, even though the overridden method in the parent does. The overriding method in the subclass can call the overridden method in the parent though.

例如:

public class Parent {
    private int bar = 0;
    public void setBar(int value) {
        bar = value;
    }
}

public class Derived extends Parent {
    @override
    public void setBar(int value) {
        bar = value + 1; // NOT ALLOWED
        super.setBar(value + 1); // ALLOWED (same result)
    }
}

低级别信息:

但是,在低级别,我可能会创建一个SubClass实例,它将为所有实例变量分配一块内存块对于SubClass和所有父类,包括Object。

At a low level, though, I might create an instance of SubClass which will allocate a block of memory with room for all the instance variables for SubClass and all the parent classes up to, and including, Object.

方法本身的代码在于某些ClassLoader为包含每个方法的Class分配的内存。这是按班级分配的。因此,即使数据一起存储在实例中,各种子类和父类的代码也不会存储在一起。

The code for the methods themselves lies in some memory allocated by some ClassLoader for the Class containing each method. That is allocated on a class by class basis. So the various sub- and parent- classes' code isn't stored together even though the data is stored together in the instances.

访问规则不会让SubClass中的代码访问为父或祖先类私有的实例变量分配的内存。

The rules of access just don't let the code in SubClass access the memory allocated for the instance variables that are private to a parent, or ancestor, class.

在这种情况下,它很少值得在这么多细节上思考它的努力。这是我的经验。其他人可能会有不同的看法。

In this case, though, it is seldom worth the effort of thinking about it in this much detail. That's my experience with it. Others may see it differently.

注意:有办法通过反思访问私有变量。

Note: There are ways to access private variables through reflection.

可见性

我可能需要一些帮助,因为我在记忆中工作。

I may need some help here as I'm working from memory.

为成员变量分配了四个级别的可见性。这些与类变量和方法一样使用。

There are four levels of visiblity assigned to member variables. These are the same four used with class variables and methods.

private - 这些变量只能由声明它们的同一个类中的代码访问。 (嗯......它们也可以通过该类中的内部类访问。)

private - these variables can be accessed only by code within the same class in which they are declared. (Well ... they can be accessed by inner classes within that class too.)

包 - 这些可以通过相同类中的代码和任何类中的代码访问在与该类相同的包中。具有该代码的类可以在您的源文件或某些jar文件中,或者实际上在类路径中的任何位置。 (注意:没有关键字package。如果没有其他关键字来表示可见性,则变量具有包级别可见性。我喜欢将package放在/ * * / comment中。)

package - these can be accessed by code within the same class AND code in any class in the same package as that class. The class with that code could be in your source files or some jar file or, really, anywhere in the classpath. (Note: There is no keyword "package". A variable has package level visibility if there is no other keyword to indicate visibility. I like to put the word 'package' in a /* */ comment.)

protected - 这些可以通过相同类中的代码访问,并且该类的任何子类中的代码和该类相同包中的任何类中的代码都可以访问。

protected - these can be accessed by code within the same class AND code in any subclass of that class AND code in any class in the same package as that class.

public - 这些可以通过任何其他类中的代码访问。

public - these can be accessed by code within any other class.

警告:还有一些代码可能是您希望的代码可见不是。这是因为ClassLoader的工作方式。 Java EE具有类加载器的嵌套,您最终可能会遇到由一个类加载器加载的代码,这些加载器对另一个加载的代码不可见。您最终可以使用两个具有相同全名的类(包括包)。我认为所有这些都是高级主题,我必须阅读它来解释它。我确实想要注意它会发生,并且会让你怀疑可见性。

Warning: there are also ways that code you would otherwise expect to be visible is not. This is because of the way the ClassLoader(s) work. Java EE has a nesting of class loaders and you can end up with code loaded by one class loader not being visible to code loaded by another one. You can end up with two classes with the same full name (including package). I consider all this an "advanced" topic and I'd have to read up on it to explain it. I did want to note that it happens and can cause you to wonder about visibility.

public class MyClass {
    private int foo1 = 1;       // visible in this class only
    protected int foo2 = 2;     // visible here, in subclasses and in classes with same package
    int foo3 = 3;               // visible here and in classes with the same package
    public int foo4 = 4;        // visible here, there and everywhere

    /* package */ int foo5 = 5; // how I like to do 'package' variables with a comment
                                //  to show I intended to do it on purpose. If you read
                                //  my code you don't have to wonder if I forgot it.

    ...
}

最后一个实用说明:从长远来看,我发现它几乎所有成员变量都是私有的非常有用。如果您需要将它们更改为更明显的内容,请执行此操作,或者只创建具有所需可见性的getter和setter方法。一个优点是,如果我提供一个getter而没有setter,或者getter是公共的并且setter受到保护,我可以给予readonly访问权限。我也可以创建一个可以设置但永远不会读取的writeonly变量。这适用于依赖注入,但你应该对此进行评论。也许你可以看到优势。缺点是您编写了更多代码行,但是如果您愿意,其他IDE中的eclipse会为您生成这些方法。

One final, practical note: I find it extremely useful in the long run to make almost all member variables private. If you need to change them from to something more visible, do so OR maybe just create getter and setter methods with the desired visibility. One advantage is that I can give readonly access if I provide a getter and no setter or if the getter is public and the setter is protected. I can also make a writeonly variable that can be set but never read. This works with dependency injection but you should put in a comment about that. Maybe you can see the advantages. The disadvantage is you write more lines of code but eclipse among other IDEs will generate those methods for you if you like.

这篇关于java中的继承私有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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