Java题目1

博客内容涉及设计一个Java类层次结构,以Animal为基类,包括鸟类、昆虫类、爬行类和鱼类四个子类,每个子类都有特定的行为和属性。通过继承和重写实现多态,并利用循环生成100个随机动物对象,报告各自类别和天赋。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

有来自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数组了,所以我直接把题目改了…..
//欢迎指正或者提意见

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值