gpt4 book ai didi

Java重写私有(private)函数不显示多态行为

转载 作者:行者123 更新时间:2023-11-30 23:51:45 25 4
gpt4 key购买 nike

public class Shape
{

final private void print()
{
System.out.println("in class Shape");
}

public static void main(String[] args)
{

Shape shape=new Rectangle();
shape.print();
//calling shape class function
//giving output in class shape


}

 public class Rectangle extends Shape
{
public void print()
{
System.out.println("in class Rectangle");
//super.print();

}
}

问题:为什么私有(private)函数不显示多态行为? 而我们仍在重写 final方法?它调用基类函数为什么?

最佳答案

私有(private)函数对其子函数不可见,也不可调用;因此,这是两个完全不同的功能。从子类的角度来看,没有什么可以覆盖的,因为它不知道父类甚至 print() 函数。

关于Java重写私有(private)函数不显示多态行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27167661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com