java继承练习

选择题
1.A
2.D
3.C
4.
5.B
6.C
7.C
8.B
9.ABC
10.D
11.D
12.C
13.C
编程
1

//车类
public class Vehicle {
	String brand;
	String color;
	int wheel = 2;
	int seat = 1;

	public Vehicle() {}

	public Vehicle(String brand, String color) {
		this.brand = brand;
		this.color = color;
	}
	
	public Vehicle(String brand, String color, int wheel, int seat) {
		this.brand = brand;
		this.color = color;
		this.wheel = wheel;
		this.seat = seat;
	}

	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 void info() {
		System.out.println("父类信息测试:这是一辆" + this.getColor() + "的," + this.getBrand() + "的非机动车," + "有" + this.getWheel()
				+ "个轮子," + this.getSeat() + "个座椅");
	}
}

//自行车类
public class Bicycle extends Vehicle {
	public Bicycle() {
	}

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

	public Bicycle(String brand, String color, int wheel, int seat) {
		super(brand, color, wheel, seat);
	}

	@Override
	public void info() {
		System.out.println("自行车类信息测试:这是一辆" + this.getColor() + "的," + this.getBrand() + "的自行车");
	}

}

//电动车类
public class Electrocar extends Vehicle {
	String battery;

	public Electrocar() {
	}

	public Electrocar(String battery) {
		this.battery = battery;
	}

	public String getBattery() {
		return battery;
	}

	public void setBattery(String battery) {
		this.battery = battery;
	}

	@Override
	public void info() {
		System.out.println("电动车类信息测试:这是一辆使用" + this.getBattery() + "的电动车");
	}

	
}

//三轮车类
public class Ticycle extends Vehicle {
	public Ticycle() {
		this.setWheel(3);
	}
	
	public Ticycle(String brand, String color) {
		super(brand, color);
	}

	public Ticycle(String brand, String color, int wheel, int seat) {
		super(brand, color, wheel, seat);
	}

	@Override
	public void info() {
		System.out.println("三轮车类信息测试:三轮车是一款有" + wheel + "个轮子的非机动车");
	}
	
	
}

//测试类
public class Test {
	public static void main(String[] args) {
		Vehicle v = new Vehicle("天宇牌", "红颜色", 4, 2);
		v.info();
		Bicycle b = new Bicycle("捷安特牌", "黄颜色");
		b.info();
		Electrocar e = new Electrocar("飞鸽牌电池");
		e.info();
		Ticycle t = new Ticycle();
		t.info();
	}
}

//水果类
public class Fruits {
	private String shape;
	private String taste;
	
	public Fruits() {}

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

	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 void eat() {
		System.out.println("水果可供人们食用!");
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((shape == null) ? 0 : shape.hashCode());
		result = prime * result + ((taste == null) ? 0 : taste.hashCode());
		return result;
	}

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

//杨梅类
public class Waxberry extends Fruits {
	private String color;
	
	public Waxberry() {}


	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;
	}
	
	final public void face() {
		System.out.println("杨梅:" + this.getColor() + "、" + this.getShape() + ",果味" + this.getTaste() + "。");
	}


	@Override
	public void eat() {
		System.out.println("杨梅" + this.getTaste() + ",非常好吃!");
	}


	@Override
	public String toString() {
		return "杨梅的信息:果实为" + this.getShape() + "、" + this.getColor() + "," + this.getTaste() + ",非正好吃!";
	}	
}

//仙人蕉类
public class Banana extends Fruits {
	private String variety;
	
	public Banana() {}

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

	public String getVariety() {
		return variety;
	}

	public void setVariety(String variety) {
		this.variety = variety;
	}
	
	public void advantage() {
		System.out.println("仙人蕉果形" + this.getShape() + ","  + this.getTaste() + ",可供生食。");
	}
	
	public void advantage(String color) {
		System.out.println("仙人蕉颜色为" + color);
	}
	
}

//测试类
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));
		Waxberry w = new Waxberry("圆形", "酸甜适中", "紫红色");
		w.face();
		w.eat();
		System.out.println(w.toString());
		Banana b = new Banana("短而稍圆", "果肉香甜", " ");
		b.advantage();
		b.advantage("黄色");
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值