Java继承练习

选择

// 1、A
// 2、D
// 3、C
// 4、
// 5、B
// 6、C
// 7、C
// 8、B
// 9、ABD
// 10、D
// 11、D
// 12、C

编程

  1. 编程练习:某公司要开发“XX车行管理系统”,请使用面向对象的思想,设计自定义类描述自行 车、电动车和三轮车。
    任务分析; 第一步:分析自行车、电动车和三轮车的共性:

    1. 都是非机动车,具有非机动车的基本特征 2. 都有运行的方法
      第二步:根据共性,定义非机动车 属性:品牌、颜色、轮子(默认2个)、座椅(默认 1个)

    方法:

    1. 编写无参构造方法、双参构造方法和四参构造方法,其中,在双参构造方法中,完成对品牌和 颜色的赋值;在四参构造方法中,完成对所有属性的赋值
    2. 编写运行的方法,描述内容为:这是一辆颜色的,牌的非机动车,有个轮子,有个座椅 的非机动车。其中**的数据由属性提供 第三步:定义自行车、电动车和三轮车分别继承自行车类,要求:

    自行车类:

    在构造方法中调用父类多参构造,完成属性赋值 2. 重写运行方法,描述内容为:这是一辆**颜色的,**牌的自行车。其中**的数据由属性提供
    

    电动车:

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

    三轮车:

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

/**
 * 非机动车类
 * @author Clearlove7
 *
 */
public class NonMotorVehicle {
	
	private String brand;  // 品牌
	
	private String color; // 颜色
	
	private int wheel = 2;  // 车轮(数量默认为2)
	
	private int seat = 1; // 座椅(数量默认为1)

	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 getSeat() {
		return seat;
	}

	public void setSeat(int seat) {
		this.seat = seat;
	}

	public NonMotorVehicle(String brand, String color, int wheel, int seat) {
		super();
		this.setBrand(brand);
		this.setColor(color);
		this.setWheel(wheel);
		this.setSeat(seat);
	}

	public NonMotorVehicle(String brand, String color) {
		super();
		this.setBrand(brand);
		this.setColor(color);
	}

	public NonMotorVehicle() {
		super();
	}
	
	
	public String  print() {
		String str = "父类信息测试:这是一辆" + this.getColor() + "的," + this.getBrand() + "的非机动车,有" + this.getWheel() + "个轮子,有" + this.getSeat() + "个座椅";
				
		return str;
		
	}
	
	
	
}

package java0417;

/**
 * 自行车类
 * @author Clearlove7
 *
 */
public class Bicycle extends NonMotorVehicle {

	public Bicycle(String brand, String color) {
        super(brand, color);

    }
	
	@Override
	public String print() {
		String str = "自行车类信息测试:这是一辆" + this.getColor() + "的," + this.getBrand() + "的自行车";
		
		return str;
	}
	
}

package java0417;

/**
 * 电动车类
 * @author Clearlove7
 *
 */
public class ElectricBicycle extends NonMotorVehicle {

	private String batteryBrand; // 电池品牌

	public String getBatteryBrand() {
		return batteryBrand;
	}

	public void setBatteryBrand(String batteryBrand) {
		this.batteryBrand = batteryBrand;
	}

	public ElectricBicycle(String batteryBrand) {
		super();
		this.batteryBrand = batteryBrand;
	}

	public ElectricBicycle() {
		super();
	}
	
	@Override
	public String print() {
		String str = "电动车类信息测试:这是一辆使用" + this.getBatteryBrand() + "电池的电动车";
		
		return str;
	}
	

}

package java0417;
/**
 * 三轮车类
 * @author Clearlove7
 *
 */
public class Tricycle extends NonMotorVehicle {

	public Tricycle() {
		super();
		this.setWheel(3);
	}
	
	@Override
	public String print() {
		String str = "三轮车类信息测试:三轮车是一款有" +this.getWheel() + "个轮子的非机动车";
		
		return str;
	}
}

package java0417;

/**
 * 测试类
 * @author Clearlove7
 *
 */
public class TestV {
	
	public static void main(String[] args) {
		NonMotorVehicle no = new NonMotorVehicle("红颜色", "天宇牌", 4, 2);
		System.out.println(no.print());
		//父类信息测试:这是一辆天宇牌的,红颜色的非机动车,有4个轮子,有2个座椅
		
		Bicycle bi = new Bicycle("捷安特", "黄色");
		System.out.println(bi.print());
		//自行车类信息测试:这是一辆黄色的,捷安特的自行车
		
		ElectricBicycle el = new ElectricBicycle("飞鸽牌");
		System.out.println(el.print());
		//电动车类信息测试:这是一辆使用飞鸽牌电池的电动车
		
		Tricycle ti = new Tricycle();
		System.out.println(ti.print());
		//三轮车类信息测试:三轮车是一款有3个轮子的非机动车

	}
}

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

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

方法:
创建带参(name、age、sex为参数)构造方法 重写 toString 方法,输出信息格式为:姓名:** 年龄:** 性别:**(其中,**为对象对应 属性值)

创建测试类,在测试方法中,实例化 Person对 象,并传入三个属性值。然后,分别通过直接 打印Person对象以及利用重写的 toString 方法,打印输出2行对象信息

package java0417;

public class Person {

	private String name; // 姓名
	
	private int age; // 年龄
	
	private String sex; // 性别

	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;
	}

	public Person(String name, int age, String sex) {
		super();
		this.setName(name);
		this.setAge(age);
		this.setSex(sex);
	}

	public Person() {
		super();
	}
	
	public String toString() {
		String str = "姓名:" + this.getName() + " " + "年龄:" + this.getAge() + " " + "性别:" + this.getSex() ;
		
		return str;
	}
	
	
package java0417;

public class TestP {
	public static void main(String[] args) {
		Person pe1 = new Person("黎明", 18, "男");
		pe1.toString();
		System.out.println(pe1.toString());
		//姓名:黎明 年龄:18 性别:男
		
		Person pe2 = new Person("黎明", 18, "男");
		pe2.toString();
		System.out.println(pe2.toString());
		//姓名:黎明 年龄:18 性别:男
	}
}

  1. 请使用面向对象的思想,实现杨梅和仙人蕉的信息描述。
    程序参考运行效果图如下:
    在这里插入图片描述
    思路分析:
    1、根据杨梅和香蕉的共性,抽取父类水果(Fruits)

    私有属性:水果的形状(shape)和口感(taste)

    方法:
    带参构造函数(参数为shape和taste)
    创建无参无返回值得方法eat(描述内容为:水果可供人们食用!)
    重写equals方法,比较两个对象是否相等(比较shape,taste)

    2、子类Waxberry

    私有属性:颜色(color)

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

    3、子类:Banana

    私有属性:品种(variety)

    方法:
    带参构造方法为所有属性赋值
    创建无参无返回值的advantage方法,描述为:果形,果肉香甜,可供生食。
    重载要求(2)中的advantage方法(带参数color),描述为:颜色为

    4、测试,运行效果参照效果图

    实例化2个父类对象,并传入两组相同的参数值
    调用父类eat方法
    测试重写equals方法,判断两个对象是否相等
    实例化子类Wacberry对象,并传入相关参数值
    调用子类face方法和重写父类eat方法后的eat方法
    测试重写toString方法,输出子类对象的信息
    实例化Banana类对象,并传入相关参数值
    调用子类的advantage和它的重载方法

package lianxi;

/**
 * 父类-水果类
 * @author Clearlove7
 *
 */
public class Fruits {
	private String shape; // 形状
	
	private String taste; // 口感
	
	public void eat() {
		System.out.println("水果可供人们食用");
	}

	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;
	}

	public Fruits(String shape, String taste) {
		super();
		this.shape = shape;
		this.taste = taste;
	}

	public Fruits() {
		super();
	}
	
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		} else {
			Fruits fru = (Fruits)obj;
			if(this.getShape().equals(fru.getShape()) && this.getTaste().equals(fru.getTaste())) {
				return true;
			} else {
				return false;
			}
		}
		
	}
}
package lianxi;

public final class Waxberry extends Fruits {
	private String color;

	public final void face() {
		System.out.println("杨梅:" + this.color + "、" + this.getShape() + ",果味酸甜适中");
	}

	@Override
	public void eat() {
		System.out.println("杨梅酸甜适中,非常好吃");
	}

	public Waxberry(String shape, String taste, String color) {
		super(shape, taste);
		this.color = color;
	}

	public String getColor() {
		return color;
	}

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

	@Override
	public String toString() {
		return "杨梅的信息:" + this.getShape() + this.color + this.getTaste() + "];";
	}
}

package lianxi;

public class Banana extends Fruits{
	private String variety; // 品种

	public String getVariety() {
		return variety;
	}

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

	public Banana(String shape, String taste, String variety) {
		super(shape, taste);
		this.variety = variety;
	} 
	
	
	public void adventage() {
		System.out.println(this.getVariety() + "果形" + this.getShape() + "果肉香甜,可供生食");
	}
	
	public void adventage(String color) {
		System.out.println(this.getVariety()  + "颜色为" + color );
	}
	
}

package lianxi;

public class Test {
	public static void main(String[] args) {
		Fruits fru1 = new Fruits("圆形", "甜");
		Fruits fru2 = new Fruits("圆形", "甜");
		
		fru1.eat();
		System.out.println("fru1和fru2的引用比较:" + fru1.equals(fru2));
		
		System.out.println("————————————————————————————————————————————————————————————");
		
		Waxberry wb = new Waxberry("紫红色", "酸甜适中非常好吃", "圆形");
		wb.face();
		wb.eat();
		System.out.println(wb.toString());
		
		Banana ba = new Banana("短而稍圆", "" , "仙人蕉");
		ba.adventage();
		ba.adventage("黄色");
	}
}

好的,以下是一道 Java 继承练习题: 假设有一个父类 `Animal` 和两个子类 `Dog` 和 `Cat`。`Animal` 类包含一个 `name` 成员变量和一个 `eat` 方法,`Dog` 和 `Cat` 类都继承了 `Animal` 类,并且都有一个 `sound` 方法。现在请你完成以下练习: 1. 在 `Animal` 类中,添加一个 `sleep` 方法,并在 `Dog` 和 `Cat` 类中覆盖(重写)该方法,使得 `Dog` 和 `Cat` 的 `sleep` 方法输出不同的内容。 2. 在 `Dog` 和 `Cat` 类中,实现各自的 `sound` 方法,使得 `Dog` 的 `sound` 方法输出 `"汪汪"`,`Cat` 的 `sound` 方法输出 `"喵喵"`。 3. 编写一个名为 `Main` 的测试类,并在其中创建一个 `Dog` 对象和一个 `Cat` 对象,分别调用它们的 `eat`、`sleep` 和 `sound` 方法。 你可以参考以下代码: ```java public class Animal { private String name; public Animal(String name) { this.name = name; } public void eat() { System.out.println(name + " is eating"); } public void sleep() { System.out.println(name + " is sleeping"); } } public class Dog extends Animal { public Dog(String name) { super(name); } public void sleep() { System.out.println(getName() + " is sleeping soundly"); } public void sound() { System.out.println("汪汪"); } } public class Cat extends Animal { public Cat(String name) { super(name); } public void sleep() { System.out.println(getName() + " is sleeping quietly"); } public void sound() { System.out.println("喵喵"); } } public class Main { public static void main(String[] args) { Animal dog = new Dog("旺财"); Animal cat = new Cat("小花"); dog.eat(); dog.sleep(); ((Dog) dog).sound(); cat.eat(); cat.sleep(); ((Cat) cat).sound(); } } ``` 希望这个练习题能够帮助你理解 Java 继承的相关知识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值