面向对象第四章课后作业

第5题 设计Bird 、Fish类,都继承自抽象类Animal,实现其抽象方法info(),并输出他们的信息

package com.homework.demo.test4_5;

/**
 * 动物类
 * @author suixin
 *
 */
public abstract class Animal {
	
	private int age; //年龄

	/**
	 * 无参构造
	 */
	public Animal() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * 带参构造
	 * @param age 年龄
	 */
	public Animal(int age) {
		super();
		this.age = age;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	/**
	 * 抽象输出信息方法
	 */
	public abstract void info(); 
}


package com.homework.demo.test4_5;

/**
 * 鸟类
 * @author suixin
 *
 */
public class Bird extends Animal {
	
	private String color = "红色"; //颜色

	public String getColor() {
		return color;
	}

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

	/**
	 * 无参构造方法
	 */
	public Bird() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 带参构造方法
	 * @param age 年龄
	 */
	public Bird(int age) {
		super(age);
		// TODO Auto-generated constructor stub
	}

	/**
	 *重写父类info()方法
	 */
	@Override
	public void info() {
		// TODO Auto-generated method stub
		System.out.println("我是一只"+this.color+"的鸟!\n"+"今年"+super.getAge()+"岁了!\n");
	}
	
}

package com.homework.demo.test4_5;

/**
 * 鱼类
 * @author suixin
 *
 */
public class Fish extends Animal {
	
	private int swight = 5; //体重

	public int getSwight() {
		return swight;
	}

	public void setSwight(int swight) {
		this.swight = swight;
	}

	/**
	 * 无参构造方法
	 */
	public Fish() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * 带参构造方法
	 * @param age 年龄
	 */
	public Fish(int age) {
		super(age);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 重写父类info()方法
	 */
	@Override
	public void info() {
		// TODO Auto-generated method stub
		System.out.println("我是一只"+this.swight+"斤重的鱼!\n"+"今年"+super.getAge()+"岁了!\n\n");
	}
	
	
	
}

package com.homework.demo.test4_5;

/**
 * 测试类
 * @author suixin
 *
 */
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Bird bird = new Bird(4); //传age的实参为4构造对象
		bird.info();
		
		Fish fish = new Fish(2); //传age的实参为4构造对象
		fish.info();
	}

}


第6题 兜兜家养了两只家禽:一只鸡和一只鸭,请用面向对象思想的封装和继承的特性进行描述。

package com.homework.demo.test4_6;

/**
 * 动物类
 * @author suixin
 *
 */
public abstract class Animal1 {
	
	private String name; //姓名
	
	private String strain; //品种

	public String getName() {
		return name;
	}

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

	public String getStrain() {
		return strain;
	}

	public void setStrain(String strain) {
		this.strain = strain;
	}

	/**
	 * 无参构造方法
	 */
	public Animal1() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * 有参构造方法
	 * @param name 姓名
	 * @param strain 品种
	 */
	public Animal1(String name, String strain) {
		super();
		this.name = name;
		this.strain = strain;
	}
	
	public abstract void print(); //抽象方法,输出信息
}

package com.homework.demo.test4_6;

/**
 * 鸡类
 * @author suixin
 *
 */
public class Chicken extends Animal1 {

	/**
	 * 无参构造方法
	 */
	public Chicken() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * 带参构造方法
	 * @param name 姓名
	 * @param strain 品种
	 */
	public Chicken(String name, String strain) {
		super(name, strain);
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 重写父类print()方法
	 */
	@Override
	public void print() {
		// TODO Auto-generated method stub
		System.out.println("我叫"+super.getName()+",是一只"+super.getStrain()+"\n我喜欢吃虫子!\n我会打鸣!\n");
	}
	
}

package com.homework.demo.test4_6;

/**
 * 鸭类
 * @author suixin
 *
 */
public class Duck extends Animal1 {

	/**
	 * 无参构造方法
	 */
	public Duck() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * 带参构造方法
	 * @param name 姓名
	 * @param strain 品种
	 */
	public Duck(String name, String strain) {
		super(name, strain);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 重写父类print()方法
	 */
	@Override
	public void print() {
		// TODO Auto-generated method stub
		System.out.println("我叫"+super.getName()+",是一只"+super.getStrain()+"\n我喜欢吃小鱼虾!\n我会游泳!");
	}
	
}


 package com.homework.demo.test4_6;

/**
 * 测试类
 * @author suixin
 *
 */
public class TestAnimal1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Chicken chicken = new Chicken("喔喔","芦花鸡");  //带参构造对象
		chicken.print(); //调用print方法
		
		Duck duck = new Duck("嘎嘎","斑嘴鸭"); //带参构造对象
		duck.print();	//调用print方法
	}

}


猜你喜欢

转载自blog.csdn.net/suixincaesar/article/details/80087342