日志文章

2019-12-14 aflfte2011

继承当中方法的重写操作方法

package com.aflfte.oo2;
/***
 * 方法重写Override的用法
 * @author root
 *重写返回值大小要小于等于父类
 */
public class TestOverride {

    public static void main(String[] args) {
        Horse h=new Horse();
        h.run();
    }
}
class Vehicle{
    public void run() {
        System.out.println("走。。。。。。。。。");
    }
    public void stop() {
        System.out.println("站住!!!");
    }
    public C1 who() {
        return new C1();
    }
}
class Horse extends Vehicle{
    public void run() {//在子类中重新定义方法可重写父类的方法语块
        System.out.println("跑起来。。。。。");
    }
    public C2 who() {//重写方法必须小于等于父类的方法块
        return new C2();
    }
}
例子当中Vehicle为父类,Horse为子类,子类要想重写父类的方法需要重新编写一下方法即可,但是格式必须保持一致

« Object的使用 | 类的继承关系»