Java继承中的私有成员

发布于 2024-11-18 03:30:03 字数 100 浏览 3 评论 0原文

有人告诉我,对于 Java 子类,它可以继承其超类的所有成员。那么这是否意味着私人成员也是如此?我知道它可以继承受保护的成员。

有人可以向我解释一下吗?我现在完全困惑了。

I was told that for a Java subclass it can inherit all members of its superclass. So does this mean even private members? I know it can inherit protected members.

Can someone explain this to me. I am now totally confused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情定在深秋 2024-11-25 03:30:03

不,私有成员不会被继承,因为私有成员的范围仅限于到定义它的类。只有 public 和 protected 成员才会被继承。

来自 Java 文档

超类中的私人成员

子类不继承
其父类的私有成员。

但是,如果超类有公共
或访问其受保护的方法
私有字段,这些也可以使用
由子类。嵌套类有
访问所有私有成员
它的封闭类——字段和
方法。因此,公众或
由 a 继承的受保护嵌套类
子类可以间接访问所有
超类的私有成员。

来自 JLS,

声明的类的成员
private 不会被继承

该类的子类。仅限会员
被声明为 protected 的类
或 public 被子类继承
在除以下包之外的包中声明
其中声明了该类。

一个有用的链接:子类继承私有字段吗?

No, the private member are not inherited because the scope of a private member is only limited to the class in which it is defined. Only the public and protected member are inherited.

From the Java Documentation,

Private Members in a Superclass

A subclass does not inherit the
private members
of its parent class.
However, if the superclass has public
or protected methods for accessing its
private fields, these can also be used
by the subclass. A nested class has
access to all the private members of
its enclosing class—both fields and
methods. Therefore, a public or
protected nested class inherited by a
subclass has indirect access to all of
the private members of the superclass.

From the JLS,

Members of a class that are declared
private are not inherited
by
subclasses of that class. Only members
of a class that are declared protected
or public are inherited by subclasses
declared in a package other than the
one in which the class is declared.

A useful link : Does subclasses inherit private fields?

你げ笑在眉眼 2024-11-25 03:30:03

这取决于您对“继承”一词的确切用法。我将通过示例进行解释。

假设您有两个类:ParentChild,其中 Child 扩展了 Parent。此外,Parent 有一个名为value 的私有整数。

现在问题来了:Child是否继承了私有value?在 Java 中,继承的定义方式是“否”。然而,在一般的 OOP 术语中,存在轻微的歧义。

您可以说它没有继承,因为Child无法显式引用value。即任何像 this.value 这样的代码都不能在 Child 中使用,也不能在某些调用代码中使用 obj.value (显然) 。

然而,从另一种意义上来说,您可以说value继承的。如果您认为 Child 的每个实例也是 Parent 的实例,则该对象必须包含 value 作为在Parent中定义。即使 Child 类对此一无所知,名为 value 的私有成员仍然存在于 Child 的每个实例中。因此,从这个意义上说,您可以说 value 是在 Child 中继承的。

因此,在不使用“继承”这个词的情况下,只需记住子类不知道父类中定义的私有成员。但还要记住,这些私有成员仍然存在于子类的实例中。

This kind of depends on your exact usage of the word inheritance. I'll explain by example.

Suppose you have two classes: Parent and Child, where Child extends Parent. Also, Parent has a private integer named value.

Now comes the question: does Child inherit the private value? In Java, inheritance is defined in such a way that the answer would be "No". However, in general OOP lingo, there is a slight ambiguity.

You could say that it not inherited, because nowhere can Child refer explicitly to value. I.e. any code like this.value can't be used within Child, nor can obj.value be used from some calling code (obviously).

However, in another sense, you could say that value is inherited. If you consider that every instance of Child is also an instance of Parent, then that object must contain value as defined in Parent. Even if the Child class knows nothing about it, a private member named value still exists within each and every instance of Child. So in this sense, you could say that value is inherited in Child.

So without using the word "inheritance", just remember that child classes don't know about private members defined within parent classes. But also remember that those private members still exist within instances of the child class.

在你怀里撒娇 2024-11-25 03:30:03

在这里您将百分百满意。我在我的电脑上测试了它,我的结论是我将把它发布在这里。只需浏览下面编写的程序,查看程序输出并阅读最后给出的结论即可。
要亲自测试它,请复制整个程序并将其保存在名为“InheritanceTest.java”的文件中,然后编译并最终运行它。

程序

// Testing if a subclass can access the private members of a superclass

class Class1 {
    private String name;

    public void setName(String name) {
        this.name = name;
        System.out.println("The name has been set successfully.");
    }

    public void showName() {
        System.out.println("The name is: " + name);
    }
}

class Class2 extends Class1 {
    private int age;

    public void setAge(int age) {
        this.age = age;
        System.out.println("The age has been set successfully.");
    }

    public void showAge() {
        System.out.println("The age is: " + age);
    }

    public void displayName() {
        //Accessing the private member of superclass here
        //System.out.println("The name is: " + name); //error, can't compile because access to the private member name of the superclass Class1 is not permitted here.
    }
}

class InheritanceTest {
    public static void main(String[] args) {

        Class1 c1 = new Class1();
        Class2 c2 = new Class2();

        c1.setName("Name_C1");
        c2.setName("Name_C2"); //No error, setName() is a public member of the superclass which indirectly gives access to the private member "name".

        c1.showName();
        c2.showName(); //No error, showName() is a public member of the superclass which indirectly gives access to the private member "name".

        c2.setAge(25);
        c2.showAge();

        //c2.displayName(); //error
    }
}

输出

The name has been set successfully.
The name has been set successfully.
The name is: Name_C1
The name is: Name_C2
The age has been set successfully.
The age is: 25

结论

是的,子类可以间接访问超类的私有成员。子类不能直接访问超类的私有成员。

超类的所有公共、私有和受保护成员(即所有字段和方法)都由子类继承,但子类只能直接访问超类的公共和受保护成员。如果从超类继承的成员允许访问超类的私有成员,则子类可以使用此继承的成员来访问超类的私有成员。

You will be satisfied here 100%. I tested it on my computer and what I concluded I'm going to post it here. Just go through the program written below, see the program output and READ THE CONCLUSION given at the end.
To test it yourself, copy the whole program and save it in a file named "InheritanceTest.java" then compile it and finally run it.

Program

// Testing if a subclass can access the private members of a superclass

class Class1 {
    private String name;

    public void setName(String name) {
        this.name = name;
        System.out.println("The name has been set successfully.");
    }

    public void showName() {
        System.out.println("The name is: " + name);
    }
}

class Class2 extends Class1 {
    private int age;

    public void setAge(int age) {
        this.age = age;
        System.out.println("The age has been set successfully.");
    }

    public void showAge() {
        System.out.println("The age is: " + age);
    }

    public void displayName() {
        //Accessing the private member of superclass here
        //System.out.println("The name is: " + name); //error, can't compile because access to the private member name of the superclass Class1 is not permitted here.
    }
}

class InheritanceTest {
    public static void main(String[] args) {

        Class1 c1 = new Class1();
        Class2 c2 = new Class2();

        c1.setName("Name_C1");
        c2.setName("Name_C2"); //No error, setName() is a public member of the superclass which indirectly gives access to the private member "name".

        c1.showName();
        c2.showName(); //No error, showName() is a public member of the superclass which indirectly gives access to the private member "name".

        c2.setAge(25);
        c2.showAge();

        //c2.displayName(); //error
    }
}

Output

The name has been set successfully.
The name has been set successfully.
The name is: Name_C1
The name is: Name_C2
The age has been set successfully.
The age is: 25

Conclusion

Yes, a subclass can indirectly access the private members of a superclass. A subclass can't directly access the private members of a superclass.

All the public, private and protected members (i.e. all the fields and methods) of a superclass are inherited by a subclass but the subclass can directly access only the public and protected members of the superclass. If an inherited member from a superclass gives access to a private member of the superclass then the subclass can use this inherited member to access the private member of the superclass.

箜明 2024-11-25 03:30:03

IMO 绝不是一个定义问题。在基于类的继承中,意味着将行为传播给后代。由于此类私有成员确实会被继承,因此我不会详细说明这是如何发生的。

实际上,我发现“不继承”的答案对于新开发人员来说是危险的,他们不会立即理解私有成员隐藏在你的类的皮肤下,它们(可以)对其行为、类的大小产生严重影响。 在计算机科学中,“开发先于理解”是很常见的,但是

,假设某些编写基于类的著名 OO 平台手册的技术人员采用了错误的“定义”,我们应该避免构建(或破坏)我们的 OOP 概念化。

很抱歉在这么旧的帖子中陈述一些内容,但这个问题总是有效的。

IMO by no way is it a matter of definition. In class-based Inheritance implies propagation of behavior to descendants. As such private members DO get inherited , and I will not go into the details how this happens.

Actually I find the "not inherited" answer to be dangerous for new developers and they do not comprehend right away that the private members are there hidden under the skin of your class and they (can) have severe impact on its behavior, size of the objects etc.

It is common that "development comes before understanding" in computer science, however lets avoid building (or destroying) our conceptualization of OOP assuming the wrong "definition" adopted by some technician writing the manual of a well known class based OO platform.

Sorry for stating something in such an old post, but the issue is always valid.

树深时见影 2024-11-25 03:30:03

虽然 https://docs.oracle .com/javase/specs/jls/se8/html/jls-8.html#jls-8.2显示私有成员不被继承。实际上,它是被子类继承的。当我们使用调试器跟踪变量时,它会在“继承”标签下显示私有成员,所以试试吧。还有一个帖子讨论这个问题,大部分都认为不是遗传的,这误导了很多人,包括我一开始。

Though https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.2 shows that Private Members are not inherited. Actually, it is inherited by subclass. When we use debuggers to trace variables, it will show the private members under the label of "inherited", so just try it. there is another post discussing this question, and most of them think not inherited, which misleads many people, including me at first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文