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

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

来源:萌宠菠菠乐园 时间:2024-11-15 14:08

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(); } } }

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241

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

相关知识

宠物包罗万象,按动物学分为哺乳类、爬行类、鸟类、鱼类和昆虫类
分别使用继承、多态、抽象类和接口方式实现动物类需求
鸟类哺乳动物两栖动物,爬行动物,鱼类昆虫类他们的分类依据。
Java分别采用继承、多态、抽象类、接口实现猫和狗的入门案例
【另类宠物(宠物系列)】哺乳类、爬行类、两栖类、鱼类、甲壳类、昆虫类、节肢类、软体动物.....
宠物的种类有哪些?有哺乳类、鸟类、爬行类等等
接口版编写猫和狗
java对抽象类的理解与实践
Java案例——猫与狗(接口版)
宠物包括哪个

网址: 分别用抽象类和接口实现四个动物类(鱼类、鸟类、爬行类、昆虫类)的类别和天赋 https://www.mcbbbk.com/newsview563242.html

所属分类:萌宠日常
上一篇: 宠物爬行类图片
下一篇: 上海自如心舍3.0升级上线,宠物

推荐分享