分别用抽象类和接口实现四个动物类(鱼类、鸟类、爬行类、昆虫类)的类别和天赋

1.将实验五第四题中的四个动物类(鱼类、鸟类、爬行类、昆虫类)的报告类别和天赋,分别用抽象类和接口来完成


abstract class AbstractClass {
	public int id;  //编号
	public StringBuffer name;//名字
	public String type; //类别:鸟类、昆虫类、爬行类和鱼类之一
	abstract public void showType();//在console上秀自己的类别
	abstract public void showTalent();//在console上秀自己的天赋特长
	public void showName()
	{
		System.out.println("My name is "+this.name+", i am from AbstractClass(抽象类)");
	}
}
class Fish extends AbstractClass
{	public Fish(int id,StringBuffer name) 
	{this.id=id;
	this.name=name;
	this.type="fish";
	}

	public void showType()//在console上秀自己的类别
	{
	System.out.println("My ID is "+id+" and I am just an animal, my type is fish");
	}
	public void showTalent()//在console上秀自己的天赋特长
	{
	System.out.println("I am talented at swiming");
	}
	
}
class Bird extends  AbstractClass
{	public Bird(int id,StringBuffer name) 
	{this.id=id;
	this.name=name;
	this.type="bird";
	}

	public void showType()//在console上秀自己的类别
	{
		System.out.println("My ID is "+id+" and I am just an animal, my type is bird");
	}
	public void showTalent()//在console上秀自己的天赋特长
	{
		System.out.println("I am talented at flying");
	}
}	
class Insect extends AbstractClass
{	public Insect(int id,StringBuffer name) 
	{this.id=id;
	this.name=name;
	this.type="insect";
	}

	public void showType()//在console上秀自己的类别
	{
		System.out.println("My ID is "+id+" and I am just an animal, my type is insect");
	}
	public void showTalent()//在console上秀自己的天赋特长
	{
		System.out.println("I am talented at working");
	}
	
}
class Reptile extends  AbstractClass
{	public Reptile(int id,StringBuffer name) 
	{this.id=id;
	this.name=name;
	this.type="reptile";
	}

	public void showType()//在console上秀自己的类别
	{
		System.out.println("My ID is "+id+" and I am just an animal, my type is reptile");
	}
	public void showTalent()//在console上秀自己的天赋特长
	{
		System.out.println("I am talented at crawling");
	}
}

 interface Animal2
{public void showType();
public void showTalent();
public void showName();
}
 class Fish2 implements Animal2
 {	public int id;  //编号
	public StringBuffer name;//名字
	public String type; //类别:鸟类、昆虫类、爬行类和鱼类之一
	 public Fish2(int id,StringBuffer name) 
 	{this.id=id;
 	this.name=name;
 	this.type="fish";
 	}

 	public void showType()//在console上秀自己的类别
 	{
 	System.out.println("My ID is "+id+" and I am just an animal, my type is fish");
 	}
 	public void showTalent()//在console上秀自己的天赋特长
 	{
 	System.out.println("I am talented at swiming");
 	}
 	public void showName()
	{
		System.out.println("My name is "+this.name+", i am from Interface(接口)");
	}
 	
 }
 class Bird2 implements Animal2
 {	public int id;  //编号
	public StringBuffer name;//名字
	public String type; //类别:鸟类、昆虫类、爬行类和鱼类之一
	 public Bird2(int id,StringBuffer name) 
 	{this.id=id;
 	this.name=name;
 	this.type="fish";
 	}

 	public void showType()//在console上秀自己的类别
 	{
 		System.out.println("My ID is "+id+" and I am just an animal, my type is bird");
 	}
 	public void showTalent()//在console上秀自己的天赋特长
 	{
 		System.out.println("I am talented at flying");
 	}
	public void showName()
	{
		System.out.println("My name is "+this.name+", i am from Interface(接口)");
	}
 	
 }	
 class Insect2 implements Animal2
 {	public int id;  //编号
	public StringBuffer name;//名字
	public String type; //类别:鸟类、昆虫类、爬行类和鱼类之一
	 public Insect2(int id,StringBuffer name) 
 	{this.id=id;
 	this.name=name;
 	this.type="fish";
 	}

 	public void showType()//在console上秀自己的类别
 	{
 		System.out.println("My ID is "+id+" and I am just an animal, my type is insect");
 	}
 	public void showTalent()//在console上秀自己的天赋特长
 	{
 		System.out.println("I am talented at working");
 	}
	public void showName()
	{
		System.out.println("My name is "+this.name+", i am from Interface(接口)");
	}
 	
 }
 class Reptile2 implements Animal2
 {	public int id;  //编号
	public StringBuffer name;//名字
	public String type; //类别:鸟类、昆虫类、爬行类和鱼类之一
	 public Reptile2(int id,StringBuffer name) 
 	{this.id=id;
 	this.name=name;
 	this.type="fish";
 	}

 	public void showType()//在console上秀自己的类别
 	{
 		System.out.println("My ID is "+id+" and I am just an animal, my type is reptile");
 	}
 	public void showTalent()//在console上秀自己的天赋特长
 	{
 		System.out.println("I am talented at crawling");
 	}
	public void showName()
	{
		System.out.println("My name is "+this.name+", i am from Interface(接口)");
	}
 }
public class UseOfAnimal {
	public static void main(String[] args) {
		AbstractClass[] animalArmy = new AbstractClass[100]; 
		for(int i = 0; i<100; i++)
		{
			int type = (int)( Math.random()*4 );
			StringBuffer name = new StringBuffer();
			for(int j = 0; j<4; j++)
			{
				int word = (int)( Math.random()*26 );
				name.append((char)(word + 'a') );
			}
			switch (type)
			{
			case 0:
				animalArmy[i] = new Bird(i, name);
				break;
			case 1:
				animalArmy[i] = new Fish(i, name);
				break;
			case 2:
				animalArmy[i] = new Insect(i, name);
				break;
			case 3:
				animalArmy[i] = new Reptile(i,name);
			}
			animalArmy[i].showName();
			animalArmy[i].showType();
			animalArmy[i].showTalent();
			System.out.println();				
}
		 Animal2[] animalArmy2 = new  Animal2[100]; 
		for(int i = 0; i<100; i++)
		{
			int type = (int)( Math.random()*4 );
			StringBuffer name = new StringBuffer();
			for(int j = 0; j<4; j++)
			{
				int word = (int)( Math.random()*26 );
				name.append( (char)(word + 'a') );
			}
			switch (type)
			{
			case 0:
				animalArmy2[i] = new Bird2(i, name);
				break;
			case 1:
				animalArmy2[i] = new Fish2(i, name);
				break;
			case 2:
				animalArmy2[i] = new Insect2(i, name);
				break;
			case 3:
				animalArmy2[i] = new Reptile2(i,name);
			}
			animalArmy2[i].showName();
			animalArmy2[i].showType();
			animalArmy2[i].showTalent();
			System.out.println();				
}
}
}

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pistachiout

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值