6-6 Animal动物工厂 (20 分)

已知有如下Animal抽象类,请编写其子类Dog类与Cat类,另外再编写一个生产动物的Factory工厂类,具体要求如下。

已有的Animal抽象类定义:

abstract class Animal{
    private String name;  //名字
    private int age;   //年龄
    public abstract void info();  //返回动物信息
    public abstract void speak(); //动物叫

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }    
}

需要你编写的Dog子类:

增加violence(凶猛程度)属性(int型),重写info和speak方法

info方法输出Dog的name、age和violence属性,输出格式样例为:该狗的名字是Mike,年龄是2岁,凶猛程度是78度 (注意:输出结果中没有空格,逗号为英文标点符号)

speak方法输出Dog 的叫声,输出格式样例为:旺旺

需要你编写的Cat子类:

增加mousingAbility(捕鼠能力)属性(int型),重写info和speak方法

info方法输出Cat的name、age和mousingAbility属性,输出格式样例为:该猫的名字是Kitty,年龄是4岁,捕鼠能力是88分 (注意:输出结果中没有空格,逗号为英文标点符号)

speak方法输出Cat 的叫声,输出格式样例为:喵喵

需要你编写的Factory工厂类:

Animal getAnimalByType(int type,String name, int age, int ownAttribute):根据传入的子类类型和属性值,返回一个Animal类型的上转型对象,type为1代表狗类,type为2代表猫类

已有的Main类定义:

public class Main {

    public static void main(String[] args) {
        Factory factory=new Factory();
        Scanner input=new Scanner(System.in);
        int type=input.nextInt();
        String name=input.next();
        int age=input.nextInt();
        int ownAttribute=input.nextInt();
        Animal a=factory.getAnimalByType(type, name, age, ownAttribute);
        a.info();   
        a.speak();
        System.out.println(a.getName());
        System.out.println(a.getAge());
        input.close();
    }
}

/*请在这里填写你编写的Dog类、Cat类和Factory类 */

输入样例:

(本题的评分点与输入样例无关)

1 Mike 2 78

结尾无空行

输出样例:

该狗的名字是Mike,年龄是2岁,凶猛程度是78度
旺旺
Mike
2

结尾无空行

答案:

    class Factory{
        Animal a;
        public Factory(){}
        public Animal getAnimalByType(int type,String name, int age, int ownAttribute){
            if(type==1){
                a = new Dog(name,age,ownAttribute);
                return a;
            }else /*if(type==2)*/{
                a = new Cat(name,age,ownAttribute);
                return a;
            }
        }
    }
    //增加violence(凶猛程度)属性(int型),重写info和speak方法
    class Dog extends Animal{
        int vi;
        Dog(String name, int age, int ownAttribute){
            super(name,age);
            this.vi=ownAttribute;
        }
        public void info(){
            System.out.println("该狗的名字是"+this.getName()+",年龄是"+this.getAge()+"岁,凶猛程度是"+vi+"度");
        }  //返回动物信息
        public void speak(){
            System.out.println("旺旺");
        } //动物叫
    }class Cat extends Animal{
        int vi;
        Cat(String name, int age, int ownAttribute){
            super(name,age);
            this.vi=ownAttribute;
        }public void info(){
            System.out.println("该猫的名字是"+this.getName()+",年龄是"+this.getAge()+"岁,捕鼠能力是"+vi+"分");
        }  //返回动物信息
        public void speak(){
            System.out.println("喵喵");
        }
    }

  • 10
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

康康好好学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值