java继承

1.A
2.D
3.C
4.
5.D
6.C
7.C
8.B
9.AB
10.D
11.D
12.C
13.C

//车类
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();
	}
}

2

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

	//带参构造方法(name、age、sex为参数) 
	public Person(String name, int age, String sex) {
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	 //通过封装实现对属性的get/set方法设定 
	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() {
		return "姓名:" + this.getName() + "  年龄:" + this.getAge() + "  性别:" + this.getSex();
	}

}
//测试类
public class Test {
	public static void main(String[] args) {
		Person p = new Person("李明", 18, "男");
		System.out.println(p);
		System.out.println(p.toString());
	}
}

3

//水果类
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("黄色");
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值