面向对象测试-输出和修改动物信息

功能需求:1、输出各种动物叫声  2、输出各种动物腿的条数  3、实现修改数据功能

 1 package animal;
 2 
 3 /**
 4  * 动物类
 5  * @author 
 6  *
 7  */
 8 public abstract class Animal {
 9     private String name;  //名字
10     
11     public Animal() {
12     }
13     
14     public Animal(String name) {
15         this.name = name;
16     }
17 
18     public String getName() {
19         return name;
20     }
21     public void setName(String name) {
22         this.name = name;
23     }
24     
25     public abstract String shout();
26     
27 }
 1 package animal;
 2 
 3 /**
 4  * 陆生动物接口
 5  * @author 
 6  *
 7  */
 8 public interface Lusheng {
 9     int getLegs();
10     void setLegs(int legs) throws Exception;
11 }
 1 package animal;
 2 
 3 /**
 4  * 猫类
 5  * @author 
 6  *
 7  */
 8 public class Cat extends Animal implements Lusheng{
 9     private int legs;  //腿数
10     
11     public Cat() {
12     }
13 
14     public Cat(String name,int legs) {
15         super(name);
16         this.legs=legs;
17     }
18 
19     public int getLegs() {
20         return legs;
21     }
22 
23     public void setLegs(int legs) throws Exception {
24         this.legs = legs;
25         if(legs!=4) {
26             throw new Exception("猫只能有4条腿!");
27         }
28     }
29 
30     public String shout() {
31         return "喵喵喵";
32     }
33     
34 }
 1 package animal;
 2 
 3 /**
 4  * 海豚类
 5  * @author 
 6  *
 7  */
 8 public class Delph extends Animal {
 9     public Delph() {
10     }
11 
12     public Delph(String name) {
13         super(name);
14     }
15 
16     public String shout() {
17         return "海豚音";
18     }
19     
20 }
 1 package animal;
 2 
 3 import java.util.EmptyStackException;
 4 
 5 /**
 6  * 鸭子类
 7  * @author 
 8  *
 9  */
10 public class Duck extends Animal implements Lusheng{
11     private int legs;  //腿数
12 
13     public Duck() {
14     }
15     
16     public Duck(String name,int legs) {
17         super(name);
18         this.legs=legs;
19     }
20 
21     public int getLegs() {
22         return legs;
23     }
24 
25     public void setLegs(int legs) throws Exception {
26         this.legs = legs;
27         if(legs!=2) {
28             throw new Exception("鸭的腿只能是2条!");
29         }
30     }
31 
32     public String shout() {
33         return "嘎嘎嘎";
34     }
35     
36     
37 }
 1 package animal;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  * 动物管理类
 7  * @author 
 8  *
 9  */
10 public class AnimalMgr {
11     Animal[] animals = new Animal[3];
12 
13     //动物数组初始化
14     public void init() {
15         animals[0] = new Cat("加菲猫", 4);
16         animals[1] = new Duck("唐老鸭", 2);
17         animals[2] = new Delph("海豚琪琪");
18     }
19     
20     //输出动物信息
21     public void showInfo() {
22         System.out.println("动物名字\t退的条数\t动物叫");
23         for (Animal animal : animals) {
24             System.out.println(animal.getName() + "\t" + (animal instanceof Lusheng ? ((Lusheng) animal).getLegs() : "")
25                     + "\t" + animal.shout());
26         }
27     }
28 
29     //修改动物名称和腿数
30     public void modify() {
31         Scanner input = new Scanner(System.in);
32         do {
33             System.out.println("是否要继续修改数据:按0进行修改操作,其它任意数字键退出");
34             int choose = input.nextInt();
35             if (choose == 0) {
36                 try {
37                     for (int i = 0; i < animals.length; i++) {
38                         //如果动物对象是猫,则修改猫的信息
39                         if (animals[i] instanceof Cat) {
40                             Cat cat = (Cat) animals[i];
41                             System.out.println("请输入猫的名称:");
42                             String catName = input.next();
43                             cat.setName(catName);
44                             System.out.println("请输入猫腿的条数:");
45                             int catLegs = input.nextInt();
46                             cat.setLegs(catLegs);
47                         //如果动物对象是鸭子,则修改鸭子的信息
48                         } else if (animals[i] instanceof Duck) {
49                             Duck duck = (Duck) animals[i];
50                             System.out.println("请输入鸭子的名称:");
51                             String duckName = input.next();
52                             duck.setName(duckName);
53                             System.out.println("请输入鸭子腿的条数:");
54                             int duckLegs = input.nextInt();
55                             duck.setLegs(duckLegs);
56                         //如果动物对象是海豚,则修改海豚名称
57                         } else {
58                             System.out.println("请输入海豚的名称:");
59                             String name = input.next();
60                             animals[i].setName(name);
61                         }
62                     }
63                     showInfo();
64                 } catch (Exception e) {
65                     e.printStackTrace();  //打印异常的具体信息
66                 }
67             } else {
68                 break;
69             }
70 
71         } while (true);
72         System.out.println("欢迎再次使用!");
73     }
74 }
 1 package animal;
 2 
 3 public class Test {
 4 
 5     public static void main(String[] args) {
 6         AnimalMgr am=new AnimalMgr();
 7         am.init();
 8         am.showInfo();
 9         am.modify();
10     }
11 
12 }

 

转载于:https://www.cnblogs.com/baichang/p/10100803.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值