继承作业3

3. 编写Java应用程序,定义Animal类,此类中有动物的属性:名称 name,腿的数量legs,统计动物的数量 count;方法:设置动物腿数量的方法 void setLegs(),获得腿数量的方法 getLegs(),设置动物名称的方法 setKind(),获得动物名称的方法 getKind(),获得动物数量的方法 getCount()。定义Fish类,是Animal类的子类,统计鱼的数量 count,获得鱼数量的方法 getCount()。定义Tiger类,是Animal类的子类,统计老虎的数量 count,获得老虎数量的方法 getCount()。定义SouthEastTiger类,是Tiger类的子类,统计老虎的数量 count,获得老虎数量的方法 getCount()。

package zuoye10;

public class Animal {
	
	private String name;//名称
	private int legs;//退的数量
	public int count;//动物数量
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getLegs() {
		return legs;
	}
	public void setLegs(int legs) {
		this.legs = legs;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}//动物数量
	public Animal() {
		super();
	}
	
	

}

  

package zuoye10;

public class Fish extends Animal {
	

	public int getCount() {
		return Count();
	}
	public void setCount(int count) {
		this.count = count;
	}
}

  

package zuoye10;

public class Tiger extends Animal {

	public int getCount() {
		return Count();
	}
	public void setCount(int count) {
		this.count = count;
	}

}

  

package zuoye10;
//东南虎
public class SouthEastTiger extends Tiger {
	

	public int getCount() {
		return Count();
	}
	public void setCount(int count) {
		this.count = count;
	}

}

  

package zuoye10;

public class Ceshi_animal {


	public static void main(String[] args)
	{
		Fish f=new Fish();
		f.setName("鱼");
		f.setCount(20);
		f.setLegs(0);
		System.out.println("动物的类型是:"+f.getName()+"数量="+f.getCount()+"腿的数量="+f.getLegs());
		
		Tiger t=new Tiger();
		t.setName("老虎");
		t.setCount(20);
		t.setLegs(80);
		System.out.println("动物的类型是:"+t.getName()+"数量="+t.getCount()+"腿的数量="+t.getLegs());
	}

}

  

原文地址:https://www.cnblogs.com/liuyanzeng/p/5897569.html