从子类访问父类的私有实例字段… 但它有效吗?

huangapple go评论64阅读模式
英文:

Accessing private instance fields of parent class from subclass.. but it works?

问题

以下是您要翻译的代码部分:

I have a parent class that has 2 private instance fields but my child class which has no instance fields 'magically' creates 2 instance fields.

public class Parent 
{
    private String firstName;
    public String lastName;
    private int age;

    public Parent()
    {
        System.out.println("No-Parameter Constructor");
    }

    public Parent(String firstName, String lastName, int age)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this age = age;
    }

    public String toString()
    {
        return "firstName: " + firstName + " lastName: " + lastName + " age: " + age;
    }

    public String getFN()
    {
        return firstName;
    }
}

public class Student extends Parent 
{

    public Student(String firstName, String lastName, int age)
    {
        super(firstName, lastName, age);
    }


    public static void main(String[] args) 
    {
        Parent p = new Parent("Logi", "Tech", 42);
        Student s = new Student("Logi", "Camera", 21);

        System.println(p);
        System.println(s);
        System.println(p);
        System.println(p.getFN());
    }
}

Output:

firstName: Logi lastName: Tech age: 42
firstName: LogiStudent lastName: Camera age: 21
firstName: Logi lastName: Tech age: 42
LogiStudent
英文:

I have a parent class that has 2 private instance fields but my child class which has no instance fields 'magically' creates 2 instance fields.

public class Parent 
{
    private String firstName;
    public String lastName;
    private int age;

    public Parent()
    {
        System.out.println("No-Parameter Constructor");
    }

    public Parent(String firstName, String lastName, int age)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    public String toString()
    {
        return "firstName: " + firstName + " lastName: " + lastName + " age: " + age;
    }

    public String getFN()
    {
        return firstName;
    }
}

public class Student extends Parent 
{

    public Student(String firstName, String lastName, int age)
    {
        super(firstName, lastName, age);
    }


    public static void main(String[] args) 
    {
        Parent p = new Parent("Logi", "Tech", 42);
        Student s = new Student("Logi", "Camera", 21);

        System.out.println(p);
        System.out.println(s);
        System.out.println(p);
        System.out.println(p.getFN());
    }
}

Output:

firstName: Logi lastName: Tech age: 42
firstName: LogiStudent lastName: Camera age: 21
firstName: Logi lastName: Tech age: 42
LogiStudent

答案1

得分: 2

你没有从Student类中访问私有字段。只需看看你的代码 - 在class Student extends Parent {}的主体中,你以任何方式都没有提及这些字段。

class Student extends Parent 是 Java 语言的一种表达方式: "让我们定义一个新的概念叫做 Student。首先,将父类 Parent 的一切都继承下来,然后再在其基础上添加一些额外的东西"。

换句话说,如果父类 Parent 有字段 age,那么 Student 也有这个字段。

可访问性与这个概念几乎没有关系;可访问性(private)涉及的是哪些代码(而不是哪个实例!!)被允许直接与这些东西进行交互。由于 age 字段是 private,尽管每个 Student 的实例都有这个字段,但位于 Student.java 中的代码不能直接访问该字段。当然,它可以自由地调用其父类允许访问的方法(例如 toString 方法,它是 public),然后观察它如何触及这些字段。这不是"直接访问",这是间接访问,这是可以接受的。

同样,Student 可以在任何地方调用 getFN(),从而获取名字。但它不能直接设置名字,除非 Parent 决定添加一个 void setFirstName(String fn) { this.firstName = fn; } 方法,当然了。

英文:

You are not accessing private fields from the Student class. Just look at your code - nowhere in the body of class Student extends Parent {} do you refer to those fields in any way.

class Student extends Parent is java-ese for: "Let's define this new concept called Student. Begin by taking absolutely every single last thing Parent has, and then bolt, on top of all that, a few more things".

In other words, if Parent has the field 'age, then so does Student.

accessibility is almost entirely unrelated to this notion; accessibility (private) is about which code (NOT which instance!!) is allowed to directly interact with things. Given that the age field is private, whilst every instance of Student has that field, the code located in Student.java cannot touch that field directly. It is of course free to invoke a method that its superclass does allow access to (such as the toString method which is public) and then observe as IT touches those fields. That's not 'direct access', that's indirect, and that is fine.

Similarly, Student can invoke getFN() anywhere it wants, and thus get the first name. It cannot, however, set the firstname, unless Parent decides to add a void setFirstName(String fn) { this.firstName = fn; } method, of course.

huangapple
  • 本文由 发表于 2023年2月8日 12:39:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定