栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java程序设计 实验4:接口与多态

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java程序设计 实验4:接口与多态

题1:动物园管理程序设计。假设某动物园有20只笼子用于饲养宠物,包括猫和狗。动物园可以收容宠物,也可以被认领出去。猫和狗都有名字属性和“叫”的方法(方法内涵不同)。

要求用一个数据项(如数组等)管理这20只笼子和其中的宠物,另外编制一个方法bark(),用来根据实际对象调用他们“叫”的方法。

import java.util.Scanner;

public class AnimalTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Animal []an=new Animal[20];
        Cat c=new Cat();
        Dog d=new Dog();
        while (true) {
            System.out.println("选择功能");
            System.out.println("------------动物管理系统--------------");
            System.out.println("------------1.存放动物--------------");
            System.out.println("------------2.取出动物--------------");
            System.out.println("------------3.退出系统--------------");
            int i = sc.nextInt();


            switch (i) {
                case 1:
                    System.out.println("存放");
                    System.out.println("1.猫");
                    System.out.println("2.狗");
                    int k = sc.nextInt();
                    switch (k) {
                        case 1: ;
                            c.add(an);
                            break;
                        case 2:;
                            d.add(an);
                            break;
                    }break;
                case 2:
                    System.out.println("取出");
                    System.out.println("1.猫");
                    System.out.println("2.狗");
                    k = sc.nextInt();
                    switch (k) {
                        case 1:c.out(an);
                            break;
                        case 2:
                            d.out(an);
                            break;
                    }break;

                case 3:return;
            }
        }

    }
}

class Cat {
    static  int n=1;
    Scanner sc=new Scanner(System.in);
    void bark(){
        System.out.println("喵");
    }
   public void add(Animal an[]){

        System.out.println("请输入宠物名字");
        String name=sc.next();
        an[n]=new Animal(name);
        an[n].setName(name);
        System.out.println("存入成功");
        System.out.println("编号为"+n);
        bark();
        n++;
    }
    void out(Animal an[]){
        System.out.println("请输入要取走宠物的编号");
        int k=sc.nextInt();

        if(an[k]==null)
        {
            System.out.println("没有此编号");
        }
        else {
            System.out.println("请输入要取走宠物的名字");
            String name=sc.next();
            if(name.equals(an[k].name))
            {System.out.println("成功取出");
                bark();
                an[k]=null;


            }
           else
            {   System.out.println("没有这个名字");}
        }
    }

}
class Dog
{ static  int n=1;
    void bark(){
        System.out.println("汪");
    }
    Scanner sc=new Scanner(System.in);
    void add(Animal an[]){
        System.out.println("请输入宠物名字");
        String name=sc.next();
        an[n]=new Animal(name);
        an[n].setName(name);
        System.out.println("存入成功");
        System.out.println("编号为"+n);
        bark();
        n++;
    }

    void out(Animal an[]){
        System.out.println("请输入要取走宠物的编号");
        int k=sc.nextInt();
        if(an[k]==null)
        {
            System.out.println("没有此编号");
        }
        else {
            System.out.println("请输入要取走宠物的名字");
            String name=sc.next();
            if(name.equals(an[k].name))
            {
                System.out.println("成功取出");
                bark();
                an[k]=null;
            }
        else
                System.out.println("没有这个名字");

        }
    }
}
class Animal{
    String name;

    Animal(String name)
    {
        this.name=name;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

题3:创建抽象类Bird,有颜色和大小属性,类中声明两个抽象方法chirp()和shoot(), Bird类派生出AngryBird、CrazyBird和KindBird三个子类,子类分别实现chrip()和shoot()方法,但实现的方法不同(方法内容不同),并且AngryBird的类的对象增加弹出后分理处攻击力更强的三只小鸟方法spawn()方法, CrazyBird类的对象弹出后会下蛋并产生爆炸效果eggbomb()方法,KindBird增加微笑smile()方法。在主方法中,使用动态绑定方式实现对子类方法的调用。

package Day4;

import java.util.Scanner;

public class BirdTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入小鸟颜色");
        String c = sc.next();
        System.out.println("请输入小鸟大小");
        String s = sc.next();
        System.out.println("请输入数字选择小鸟种类");
        System.out.println("1.AngryBird");
        System.out.println("2.CrazyBird");
        System.out.println("3.KindBird");
        int i = sc.nextInt();
        System.out.println("小鸟的基本信息为:");
        switch (i) {
            case 1:
                Bird b = new AngryBird(c, s);
                b.Basic();
                b.chirp();
                b.shoot();
                ((AngryBird) b).spawn();
                break;
            case 2:
                Bird r=new CrazyBird(c,s);
                r.Basic();
                r.chirp();
                r.shoot();
                ((CrazyBird)r ).eggbomb();break;
            case 3:
                Bird k=new KindBird(c,s);
                k.Basic();
                k.chirp();
                k.shoot();
                ((KindBird)k ).smile();break;

        }


    }
}

abstract class Bird {
    String color, size;

    Bird(String color, String size) {
        this.color = color;
        this.size = size;
    }

    abstract void Basic();

    abstract void chirp();

    abstract void shoot();

}

class AngryBird extends Bird {
    AngryBird(String color, String size) {
        super(color, size);
    }

    void Basic() {
        System.out.println("小鸟的颜色是:" + color + "n小鸟大小为:" + size);
    }

    void chirp() {
        System.out.println("AngryBird小鸟叫");
    }

    void shoot() {
        System.out.println("AngryBird射击");
    }

    void spawn() {
        System.out.println("攻击加强");
    }
}

class CrazyBird extends Bird {
    void Basic() {
        System.out.println("小鸟的颜色是:" + color + "n小鸟大小为:" + size);
    }

    CrazyBird(String color, String size) {
        super(color, size);
    }

    void chirp() {
        System.out.println("CrazyBird叫");
    }

    void shoot() {
        System.out.println("CrazyBird射击");
    }

    void eggbomb() {
        System.out.println("下蛋并且炸弹爆炸");
    }
}

class KindBird extends Bird {
    void Basic() {
        System.out.println("小鸟的颜色是:" + color + "n小鸟大小为:" + size);
    }

    KindBird(String color, String size) {
        super(color, size);
    }

    void chirp() {
        System.out.println("KindBird叫");
    }

    void shoot() {
        System.out.println("KindBird射击");
    }

    void smile() {
        System.out.println("微笑");
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/749845.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号