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

学生喂养三种宠物:猫、狗和鸟

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

学生喂养三种宠物:猫、狗和鸟

文章目录
  • 学生喂养三种宠物:猫、狗和鸟
    • (一)编程实现
      • 1、创建动物类
      • 2、继承动物类,创建猫类
      • 3、继承动物类,创建狗类
      • 4、继承动物类,创建鸟类
      • 5、创建学生类
      • 6、创建测试学生类
      • 7、运行程序,查看结果

学生喂养三种宠物:猫、狗和鸟

动物类(Animal):属性(name、age)、方法(speak、move、eat)
猫类(Cat)继承动物类,添加新方法(play)
狗类(Dog)继承动物类,添加新方法(play)
鸟类(Bird)继承动物类,添加新方法(play)
学生类(Student):属性(name)、方法(feed(animal))

(一)编程实现 1、创建动物类

· 在p03.t03包里创建Animal类

package net.p03.t03;


public class Animal {
    private String name;
    private int 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;
    }

    public void speak() {
        System.out.println(name + "今年"  + age + "岁了。");
    }

    public void move() {
        System.out.println(name + "在动……");
    }

    public void eat() {
        System.out.println(name + "在吃……");
    }

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + ''' +
                ", age=" + age +
                '}';
    }
}
2、继承动物类,创建猫类

·在p03.t03创建Cat类

3、继承动物类,创建狗类

在net.p03.t03包里创建Dog类

4、继承动物类,创建鸟类

在p03.t03包里创建Bird类

5、创建学生类

在net.p03.t03包里创建Student类

public class Student {
    private String name;

    public String getName() {
        return name;
    }

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

    public void feed(Animal animal) {
        animal.move(); //体现多态(子类改写父类的同名同参方法)
        animal.eat();//多态体现(子类改写父类的同名同参方法)
        animal.speak();//子类继承了父类的方法
        if (animal instanceof Cat) { //判断是否养猫实例
            System.out.println(name + "喂养猫咪[" + animal.getName() + "]。");
            Cat cat = (Cat) animal;//Animal向下转型为Cat
            cat.play();
        } else if (animal instanceof Dog) { //判断是否狗实例
            System.out.println(name + "喂养狗崽[" + animal.getName() + "]。");
            Dog dog = (Dog) animal;//Animal向下转型为Cat
            dog.play();
        } else if (animal instanceof Bird) {//判断是否养鸟实例
            System.out.println(name + "喂养小鸟[" + animal.getName() + "]。");
            Bird bird = (Bird) animal;//Animal向下转型为Cat
            bird.play();

        } else {
            System.out.println(name + "喂养动物[" + animal.getName() + "]。");
        }
    }
}
6、创建测试学生类

在p03.t03包里创建TestStudent类

public class TestStudent {
    public static void main(String[] args) {
        //创建学生对象
        Student student = new Student();
        student.setName("小张");

        //创建第一个动物
        Animal animal1 = new Cat();
        animal1.setName("哇哇");
        animal1.setAge(5);

        //第二动物
        Animal animal2 = new Dog();
        animal2.setName("小小");
        animal2.setAge(6);
        //第三动物
        Animal animal3 = new Bird();
        animal3.setName("天天");
        animal3.setAge(2);
        //调用学生对象的喂养方法,传入不同的动物对象
        student.feed(animal1);
        student.feed(animal2);
        student.feed(animal3);
    }
}
7、运行程序,查看结果

运行TestStudent类

分别创建猫、狗与鸟对象,注意,指向的都是父类Animal变量animal1、animal2与animal3,传入学生对象的feed()方法,在feed()方法调用参数对象的move()与eat()方法时,程序在编译时自动识别具体的子类对象,从而选择性地调用对应的方法,这就是Java多态性的体现。由此可见,多态不仅解决了方法同名的问题,而且还使程序变得更加灵活,从而有效地提高程序的可扩展性和可维护性。
美中不足之处:运行结果当中没有显示出各种动物的玩法

修改Student类,直接添加一句animal.play(),但是要报错。

· 此时,我们要利用对象的向下转型(downcasting)
· 再次运行测试学生类,查看结果

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

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

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