方法覆盖override的用法

方法覆盖(override)”的要点

方法覆盖要求子类与父类的方法一模一样,否则就是方法重载(overload)!

在子类中,若要调用父类中被覆盖的方法,可以使用super关键字。

以下子类中调用父类的代码:

public class Grandparent {
public Grandparent()
{

System.out.println("GrandParent Created.");

}
public void show()
{
System.out.println("父类的show()方法");
}

public Grandparent(String string)
{

System.out.println("GrandParent Created.String:" + string);
}

}

class Parent extends Grandparent
{


public Parent()
{
super();

System.out.println("Parent Created");
}
public void show()
{super.show();
System.out.println("子类的show()方法");
}
}

public class Test {

public static void main(String args[])
{

Parent p = new Parent();
p.show();
}
}

posted @ 2022-10-30 11:17  一统天下。  阅读(46)  评论(0编辑  收藏  举报