多态性有什么重要的意义? [英] What is so important about polymophism?

查看:108
本文介绍了多态性有什么重要的意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对多态性有一个很好的理解,但是我的问题是,例如,在下面发布的代码中,关于在使用动物类型作为变量的同时能够制作Cat对象的特别之处是什么.这有什么目的?还要假设makenoise方法在每个对象自己的类中被重写,而其他所有类都继承的类是动物类.

I have a decent understanding about polymorphism but my question is for example, in the code I posted below, what is so special about being able to make an object of Cat while using an animal type for the variable. What purpose does that serve? Also assume that the makenoise method is being override in each objects own class and that the class all the other classes are inheriting from is the animal class.

public class Demo
{
    public static void main(String[] args) {
        Animal a1 = new Cat(); //prints meow
        a1.makeNoise(); 

        Animal a2 = new Dog();
        a2.makeNoise(); //Prints Bark
    }
}

推荐答案

多态性通过将细节限制在子类中而提供了一种将低层细节与程序主流程分开的方法,而程序只知道更高级别的细节.级课程.多态性使程序可以泛化地工作.

Polymorphism provides a way to separate low-level details from the main flow of the program, by restricting the details to subclasses, while the program only knows about the higher-level classes. Polymorphism lets the program work in terms of generalizations.

为了进一步说明您的例子

To extend your example a little we'd have

abstract class Animal {
    public abstract void makeNoise();
}

class Cat extends Animal {
    public void makeNoise() { System.out.println("meow");}
}

class Dog extends Animal {
    public void makeNoise() { System.out.println("woof");}
}

class Demo {
    public static void main(String[] args) {
        List<Animal> animals = new ArrayList<Animal>();
        animals.add(new Cat());
        animals.add(new Dog());
        for (Animal animal: animals) {
            animal.makeNoise();
        }
    }
}

在这里,演示程序可以运行在动物列表中,而无需特殊说明动物是狗还是猫的情况.这意味着我可以添加Animal的新子类,并且for循环的内容也无需更改.

Here the Demo program can run through the animals list without making any special cases for which animal is a dog and which one is a cat. That means I can add new subclasses of Animal and the for-loop contents never have to change.

如果不是多态的,代码看起来像

If it wasn't for polymorphism the code would look like

class Dog {
    public void bark() {System.out.println("woof");}
}

class Cat {
    public void meow() {System.out.println("meow");}
}

class Demo {
    public static void main(String[] args) {
        ArrayList<Object> animals = new ArrayList<Object>();
        animals.add(new Cat());
        animals.add(new Dog());
        for (Object animal: animals) {
            if (animal instanceof Dog) {
                ((Dog)animal).bark();
            } else if (animal instanceof Cat) {
                ((Cat)animal).meow();
            }
        }
    }
}

,现在该程序可以使用的动物类没有任何共性.在没有任何有用的概括的情况下,使用Demo必须了解更多有关细节的信息.引入的每种新动物都需要在for-loop身体中添加一个动物.

and now there is no commonality about the animal classes that the program can make use of. Without any useful generalization to use Demo has to know a lot more about the details. Every new kind of animal introduced will necessitate an addition to the for-loop body.

这篇关于多态性有什么重要的意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆