分别用抽象类和接口实现四个动物类(鱼类鸟类爬行类昆虫类)的类别和天赋
Posted Pistachiout
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分别用抽象类和接口实现四个动物类(鱼类鸟类爬行类昆虫类)的类别和天赋相关的知识,希望对你有一定的参考价值。
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();
}
}
}
以上是关于分别用抽象类和接口实现四个动物类(鱼类鸟类爬行类昆虫类)的类别和天赋的主要内容,如果未能解决你的问题,请参考以下文章