简体   繁体   English

家长班级公开成员召集的儿童班级私有成员

[英]Child class private member called from Parent class public member

What is happening in the code below? 下面的代码中发生了什么? Please explain the output: 请解释输出:

class Parent{
    private void fun(){
        System.out.println("parent fun\n");
    }
    public void accessFun(){
        System.out.println(this);
        this.fun();
    }
}

class Child extends Parent{
    private void fun(){
        System.out.println("child fun");
    }
}

class Test{
    public static void main(String[] args) {
        Child a = new Child();
        Parent b = new Parent();
        Parent c = new Child();
        a.accessFun();
        b.accessFun();
        c.accessFun();
    }
}

Output: 输出:

Child@7960847b

parent fun

Parent@3b192d32

parent fun

Child@16f65612

parent fun

Why the line this.fun() is not giving compile-time error? 为什么这行this.fun()没有给出编译时错误?

I think fun is a private member in Child class and therefore can't be accessed from outside the Child class(from public member of it's Parent class). 我认为fun是Child类的私有成员,因此不能从Child类之外(从其Parent类的公共成员)访问。

Why Parent class version of fun() is being called by this.fun()? 为什么this.fun()会调用fun()的父类版本? Note this refers to child class object. 请注意, 是指子类对象。

Private members are not inherited. 私有成员不会被继承。

I think this might be the key point that you are missing here. 我认为这可能是您在这里缺少的重点。 What this means is that Child.fun does not override Parent.fun . 这意味着Child.fun不会覆盖Parent.fun They are just two plain old methods that has nothing to do with each other. 它们只是两个彼此无关的简单老方法。

When you call accessFun , control always goes into this bit of code in Parent : 当您调用accessFun ,控件始终进入Parent以下代码:

public void accessFun(){
    System.out.println(this);
    this.fun(); <---- here
}

Now, since we are now inside Parent , we can access fun . 现在,由于我们现在位于Parent ,因此可以访问fun And since Parent.fun is not overridden , it calls Parent.fun and not Child.fun . 并且由于Parent.fun 没有被覆盖 ,因此它将调用Parent.fun而不是Child.fun

I think fun is a private member in child class and therefore can't be accessed from outside the class(may even from public member of it's Parent class). 我认为乐趣是孩子班级的私人成员,因此不能从班级外部访问(甚至可以从其父班级的公共成员那里访问)。

That is a complete misunderstanding. 那是一个完全的误解。 if private members can't be accessed from outside of the class, not even through public methods, then they will be much less useful. 如果不能从类外部访问私有成员,甚至不能通过公共方法访问私有成员,那么它们的作用将大大减少。 Why even have them in the first place? 为什么还要放在首位呢?

"Private members can only be accessed by members declared in the same class" is probably a better thing to remember. “私有成员只能由在同一类中声明的成员访问”可能要记住。

private fun() in Parent is called by public accessFun() of same Parent class. Parent中的private fun()由同一Parent类的public accessFun()调用。 And public accessFun() is called in main() of Test class. 在Test类的main()中调用public accessFun()
Here accessFun() is public so it can be called from any where. 这里的accessFun()是公共的,因此可以从任何地方调用它。
For example just like Pojo classes private members are accessed through public setters and getters 例如,就像Pojo类一样,私有成员可以通过公共设置者和获取者来访问

The fun method in parent is private and the child fun() method is not overriding the parent fun() method. 父级中的fun方法是私有的,子级fun()方法不会覆盖父级fun()方法。 But the accessFun method is public so it can be called from the child class and is publicly accessible. 但是accessFun方法是公共的,因此可以从子类中调用它,并且可以公共访问。

So when you call accessFun() method it will be calling the parent class fun() method. 因此,当您调用accessFun()方法时,它将调用父类fun()方法。 Not the child class fun() method as it is private. 不是子类的fun()方法,因为它是私有的。 That is why you wont get an error. 这就是为什么您不会收到错误的原因。

I think fun is a private member in child class and therefore can't be accessed from outside the class(may even from public member of it's Parent class). 我认为乐趣是孩子班级的私人成员,因此不能从班级外部访问(甚至可以从其父班级的公共成员那里访问)。

Sometimes everything comes from "outside". 有时,一切都来自“外部”。 The main call comes from outside for example. 例如, main呼叫来自外部。

If a private member can not be accessed from outside, it would always be dead code and this makes no sense. 如果无法从外部访问私有成员,则它将始终是无效代码,这没有任何意义。

The difference is the understanding from direct-access and indirect-access. 区别在于对直接访问和间接访问的理解。

In all cases you call the method non-directly via the method accessFun() . 在所有情况下,都可以通过accessFun()方法非直接地调用该方法。

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

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