• 让我们进入面向对象的世界(四)



    前言

    前面我们讲过了,面向对象继承的特性,下面我们会根据多态来展开讨论,还是用熟悉的方式,让大家去了解这个疯狂的东西。


    一. 初始多态

    1.1 多态是什么

    多态的概念:通俗来说,就是多种形态,具体点就是去完成某个行为,当不同的对象去完成时会产生出不同 的状态。
    听起来你会觉得很奇怪,当我们去调用不同对象的时候,我们会产生不同的行为,举例子来说这里有一个打印机,然后我们打印东西,可以打黑白的,也可以打彩色的,明明同样是打印机,但我们根据不同的状态,调用同一个方法,可能呈现的行为是不一样的。

    1.2 多态是怎么工作

    说实话哈,如果要看多态是怎么工作的,我们就要从对象的引用和声明,创建对象的方法开始,为什么这么说呢?大家看我下面的操作就知道了。


    //这里我声明一个狗对象
    Dog mydog =new Dog();
    
    
    • 1
    • 2
    • 3

    它实际上在做了一个什么样操作呢?看下面的图,你就明白了。

    在这里插入图片描述

    然后我这里这么写代码,记住我这样写,是因为在多态中,引用和对象可以是不同的类型

    Animal animal=new Dog();
    //这编译是能够通过的。
    
    
    • 1
    • 2
    • 3

    在这句代码中,究竟发生了什么呢?现在让我们来看看。
    在这里插入图片描述

    其实这里就说明了一个问题,就是我们在使用多态时,引用类型是实际对象类的父类


    大家如果还是对多态机制懵懵懂懂的,我们直接上来写一个简单的例子,这个例子就是简单的三个类,Animal、Dog、Cat类,就用这个例子,来讲明白多态。

    
    public class Animal {
        String picture;//动物图像的名称
        String food;//动物所吃的食物
        int hunger;//代表饥饿程度的值,根据动物吃了多少来定义
        int boundaries;//动物活动的区域
        double location;//动物活动区域的X与Y的坐标
        public Animal(){
    
        }
        public Animal(String picture, String food, int hunger, int boundaries, double location) {
            this.picture = picture;
            this.food = food;
            this.hunger = hunger;
            this.boundaries = boundaries;
            this.location = location;
        }
    
        public void  makeNoise(){
            //动物发出的声音
            System.out.println(picture+"发声音");
    
        }
        public  void eat(){
            //吃东西
        }
        public void  sleep(){
            //睡眠
        }
        public void roam(){
            //其他的行为
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    
    public class Cat extends Animal{
    
        public Cat() {
            super();
        }
        public Cat(String picture, String food, int hunger, int boundaries, double location){
            super(picture,food,hunger,boundaries,location);
        }
    
    
        @Override
        public void makeNoise() {
            System.out.println(picture+"喵喵叫");
        }
    
        @Override
        public void eat() {
            System.out.println(picture+"吃鱼");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    
    public class Dog extends Animal{
        public Dog() {
            super();
    
        }
        public Dog(String picture, String food, int hunger, int boundaries, double location){
            super(picture,food,hunger,boundaries,location);
        }
    
    
        @Override
        public void makeNoise() {
            System.out.println(picture+"汪汪叫");
        }
    
        @Override
        public void eat() {
            System.out.println(picture+"啃骨头");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    上面就是我们定义好的三个类,我们现在在main方法中进行不同的操作,看看会发生什么。

     Animal animal2=new Dog();
            animal2.picture="旺财";
            animal2.eat();
    
    • 1
    • 2
    • 3

    运行结果:
    在这里插入图片描述

    实际上,我们去调试的时候,我们去发现这个annimal的引用实际上是指向了dog对象的。
    在这里插入图片描述
    这里实际上说明了一个观点。
    当你声明一个引用变量时,任何对该引用变量类型可通过IS-A测试的对象都可以被赋值给该引用。换句话说,任何extends过声明引用变量类型的对象都可以被赋值给这个引用变量。这样子你就可以做出多态数组这一类的东西。
    相信大家对多态的印象又多了了一分,接下来,我们会接触到多态的向上转型和向下转型。

    1.3多态的向上转型和向下转型

    可能大家对于多态还是非常陌生,我也知道大家很难理解,我这里之所以引入这个东西,大家也不要在意,语法是语法,后面有实际的例子,当大家进行一个综合例子之后,就能慢慢理解多态了。


    向上转型
    我简单来理解,向上转型就等同于数据类型中的自动类型转换,感觉上是差不多的。

    /*
    Animal 和 Dog 类
    是父与子的关系
    */
    Animal aniaml=new Dog();//这里就发生了向上转型。
    //语法就是这么简单,没什么复杂度,难得是运用
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    这里我也举一个实际的例子,让大家体验一下,向上转型的语法

    我们有三个类Animal、dog、cat类,类如下:
    这里我是把三个类写到一起的,方便给你们观看的。

    public class Animal {
        String name;
        String age;
        public void Play(){
            System.out.println(name+"玩耍");
        }
        public void Eat(){
            System.out.println(name+"吃东西");
        }
        public void  sleep(){
            System.out.println(name+"睡觉");
        }
    }
    public class Dog extends Animal{
        public void Eat(){
            System.out.println(name+"啃骨头");
        }
    }
    
    public class Cat extends Animal{
    
        public void Eat(){
            System.out.println(name+"吃鱼鱼!!");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    我们接下来要做一个什么事情呢?就是我们使用不同的对象调用,会产生不同的动作。我会用俩种方式,一种是使用多态,一种是不使用多态,去进行。

    不使用多态的话,我们就要使用if条件去判断是否是一个东西,然后根据这个不同的对象,去输出行为。

    public class Test {
        public static void xingwei1(){
            Cat cat =new Cat();
            cat.name="猜猜";
            Dog dog=new Dog();
            dog.name="旺财";
            String[] animals={"dog","cat"};
            for (String a:animals){
                if (a.equals("dog")){
                    dog.eat();
                }else if (a.equals("cat")){
                    cat.eat();
                }
            }
        }
        public static void main(String[] args) {
               xingwei1();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    使用多态的向上转型后,代码稍稍做了一些修改,但也不难理解。

        public static void xingwei2(){
            Cat cat =new Cat();
            cat.name="猜猜";
            Dog dog =new Dog();
            dog.name="旺财";
            Animal[] animal1={cat,dog};//这里就是用了,多态
            for (Animal s:animal1) {
                    s.eat();
                
            }
        }
        public static void main(String[] args) {
    //           xingwei();
            xingwei2();
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    这里你就可以看出,多态的向上转型,可以让代码变得异常灵活了。


    ** 向下转型**
    我的理解是向下转型跟数据类型的强制类型转换差不多,大家大致来看一看语法机制,你就明白了。

      		Cat cat =new Cat();
            Animal animal=new Dog();
            //此时aniaml是狗,不可能是猫,此时我们就需要向下转型
           cat=(Cat) animal;
    		cat.eat();
    
    • 1
    • 2
    • 3
    • 4
    • 5

    但向下转型存在一个问题哈,你用常规性的思维思考一下,狗怎么会变成猫呢?这完全就是不符合常理,你不信的话,你运行一下,你就会看到如下警报
    在这里插入图片描述
    因此,我们有时候,在向下转型的时候,会运用一个关键字,instanof,去判断是否合法

      Cat cat =new Cat();
            Dog dog =new Dog();
            Animal animal = cat;
            //此时aniaml是狗,不可能是猫,此时我们就需要向下转型
    
            if(animal instanceof Cat){
                cat = (Cat)animal;
                cat.eat();
            }
            if(animal instanceof Dog){
                dog = (Dog)animal;
                dog.eat();
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    好了好了,同志们,看了这么久的多态以后。我们来进行一个综合例子,去观察观察,多态的好处。

    二.多态的好处

    大家都知道打印图形的例子吧,我们操作一个对象进行不同的行为。还是来两种方式,一种用多态的方式和一种用多态的方式,

    2.1不用多态的方式

    class Shape {
    
        public void draw() {
            System.out.println("画图形!");
        }
    }
    class Rect extends Shape {
        @Override
        public void draw() {
            System.out.println("画矩形!");
        }
    }
    class Cycle extends Shape {
        @Override
        public void draw() {
            System.out.println("画圆!");
        }
    }
    
    public class TestMain(){
    public static void drawMap() {
            Rect rect = new Rect();
            Cycle cycle = new Cycle();
            Flower flower = new Flower();
            //cycle  rect  cycle  rect  flower
            String[] shapes = {"cycle", "rect", "cycle", "rect", "flower"};
            for (String s : shapes) {
                if(s.equals("cycle")) {
                    cycle.draw();
                }else if(s.equals("rect")) {
                    rect.draw();
              
            }
        }
    
    public static void main(String[] args) {	
    	drawMap();
    }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    使用多态的版本

     public static void drawMap() {
            Shape rect = new Rect();
            Shape cycle = new Cycle();
            Shape flower = new Flower();
    
            Shape[] shapes = {cycle,rect,cycle,rect};
            //int[] array = {1,2,3,4};
            for(Shape shape : shapes) {
                shape.draw();
            }
        }
        public static void main(String[] args) {	
    	drawMap();
    }
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    看了上面的俩个例子香型你们已经大概理解多态的机制。

    三.总结

    通过多态,你就可以编写出引进新型子类时也不必修改的程序。
    这可能,有些简短了,但事实就是这样。
    又到了,跟大家说再见的时候了,下一次我们将带大家深入多态,了解抽象类和接口

  • 相关阅读:
    【红外图像】利用红外图像处理技术对不同制冷剂充装的制冷系统进行性能评估(Matlab代码实现)
    【MicroPython RP2040】通过ADC调节PWM输出示例
    华为OD机考算法题:机器人活动区域
    深入了解Vue.js框架:构建现代化的用户界面
    Gif裁剪工具如何操作?教你三步快速裁剪gif动图
    C Primer Plus(6) 中文版 第7章 C控制语句:分支和跳转 7.3 逻辑运算符
    el-input-number 添加单位
    微信小程序配置
    day05_流程控制语句
    网络安全(黑客)自学
  • 原文地址:https://blog.csdn.net/qq_45726327/article/details/128040025