面向对象程序设计——第四章继承,课后作业

1.设计Bird类和Fish类,都继承自抽象类Animal,实现其抽象方法info(),并输出他们的信息。

/**
 * 抽象父类
 * @author Lenovo
 *
 */
abstract class Animals {
	private int age;
	/**
	 * 无参构造方法
	 */
	public Animals() {
		
	}
	/**
	 * 有参构造方法
	 */
	public Animals(int age) {
		this.age = age;
	}
	public int getAge() {
		return age;
	}
	/**
	 * 抽象方法
	 */
	public abstract void info();
}
/**
 * 鸟类
 * @author Lenovo
 *
 */
class Bird extends Animals {
	private String colour;
	
	public Bird(int age,String colour) {
		super(age); // 调用父类的有参方法
		this.colour = colour;
	}
	public String getColour() {
		return colour;
	}
	public void setColour(String colour) {
		this.colour = colour;
	}
	/**
	 * 输出信息
	 */
	public void info() {
		System.out.println("我是一只"+this.colour+"的鸟\n今年"+this. getAge()+"岁了!");
	}
}
/**
 * 鱼类
 * @author Lenovo
 *
 */
class Fish extends Animals {
	private int weight;
	
	public Fish(int age,int weight) {
		super(age); // 调用父类的有参方法
		this.weight = weight;
	}
	public int getWeight() {
		return weight;
	}
	public void setWeight(int weight) {
		this.weight = weight;
	}
	/**
	 * 输出信息
	 */
	public void info() {
		System.out.println("我是一只"+this.weight+"斤重的鱼\n今年"+this. getAge()+"岁了!");
	}
}
public class Animal {
	public static void main(String[] args) {
		Bird animol = new Bird(4,"红色");
		animol.info();
		Fish animol1 = new Fish(2,5);
		animol1.info();
	}
}

2.兜兜家养了两只家禽,一只鸡和一只鸭。请用面向对象的封装,继承的特性进行描述。

/**
 * 家禽父类
 * @author Lenovo
 *
 */
abstract class Poultrys {
	private String name; //姓名
	/**
	 * 无参构造方法
	 */
	public Poultrys() {
		
	}
	/**
	 * 有参构造方法
	 */
	public Poultrys(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public abstract void info();
}
/**
 * 鸡类
 * @author Lenovo
 *
 */
class Chook extends Poultrys {
	private String bug; //爱吃虫子
	private String crow; //会打鸣
	/**
	 * 无参构造方法
	 */
	public Chook() {
		
	}
	/**
	 * 有参方法
	 */
	public Chook(String bug,String crow,String name) {
		super(name);
		this.bug = bug;
		this.crow = crow;
	}
	public String getBug() {
		return bug;
	}
	public void sexBug(String bug) {
		this.bug = bug;
	}
	public String getCrow() {
		return crow;
	}
	/**
	 * 输出信息
	 */
	public  void info() {
		System.out.println("我叫"+this.getName()+",是一只芦花鸡!\n我喜欢吃"+this.getBug()+
				"\n我会"+this.getCrow());
	}
	
}
/**
 * 鸭类
 * @author Lenovo
 *
 */
class Duck extends Poultrys {
	private String Shrimp;
	private String swimming;
	/**
	 * 无参构造方法
	 */
	public Duck() {
		
	}
	/**
	 * 有参方法
	 */
	public Duck(String Shrimp,String swimming,String name) {
		super(name);
		this.Shrimp = Shrimp;
		this.swimming = swimming;
	}
	public String getShrimp() {
		return Shrimp;
	}
	public void sexShrimp(String Shrimp) {
		this.Shrimp = Shrimp;
	}
	public String getSwimming() {
		return swimming;
	}
	public void setSwimming(String swimming){
		this.swimming = swimming;
	}
	/**
	 * 输出信息
	 */
	public  void info() {
		System.out.println("\n我叫"+this.getName()+",是一只石板鸭!\n我喜欢吃"+this.getShrimp() +
				"\n我会"+this.getSwimming());
	}
	
}
class Poultry {
	public static void main(String[] args) {
		Poultrys Poultrys = new Chook("虫子","打鸣","喔喔");
		Poultrys.info();
		Poultrys Poultrys1 = new Duck("小鱼虾","游泳","嘎嘎");
		Poultrys1.info();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41882685/article/details/80089118