- 在子类中可以根据需要对从基类中继承来的方法进行重写。
- 重写方法必须和被重写方法具有相同方法名称、参数列表和返回类型。
- 重写方法不能使用比被重写方法更严格的访问权限。(由于多态)
package theChildOfGod.bjxy.oop.inherit;
public class Animal{
String eye;
public void run() {
System.out.println("跑跑!");
}
public void eat() {
System.out.println("吃吃!");
}
public void sleep() {
System.out.println("zzzzzz");
}
}
class Mammal extends Animal{
public void taisheng() {
System.out.println("我是胎生");
}
}
class Bird extends Animal{
public void run(){
super.run(); //bird里面调用父类
System.out.println("我是一只小小鸟!飞呀飞不高!");
}
public void eggSheng() {
System.out.println("卵生");
}
}
package theChildOfGod.bjxy.oop.inherit;
public class Test {
public static void main(String[] args) {
Bird b = new Bird();
b.run();
}
}
补充:ctrl+T (F4)可以查看一个类的父类。