Java继承练习题

编程练习:

  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 + "颜色的," + type + "牌的自行车");
	}
}

电动车

package com.dodoke.car;

public class DianDong extends Car {
	// 电动车 增加“电池品牌”属性
	private String dianchi;

	// 无参构造方法
	public DianDong() {

	}

	// get/set
	public String getDianchi() {
		return dianchi;
	}

	public void setDianchi(String dianchi) {
		this.dianchi = dianchi;
	}

	// 重写父类
	public DianDong(String dianchi) {
		super();
		this.dianchi = dianchi;
		System.out.println("电动车类信息测试:这是一辆使用" + this.getDianchi() + "牌电池的电动车");
	}
}

三轮车

package com.dodoke.car;

public class SanLun extends Car {
	// 三轮车
	// 无参构造方法实现对轮子属性值进行修改
	public SanLun() {
		super();
		this.setWheel(3);
	}

	// 重写方法
	public String sl() {
		String str = "三轮车类测试信息:三轮车是一款有" + this.getWheel() + "个轮子的非机动车";
		return str;
	}
}

测试

package com.dodoke.test;

import com.dodoke.car.Bike;
import com.dodoke.car.Car;
import com.dodoke.car.DianDong;
import com.dodoke.car.SanLun;

public class Test {
	public static void main(String[] args) {
		Car car = new Car("红", "天宇", 4, 2);
		System.out.println(car.info());
		Bike bike = new Bike("黄", "捷安特");
		DianDong diandong = new DianDong("飞鸽");
		SanLun sanlun = new SanLun();
		System.out.println(sanlun.sl());
	}
}

  1. 请使用面向对象的思想,设计自定义类Person继承Object类,重写toString方法实现对象信息输出。

    运行效果如下图所示:
    在这里插入图片描述

    思路分析

    (1)、创建一个 Person 类继承自 Object,其中类的结构要求为:
    属性:name(姓名)、age(年龄)、sex(性别)

方法:
①.创建带参(name、age、sex为参数)构造方法
②.重写 toString 方法,输出信息格式为:姓名:xx 年龄:xx性别:xx(其中,xx为对象对应属性值)
(2)、创建测试类,在测试方法中,实例化 Person对 象,并传入三个属性值。然后,分别通过直接打印Person对象以及利用重写的 toString 方法,打印输出2行对象信息。

代码:
自定义类

package com.dodoke.people;

public class Person {
	// 属性:name(姓名)、age(年龄)、sex(性别)
	private String name;// 姓名

	private int age;// 年龄

	private String sex;// 性别

	// 无参构造方法
	public Person() {

	}

	// 带参构造方法
	public Person(String name, int age, String sex) {
		this.setName(name);
		this.setAge(age);
		this.setSex(sex);
	}

	// 各项set/get
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	// 重写 toString 方法
	@Override
	public String toString() {
		String str = "姓名:" + this.getName() + "  年龄:" + this.getAge() + "  性别:" + this.getSex();
		return str;
	}

}

测试类

package com.dodoke.test;

import com.dodoke.people.Person;

public class TestPerson {
	public static void main(String[] args) {
		Person person = new Person("李明", 18, "男");
		System.out.println(person);
		System.out.println(person.toString());
	}
}

  1. 请使用面向对象的思想,实现杨梅和仙人蕉的信息描述。

    程序参考运行效果图如下:
    Java继承练习题_第1张图片

    思路分析:

(1)、根据杨梅和香蕉的共性,抽取父类水果(Fruits)
私有属性:水果的形状(shape)和口感(taste)
方法:
①.带参构造函数(参数为shape和taste)
②.创建无参无返回值得方法eat(描述内容为:水果可供人们食用!)
③.重写equals方法,比较两个对象是否相等(比较shape,taste)

(2)、子类Waxberry
私有属性:颜色(color)
方法:
①.调用父类的构造方法,完成属性赋值
②.创建不允许重写的face方法,描述为:杨梅:xx、xx,果味酸甜适中。
③.重写父类eat方法,描述为:杨梅酸甜适中,非常好吃!
④.重写toString方法,输出的表现形式不同(输出shape,color,taste)
⑤.要求Waxberry类不允许有子类

(3)、子类:Banana
私有属性:品种(variety)
方法:
①.带参构造方法为所有属性赋值
②.创建无参无返回值的advantage方法,描述为:xx果形xx,果肉香甜,可供生食。
③.重载要求(2)中的advantage方法(带参数color),描述为:xx颜色为xx

(4)、测试,运行效果参照效果图:
①.实例化2个父类对象,并传入两组相同的参数值
②.调用父类eat方法
③.测试重写equals方法,判断两个对象是否相等
④.实例化子类Waxberry对象,并传入相关参数值
⑤.调用子类face方法和重写父类eat方法后的eat方法
⑥.测试重写toString方法,输出子类对象的信息
⑦.实例化Banana类对象,并传入相关参数值
⑧.调用子类的advantage和它的重载方法

代码:
父类

package com.dodoke.fruits;

public class Fruits {
	// 成员属性:水果的形状(shape)和口感(taste)
	private String shape;

	private String taste;

	// 无参构造方法
	public Fruits() {

	}

	// 带参构造方法
	public Fruits(String shape, String taste) {
		this.setShape(shape);
		this.setTaste(taste);
	}

	// set/get
	public String getShape() {
		return shape;
	}

	public void setShape(String shape) {
		this.shape = shape;
	}

	public String getTaste() {
		return taste;
	}

	public void setTaste(String taste) {
		this.taste = taste;
	}

	// 创建无参无返回值得方法eat
	public void eat() {
		System.out.println("水果可供人们食用!");
	}

	// 重写equals方法
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Fruits other = (Fruits) obj;
		if (shape == null) {
			if (other.shape != null)
				return false;
		} else if (!shape.equals(other.shape))
			return false;
		if (taste == null) {
			if (other.taste != null)
				return false;
		} else if (!taste.equals(other.taste))
			return false;
		return true;
	}
}

子类杨梅

package com.dodoke.fruits;

//添加final属性阻止产生子类创建
public final class Waxberry extends Fruits {
	// 私有属性:颜色(color)
	private String color;

	// 无参构造方法
	public Waxberry() {

	}

	// 带参构造方法
	public Waxberry(String shape, String taste, String color) {
		super(shape, taste);
		this.setColor(color);
	}

	// get/set
	public String getColor() {
		return color;
	}

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

	// 创建不允许重写的face方法
	public final void face() {
		System.out.println("杨梅:" + this.getColor() + "、" + this.getShape() + "," + "果味" + this.getTaste() + "。");
	}

	// 重写父类eat方法
	@Override
	public void eat() {
		System.out.println("杨梅酸甜适中,非常好吃!");
	}

	// 重写toString方法
	@Override
	public String toString() {
		String str = "杨梅的信息:果实为" + this.getShape() + "、" + this.getColor() + "," + this.getTaste() + ",非常好吃!";
		return str;
	}
}

子类仙人蕉

package com.dodoke.fruits;

public class Banana extends Fruits {
	// 私有属性:品种(variety)
	private String variety;

	// 无参构造
	public Banana() {

	}

	// 带参构造
	public Banana(String shape, String taste, String variety) {
		super(shape, taste);
		this.setVariety(variety);
	}

	// get/set
	public String getVariety() {
		return variety;
	}

	public void setVariety(String variety) {
		this.variety = variety;
	}

	// 创建无参无返回值的advantage方法
	public void advantage() {
		System.out.println(this.getVariety() + "果形" + this.getShape() + ",果肉" + this.getTaste() + ",可供生食。");
	}

	// 重载advantage方法
	public void advantage(String color) {
		System.out.println(this.getVariety() + "颜色为" + color);
	}
}

测试类

package com.dodoke.test;

import com.dodoke.fruits.Banana;
import com.dodoke.fruits.Fruits;
import com.dodoke.fruits.Waxberry;

public class TestFruits {
	public static void main(String[] args) {
		// 实例化2个父类对象,并传入两组相同的参数值
		Fruits fru1 = new Fruits("圆形", "酸甜适中");
		Fruits fru2 = new Fruits("圆形", "酸甜适中");

		// 调用父类eat方法
		fru1.eat();

		// 测试重写equals方法,判断两个对象是否相等
		System.out.println("fru1和fru2的引用比较:" + fru1.equals(fru2));
		System.out.println("——————————————————————————————————————");

		// 实例化子类Waxberry对象,并传入相关参数值
		Waxberry wb = new Waxberry("圆形", "酸甜适中", "紫红色");

		// 调用子类face方法和重写父类eat方法后的eat方法
		wb.face();
		wb.eat();

		// 测试重写toString方法,输出子类对象的信息
		System.out.println(wb);
		System.out.println("——————————————————————————————————————");

		// 实例化Banana类对象,并传入相关参数值
		Banana bn = new Banana("短而稍圆", "香甜", "仙人蕉");

		// 调用子类的advantage和它的重载方法
		bn.advantage();
		bn.advantage("黄色");
	}
}

你可能感兴趣的:(java,java)