简体   繁体   English

访问子类中的私有实例

[英]Accessing private instances in child class

I've seen answers for questions related to mine on Stack Overflow, but I am still left with some ambiguity. 我已经在Stack Overflow上看到了与我有关的问题的答案,但我仍然有些含糊不清。 A parent class method has access to its own private instance variables. 父类方法可以访问自己的私有实例变量。 If a child class inherits the class, what happens when the getA() method is called on an instance of the Child class? 如果子类继承了该类,那么在Child类的实例上调用getA()方法时会发生什么? Does it return the a from the Parent class or the a from the Child class? 它返回aParent类或aChild类?

class Parent {
    private int a = 10;  
    public int getA() {
       return a;
    }
 }

class Child extends Parent {
    private int a = 22;              
 }

public class Test { 
   public static void main(String []args) throws Exception {        
       Child c = new Child(); 
       System.out.println(c.getA());
   }    
}

As the Method getA() is inherited, if you call this Method, you'll always invoke the Parent 's Method. 由于继承了方法getA() ,如果调用此方法,您将始终调用Parent方法。

The current Object will be treated as a Parent and not as a Child , and the Parent 'sa will be returned. 当前对象将被视为一个Parent ,而不是作为一个Child ,和Parent “SA将被退回。

Even though you have your own variable a , this variable wont override the Parent 's a . 即使你有自己的变量a ,这个变量也不会覆盖Parenta They are different from each other, have different addresses and different values. 它们彼此不同,具有不同的地址和不同的值。

If you want getA() to return Child 's a , you need to override the Method to return your new variable. 如果你想让getA()返回Childa ,你需要覆盖Method来返回你的新变量。

class Child extends Parent {
    private int a = 22;

    @Override
    public int getA(){
        return a;
    }
 }

You could also "go crazy" and do stuff like the following: 你也可以“疯狂”并做以下事情:

class Child extends Parent {
    private int a = 22;       

    @Override
    public int getA(){
        int superA = super.getA();

        return a+superA;    
    }
 }

That way you could return the sum of Parent 's and Child 's a . 这样,你可以返回的总和Parent的和Childa

(Just an example) (只是一个例子)

This is a duplicate of this SO post . 这是此SO帖子的副本。

The variable a in subclass Child hides the a in the parent class Parent . 变量a中的子类Child隐藏了a父类的Parent

Fields cannot be overridden. 字段无法覆盖。

In your code, an instance of child (like the one referred to by c ) has two different fields, which are both called a . 在您的代码中,子实例(如c引用的实例)有两个不同的字段,它们都被称为a

You cannot access Parent 's private variables inside Child , full stop. 您无法在Child访问Parent的私有变量,完全停止。 That's the entire point of private . 这就是private的全部观点。 There is nothing* you can write inside Child to make the parent's a field equal 22. 没有什么*您可以在里面写Child做父母的a等于22场。




* Technically, you could with reflection. *从技术上讲,你可以用反射。 Reflection doesn't count though, since its purpose is essentially to allow you to break things, and do things that are otherwise impossible. 反思不算数,因为它的目的主要是让你破坏事物,并做其他不可能的事情。

Private variables are local to the class and in your code you are inheriting the properties of the parent class so you can access getA() and it will return the parent's attribute. 私有变量是类的本地变量,在您的代码中,您继承父类的属性,因此您可以访问getA()并返回父属性。 And you cannot access child's variable unless you have public getter method for child attribute. 除非你有子属性的公共getter方法,否则你无法访问子变量。

坚持基础“私有成员变量只能通过自己的成员函数在类中访问,并且字段不能被继承”所以基本上当你访问父类中的私有变量A时,该方法应该访问它自己的私有成员而不是然后是任何子类的私有领域。

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

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