Java基础8--面向对象--多态

1.多态

1.1多态的概述

  • 什么是多态

    ​ 同一个对象,在不同时刻表现出来的不同形态

  • 多态的前提

    • 要有继承或实现关系
    • 要有方法的重写
    • 要有父类引用指向子类对象

1.2多态中的成员访问特点

  • 成员访问特点

    • 成员变量

      ​ 编译看父类,运行看父类

    • 成员方法

      ​ 编译看父类,运行看子类

  • 代码演示

    • 动物类

      public class Animal {
          public int age = 40;
      
          public void eat() {
              System.out.println("动物吃东西");
          }
      }
      
    • 猫类

      public class Cat extends Animal {
          public int age = 20;
          public int weight = 10;
      
          @Override
          public void eat() {
              System.out.println("猫吃鱼");
          }
      
          public void playGame() {
              System.out.println("猫捉迷藏");
          }
      }
      
    • 测试类

      public class AnimalDemo {
          public static void main(String[] args) {
              //有父类引用指向子类对象
              Animal a = new Cat();
      
              System.out.println(a.age);
      //        System.out.println(a.weight);
      
              a.eat();
      //        a.playGame();
          }
      }
      

1.3多态的好处和弊端

  • 好处

    ​ 提高程序的扩展性。定义方法时候,使用父类型作为参数,在使用的时候,使用具体的子类型参与操作

  • 弊端

    ​ 不能使用子类的特有成员

1.4多态中的转型

  • 向上转型

    ​ 父类引用指向子类对象就是向上转型

  • 向下转型

    ​ 格式:子类型 对象名 = (子类型)父类引用;

  • 代码演示

    • 动物类
    public class Animal {
        public void eat() {
            System.out.println("动物吃东西");
        }
    }
    
    • 猫类
    public class Cat extends Animal {
        @Override
        public void eat() {
            System.out.println("猫吃鱼");
        }
    
        public void playGame() {
            System.out.println("猫捉迷藏");
        }
    }
    
    • 测试类
    public class AnimalDemo {
        public static void main(String[] args) {
            //多态
            //向上转型
            Animal a = new Cat();
            a.eat();
    //      a.playGame();
    
    
            //向下转型
            Cat c = (Cat)a;
            c.eat();
            c.playGame();
        }
    }
    

1.5多态的案例

  • 案例需求

    ​ 请采用多态的思想实现猫和狗的案例,并在测试类中进行测试

  • 代码实现

    • 动物类
    public class Animal {
        private String name;
        private int age;
    
        public Animal() {
        }
    
        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;
        }
    
        public void eat() {
            System.out.println("动物吃东西");
        }
    }
    
    • 猫类
    public class Cat extends Animal {
    
        public Cat() {
        }
    
        public Cat(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println("猫吃鱼");
        }
    }
    
    • 狗类
    public class Dog extends Animal {
    
        public Dog() {
        }
    
        public Dog(String name, int age) {
            super(name, age);
        }
    
        @Override
        public void eat() {
            System.out.println("狗吃骨头");
        }
    }
    
    • 测试类
    public class AnimalDemo {
        public static void main(String[] args) {
            //创建猫类对象进行测试
            Animal a = new Cat();
            a.setName("加菲");
            a.setAge(5);
            System.out.println(a.getName() + "," + a.getAge());
            a.eat();
    
            a = new Cat("加菲", 5);
            System.out.println(a.getName() + "," + a.getAge());
            a.eat();
        }
    }
    
posted @ 2020-12-15 22:10  全栈老刘  阅读(85)  评论(0编辑  收藏  举报