Java继承练习题

本文通过设计非机动车类(自行车、电动车、三轮车)和Person类,以及水果类(杨梅、香蕉)来讲解Java的继承和面向对象思想。详细介绍了各子类如何重写父类方法,实现特定功能。


一、某公司要开发“XX车行管理系统”,请使用面向对象的思想,设计自定义类描述自行车、电动车和三轮车。 程序参考运行效果图如下:

在这里插入图片描述

任务分析:
第一步:分析自行车、电动车和三轮车的共性:
(1)、都是非机动车,具有非机动车的基本特征
(2)、都有运行的方法

第二步:根据共性,定义非机动车
属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)
方法:
(1)、编写无参构造方法、双参构造方法和四参构造方法,其中,在双参构造方法中,完成对品牌和颜色的赋值;在四参构造方法中,完成对所有属性的赋值
(2)、编写运行的方法,描述内容为:这是一辆xx颜色的,xx牌的非机动车,有xx个轮子,有xx个座椅的非机动车。其中xx的数据由属性提供

第三步:定义自行车、电动车和三轮车分别继承自行车类,要求:
(1)、自行车类:
①.在构造方法中调用父类多参构造,完成属性赋值
②.重写运行方法,描述内容为:这是一辆xx颜色的,xx牌的自行车。其中xx的数据由属性提供

(2)、电动车:
①.增加“电池品牌”属性
②.重写运行方法,描述内容为:这是一辆使用xx牌电池的电动车。其中xx的数据由属性提供

(三)、三轮车:
①.在无参构造中实现对轮子属性值进行修改
②.重写运行方法,描述内容为:三轮车是一款有xx个轮子的非机动车。其中xx的数据由属性提供

父类

package com.hopu;
//父类
public class Car {
   
   
    private String color;// 颜色

    private String type;// 品牌

    private int wheel;// 轮子

    private int seat;// 座椅

    public Car() {
   
   
    }
    public Car(String color, String type) {
   
   
        this.setColor(color);
        this.setType(type);
    }
    public Car(String color, String type, int wheel, int seat) {
   
   
        this.color = color;
        this.type = type;
        this.wheel = wheel;
        this.seat = seat;
    }

    public String getColor() {
   
   
        return color;
    }

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

    public String getType() {
   
   
        return type;
    }

    public void setType(String type) {
   
   
        this.type = type;
    }

    public int getWheel() {
   
   
        return wheel;
    }

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

    public int getSeat() {
   
   
        return seat;
    }

    public void setSeat(int seat) {
   
   
        this.seat = seat;
    }
    public String info() {
   
   
        String str = "父类信息测试:这是一辆" + this.getColor() + "颜色的," + this.getType() + "牌的非机动车,有" + this.getWheel() + "个轮子,有"
                + this.getSeat() + "个座椅";
        return str;
    }
}

自行车

package com.hopu;

public class Bike extends Car{
   
   
    public  void Bike(){
   
   

    }
    //
    public   Bike(String color,String type){
   
   
        super(color,type);
        System.out.println("自行车类信息测试:这是一辆" + color + "颜色的," 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值