车继承小题

该博客展示了非机动车类`FeiJiDong`的定义,包括品牌、颜色、轮子数量和座椅数量等属性。通过继承,创建了自行车`Bycicle`、三轮车`SanLun`和电动车`DianDong`的子类。每个子类都覆盖了`yunxing`方法,展示各自的特点。在`Main`类中,实例化了各类并调用`yunxing`方法进行演示。
摘要由CSDN通过智能技术生成

在这里插入图片描述
1.非机动车类

package M8.D10.Car;

public class FeiJiDong {
    private String brand;
    private String color;
    private int wheel=2;
    private int chair=1;

    public FeiJiDong(){}

    public FeiJiDong(String brand,String color){
        this.brand=brand;
        this.color=color;
    }
    public FeiJiDong(String brand,String color,int wheel,int chair){
        this.brand=brand;
        this.color=color;
        this.chair=chair;
        this.wheel=wheel;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getWheel() {
        return wheel;
    }

    public void setWheel(int wheel) {
        this.wheel = wheel;
    }

    public int getChair() {
        return chair;
    }

    public void setChair(int chair) {
        this.chair = chair;
    }

    public void yunxing(){
        System.out.println("这是一辆"+this.getColor()+"颜色"+this.getBrand()+"品牌的非机动车,有"+this.getWheel()+"个轮子,有"+this.getChair()+"个座椅");
    }
}

2.自行车类

public class Bycicle extends FeiJiDong{

    public  Bycicle(String brand,String color,int wheel,int chair){
        super(brand,color,wheel,chair);
    }

    @Override
    public void yunxing() {
        System.out.println("这是一辆"+this.getColor()+"颜色"+this.getBrand()+"品牌的自行车");
    }
}

3.三轮车类

public class SanLun extends  FeiJiDong{
    public SanLun(){
        super.setWheel(3);
    }

    @Override
    public void yunxing() {
        System.out.println("三轮车是一款拥有"+this.getWheel()+"个轮子的非机动车");
    }
}

4.电动车类

public class DianDong extends FeiJiDong{
    private String DianChi;

    public String getDianChi() {
        return DianChi;
    }

    public void setDianChi(String dianChi) {
        DianChi = dianChi;
    }

    @Override
    public void yunxing() {
        System.out.println("这是一辆使用"+this.getDianChi()+"牌子电池的电动车");
    }
}

5.Main类

public class Main {
    public static void main(String[] args) {
        FeiJiDong feiJiDong=new FeiJiDong("天宇牌","红颜色");
        feiJiDong.yunxing();

        Bycicle bycicle=new Bycicle("捷安特牌","黄颜色",2,1);
        bycicle.yunxing();

        SanLun sanLun=new SanLun();
        sanLun.yunxing();

        DianDong dianDong=new DianDong();
        dianDong.setDianChi("飞鸽");
        dianDong.yunxing();

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值