题目:
有来自4类(鸟类、昆虫类、爬行类和鱼类)的100个动物聚在一起开会,商议和另一个动物部落打仗事宜,会议要求每个动物都要报告自己所属的动物类别和自己的天赋,以便选拔人才、组织兵力迎战。
设计:用Animal作为基类,鸟类、昆虫类、爬行类和鱼类各作为Animal的子类设计类层次结构,设计时运用继承、重写并设计多态机制,同时对每个子类至少要添加一个其描述的动物特有的行为和一个特有的属性,以更准确地描述子类对象。
使用:用循环随机生成这100个动物,要对每个动物进行编号和随机命名,用循环让每个参会的动物报告自己的类别和天赋。
package ex;
import java.util.Random;
public class Test {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
String []name = new String [100];
String []id = new String [100];
randomName(name);
randomId(id,5);
Random r = new Random();
String species = "";
for(int i = 0;i<100;i++) {
species = randomSpecies(r.nextInt(4));
Animal animal = (Animal)Class.forName("ex."+species).newInstance(); //想用用看java反射
animal.setAttri(id[i],name[i]);
animal.showType();
animal.showTalent();
}
}
public static String randomSpecies(int i) { //随机动物类型
switch(i) {
case 0:
return "Bird";
case 1:
return "Insect";
case 2:
return "Reptile";
case 3:
return "Fish";
default:
return "";
}
}
public static void randomName(String[] str) { // 随机名字
char []temp = new char[52];
int index = 0;
for(int i = 97;i<123;i++) {
temp[index++] = (char)i;
}
for(int i = 65;i<91;i++) {
temp[index++] = (char)i;
}
Random r = new Random(); //随便取字母
int nameLen = 0;
@SuppressWarnings("unused")
String name = "";
for(int i = 0;i < str.length;i++) {
nameLen = 3 + r.nextInt(4); //名字3-6位
for(int j = 0;j<nameLen;j++) {
index = r.nextInt(52);
name += temp[index];
}
str[i] = name;
name = "";
}
}
public static void randomId(String[] str,int idLen) {
char []temp = new char[10];
int index = 0;
for(int i = 48;i<58;i++) {
temp[index++] = (char)i;
}
Random r = new Random(); //随便取数字
@SuppressWarnings("unused")
String id = "";
for(int i = 0;i < str.length;i++) {
for(int j = 0;j<idLen;j++) {
index = r.nextInt(10);
id += temp[index];
}
str[i] = id;
id = "";
}
}
}
abstract class Animal{ //Animal类
protected String id; //编号
protected String name;//名字
public String type;
public void showType() {//在console上秀自己的类别
System.out.println("I'am a " + type + ",my id is " + id + " and my name is " + name);
}
public abstract void showTalent();//在console上秀自己的天赋特长
public void setAttri(String id,String name) { //反射的newInstance方法只能调用无参构造函数,就弄个set吧
this.id = id;
this.name = name;
}
}
class Bird extends Animal{ //鸟类
public Bird() {
type = "Bird";
}
public Bird(String id,String name) {
type = "Bird";
this.id = id;
this.name = name;
}
public void showTalent() {
System.out.println("I can fly!");
}
}
class Insect extends Animal{ //昆虫类
public Insect() {
type = "Insect";
}
public Insect(String id,String name) {
type = "Insect";
this.id = id;
this.name = name;
}
public void showTalent() {
System.out.println("I'm very small!");
}
}
class Reptile extends Animal{ //爬行动物类
public Reptile() {
type = "Reptile";
}
public Reptile(String id,String name) {
type = "Reptile";
this.id = id;
this.name = name;
}
public void showTalent() {
System.out.println("I'm very strong!");
}
}
class Fish extends Animal{ //鱼类
public Fish() {
type = "Fish";
}
public Fish(String id,String name) {
type = "Fish";
this.id = id;
this.name = name;
}
public void showTalent() {
System.out.println("I can swim!");
}
}
//自己写着玩儿的,像上面的反射只是为了想用用反射就写着玩儿的
//老师布置的是装入动物数组,由于animal被我定义成了抽象类就弄不了animal数组了,所以我直接把题目改了…..
//欢迎指正或者提意见