第4章课后作业

 
  
5.设计Bird、Fish类,都继承自抽象类Animal
•实现其抽象方法info(),
public abstract class Dongwu{

abstract void info();
}
public class Bird extends Animal {

public static void main(String[] args) {
// TODO Auto-generated method stub
Bird b=new Bird();
b.info();
}

@Override
void info() {
// TODO Auto-generated method stub
System.out.println("我是一只红色的鸟!"+"\n"+"我今年四岁了!");
}

}






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

/**
 * 家禽类
 */
public abstract class Shiwu {
private String name;//名字
private String likeFood;//喜爱食物
public Poultry(String name,String likeFood) {
super();
this.name = name;
this.likeFood = likeFood;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLikeFood() {
return likeFood;
}
public void setLikeFood(String likeFood) {
this.likeFood = likeFood;
}
public abstract void show();
}
/**
 * 鸡类
 */
public class Chicken extends Poultry {
private String strain;  //品种

public Chicken(String name, String likeFood, String strain) {
super(name, likeFood);
this.strain = strain;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
public void crow() {
System.out.println("我会打鸣!");
}
public void show() {
System.out.println("我叫"+this.getName()+",是一只"+this.strain+"!\n我喜欢吃"+this.getLikeFood()+"!");
this.crow();
}
}
\**
 * 鸭类
 */
public class Duck extends Poultry {
private String type;//类型

public Duck(String name, String likeFood, String strain) {
super(name, likeFood);
this.type = strain;
}

public String getStrain() {
return type;
}

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

public void crow() {
System.out.println("我会游泳!");
}

public void show() {
System.out.println("我叫" + this.getName() + ",是一只" + this.type + "!\n我喜欢吃" + this.getLikeFood()+"!");
this.crow();
}

}
/**
 * 测试类
 */
public class PoultryTest {
public static void main(String[] args) {
Poultry poultry = new Chicken("喔喔", "虫子", "芦花鸡");
poultry.show();
System.out.println("");
Poultry poultry1 = new Duck("嘎嘎", "小鱼虾", "斑嘴鸭");
poultry1.show();
}
}


猜你喜欢

转载自blog.csdn.net/liyiming85/article/details/80087177