6-1 动物体系

基于继承关系编写一个动物体系,具体的动物包含小狗和小猫。每只动物都有名字和颜色,都能够做自我介绍(introduce)。此外,小狗有智商属性(整数),能接飞盘(catchFrisbee(),方法体内输出一行“catch frisbee”即可),小猫有眼睛颜色属性,能抓老鼠(catchMouse(),方法体内输出一行“catch mouse”即可)。各种小动物自我介绍时均介绍自己的姓名和颜色,此外,小狗应介绍自己的智商,小猫应介绍自己的眼睛颜色。小狗介绍时输出”My name is xxx, my color is xxx, my IQ is xxx”, 小猫介绍时输出“My name is xxx, my color is xxx, my eyecolor is xxx”
构造类TestAnimal,提供静态函数introduce(Animal),对参数动物自我介绍。提供静态函数action(Animal),根据参数对象的实际类型进行活动,如果是小狗,则让其接飞盘,如果是小猫,则让其抓老鼠。
Main函数中,根据动物类型构造动物,并调用TestAnimal中的方法进行自我介绍(introduce)和活动(action)

输入描述:

动物类型 动物名称 动物颜色 动物其他属性 如
1 猫名称 猫颜色 猫眼睛颜色
2 狗名称 狗颜色 狗的智商

输出描述:

自我介绍
活动

裁判测试程序样例:

import java.util.Scanner;

/*你的代码被嵌在这里 */

public class Main{
    
    public static void main(String args[]) {
        
        Scanner s = new Scanner (System.in);
        int i = s.nextInt();
        Animal a = null;
        if (i==1) {
            a = new Cat(s.next(), s.next(), s.next());
        } else if (i==2) {
            a = new Dog(s.next(), s.next(), s.nextInt());
        }
        TestAnimal.introduce(a);
        TestAnimal.action(a);
        
    }
}

输入样例:

在这里给出一组输入。例如:

1 Mikey white blue

输出样例:

在这里给出相应的输出。例如:

My name is Mikey, my color is white, my eyecolor is blue
catch mouse

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

答案

abstract class Animal {
    String name,color;
    Animal(String name,String color)
    {
        this.name=name;
        this.color=color;
    }
  public abstract void introduce();
}
class Dog extends Animal
{
    int IQ;
    Dog(String name,String color,int IQ)
    {
        super(name,color);
        this.IQ=IQ;
    }
     public void introduce()
    {
        System.out.println("My name is "+name+", "+"my color is "+color+", my IQ is "+IQ);
    }
    public void catchFrisbee()
    {
        System.out.printf("catch frisbee");
    }
}
class Cat extends Animal
{
    String eyecolor;
    Cat(String name,String color,String eyecolor)
    {
        super(name,color);
        this.eyecolor=eyecolor;
    }
    public void introduce()
    {
        System.out.println("My name is "+name+", "+"my color is "+color+", my eyecolor is "+eyecolor);
    }
    public void catchMouse()
    {
        System.out.printf("catch mouse");
    }
}
class TestAnimal{
    public static void action(Animal a) {
        if(a instanceof Dog) {
            Dog d=(Dog)a;
            d.catchFrisbee();
        }else if(a instanceof Cat) {
            Cat c=(Cat)a;
            c.catchMouse();
        }
    }
    public static void introduce(Animal a) {
        if(a instanceof Dog) {
            Dog d=(Dog)a;
            d.introduce();
        }else if(a instanceof Cat) {
            Cat c=(Cat)a;
            c.introduce();
        }
    }
}

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小白宇航(互关版)

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

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

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

打赏作者

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

抵扣说明:

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

余额充值