java 继承实例

java继承:

以汽车举例

  • 创建父类Car,并创建公共属性color、wheel、engine
  • 创建子类Sport_Car,并创建私有属性tailplane(尾翼),再重写run()方法
  • 创建子类SUV,并创建私有属性warehouse(货仓),再重写run()方法
  • 创建java文件,并使用主函数创建对象进行调用

一、父类Car:

public class Car {
    // 定义私有属性
    private String color;
    private String wheel;
    private String engine;
//    public
    // 定义方法
    public void run(){
        System.out.println("汽车正在奔跑");
    }
    public Car(){
        super();
    }
    public Car(String color,String wheel,String engine){
        this.color = color;
        this.wheel = wheel;
        this.engine = engine;
    }
//  提供公共属性让其他类进行访问,get方法得到属性的值,set方法给属性赋值
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }

    public String getWheel() {
        return wheel;
    }
    public void setWheel(String wheel) {
        this.wheel = wheel;
    }

    public String getEngine() {
        return engine;
    }
    public void setEngine(String engine) {
        this.engine = engine;
    }
}

二、子类Sport_car:

public class Sport_car extends Car{
    // 定义跑车的私有属性
    private String tailplane;   //尾翼
    // 定义无参构造函数
    public Sport_car(){
        super();
    }
    public Sport_car(String color,String wheel,String engine,String tailplane){
        super(color,wheel,engine);
        this.tailplane = tailplane;
    }
    // 重写run()方法
    public void run(){
        System.out.println("我是跑车");
        System.out.println("我的颜色是"+super.getColor());
        System.out.println("我的轮子数是"+super.getWheel());
        System.out.println("我的引擎是"+super.getEngine());
        System.out.println("我的特点是我有"+this.tailplane);
    }
    //  提供公共属性让其他类进行访问,get方法得到属性的值,set方法给属性赋值
    public String getTailplane() {
        return tailplane;
    }
    public void setTailplane(String tailplane) {
        this.tailplane = tailplane;
    }
}

三、子类SUV:

public class SUV extends Car {
    private String warehouse;
    // 创建无参的构造方法
    public SUV(){
        super();
    }
    // 创建有参的构造方法
    public SUV(String color,String wheel,String engine,String warehouse){
        super(color,wheel,engine);
        this.warehouse = warehouse;
    }
    // 重写run()方法
    public void run(){
        System.out.println("我是卡车");
        System.out.println("我的颜色是"+super.getColor());
        System.out.println("我的轮子数是"+super.getWheel());
        System.out.println("我的引擎是"+super.getEngine());
        System.out.println("我的特点是我有"+this.warehouse);
    }
    //  提供公共属性让其他类进行访问,get方法得到属性的值,set方法给属性赋值
    @Override
    public String getWheel() {
        return super.getWheel();
    }
    public void setWarehouse(String warehouse) {
        this.warehouse = warehouse;
    }
}

四、创建主函数运行

public class Test {
    public static void main(String[] args) {
        // 创建子类sport_car对象,set方法可以给属性进行重新赋值
        Sport_car sport_car = new Sport_car();
        sport_car.setColor("灰色");
        sport_car.setWheel("四个");
        sport_car.setEngine("八个缸");
        sport_car.setTailplane("尾翼");
        sport_car.run();

        // 创建suv对象
        SUV suv = new SUV();
        suv.setColor("白色");
        suv.setWheel("八个");
        suv.setEngine("四个缸");
        suv.setWarehouse("货仓");
        suv.run();
    }
}

结果如下:
在这里插入图片描述


  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值