这是面试问题。

子类继承private吗 字段?

我的回答是“不”,因为我们不能使用“正常的面向对象的方式”访问它们。但是采访者认为它们是继承的,因为我们可以间接或使用反射访问这些字段,并且它们仍然存在于对象中。

回来后,我在javadoc中找到了以下引用:

超类中的私人成员 一个 子类不继承私有 父类的成员。

你知道面试官观点的论据吗?


当前回答

我们可以简单地声明,当父类被继承时,父类的私有成员实际上成为子类的私有成员,并且不能被进一步继承或子类的对象不可访问。

其他回答

例如,

class Person {
    private String name;

    public String getName () {
        return this.name;
    }

    Person(String name) {
        this.name = name;
    }
}
public class Student extends Person {

    Student(String name) {
        super(name);
    }
    
    public String getStudentName() {
        return this.getName(); // works
        // "return this.name;" doesn't work, and the error is "The field Person.name is not visible"

    }
}

public class Main {
    public static void main(String[] args) {
        Student s = new Student("Bill");

        String name = s.getName(); // works
        // "String name = s.name;" doesn't work, and the error is "The field Person.name is not visible"

        System.out.println(name);
    }
}

我必须回答,Java中的私有字段是继承的。请允许我演示一下:

public class Foo {

    private int x; // This is the private field.

    public Foo() {
        x = 0; // Sets int x to 0.
    }

    //The following methods are declared "final" so that they can't be overridden.
    public final void update() { x++; } // Increments x by 1.
    public final int getX() { return x; } // Returns the x value.

}


public class Bar extends Foo {

    public Bar() {

        super(); // Because this extends a class with a constructor, it is required to run before anything else.

        update(); //Runs the inherited update() method twice
        update();
        System.out.println(getX()); // Prints the inherited "x" int.

    }

}

如果你在一个程序中运行Bar Bar = new Bar();,那么你总是会在输出框中看到数字“2”。由于整数“x”被update()和getX()方法封装,因此可以证明该整数是继承的。

令人困惑的是,因为不能直接访问整数“x”,所以人们认为它不是继承的。然而,类中的每一个非静态的东西,无论是字段还是方法,都是继承的。

我可以试着帮你。 当一个子类(例如命名为B)扩展一个超类(例如命名为a)时,它自动从它的超类继承字段(例如属性和/或方法)。 现在,B在它的内存布局中为类A中的每个字段提供了空间,包括私有字段。事实上,Java不允许子类B使用私有字段,因为它们是私有的。

我将用代码演示这个概念。子类实际上继承了父类的私有变量。唯一的问题是他们不能访问 子对象,除非您为私有变量提供公共getter和setter 在超级班。

考虑包转储中的两个类。子扩展父。

如果我没记错的话,内存中的子对象由两个区域组成。一个是父部分,另一个是子部分。子程序可以访问私有文件 节只能通过父类中的公共方法在其父类的代码中调用。

这样想。波拉特的父亲博尔托克有一个装有10万美元的保险箱。他不想共享他的“私有”变量保险箱。所以,他没有提供保险箱的钥匙。波拉特继承了保险箱。但是,如果他连门都打不开,那又有什么用呢?要是他的 爸爸提供了钥匙。

父母,

package Dump;

public class Parent {

    private String reallyHidden;
    private String notReallyHidden;

    public String getNotReallyHidden() {
        return notReallyHidden;
    }

    public void setNotReallyHidden(String notReallyHidden) {
        this.notReallyHidden = notReallyHidden;
    }

}//Parent

孩子,

package Dump;

public class Child extends Parent {

    private String childOnly;

    public String getChildOnly() {
        return childOnly;
    }

    public void setChildOnly(String childOnly) {
        this.childOnly = childOnly;
    }

    public static void main(String [] args){

        System.out.println("Testing...");
        Child c1 = new Child();
        c1.setChildOnly("childOnly");
        c1.setNotReallyHidden("notReallyHidden");

        //Attempting to access parent's reallyHidden
            c1.reallyHidden;//Does not even compile

    }//main

}//Child

不,私有字段不被继承。唯一的原因是子类不能直接访问它们。