Java继承练习题

该博客探讨了如何使用面向对象编程思想设计Java类,包括非机动车(自行车、电动车、三轮车)、Person对象、水果(杨梅、仙人蕉)的继承关系和方法重写。内容涵盖构造方法、运行方法、属性、toString方法、equals方法以及特殊方法的实现。
摘要由CSDN通过智能技术生成

编程练习:

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

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

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

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

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

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

代码:
父类

package com.dodoke.car;

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.setColor(color);
		this.setType(type);
		this.setWheel(wheel);
		this.setSeat(seat);
	}

	// 每项的get和set
	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.dodoke.car;

public class Bike extends Car {
   
	// 自行车
	// 无参构造
	public Bike() {
   

	}

	// 重写父类
	public Bike(String color, String type) {
   
		super(color, type);
		System.out.println("自行车类信息测试:这是一辆" + color + <
  • 21
    点赞
  • 100
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值