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

问题描述

当我开始寻找多态性的好处时,我在这里找到问题。但在这里我无法找到答案。让我告诉我想要找到什么。这里我有一些课程:

  class CoolingMachines {
public void startMachine(){
//否执行
}
public void stopMachine(){
//无执行
}
}

类冰箱扩展CoolingMachines {
public void startMachine(){
System.out.println(Refrigerator Starts);
}
public void stopMachine(){
System.out.println(Refrigerator Stop);
}
public void trip(){
System.out.println(Refrigerator Trip);
}
}

类AirConditioner扩展CoolingMachines {
public void startMachine(){
System.out.println(AC Starts);
}
public void stopMachine(){
System.out.println(AC Stop);
}
}

公共类PolymorphismDemo {
CoolingMachines cm = new Refrigerator();
冰箱rf =新冰箱();
}

现在我在Demo类中创建了两个对象,并引用了冰箱。我完全理解,从 rf 对象,我可以调用 trip()方法c> Refrigerator ,但该方法将隐藏在 cm 对象中。现在我的问题是为什么我应该使用多态或我为什么要使用

  CoolingMachines cm = new Refrigerator();我b 

$ b $ $ p> 冰箱rf =新冰箱();

多态对象的效率是好还是重量轻?这两个对象的基本目的和区别是什么? cm.start(); rf.start()

$ b之间有什么区别吗? $ b

解决方案

处理清单时很有用...
一个简短的例子:

 列表与LT; CoolingMachines> coolingMachines = ... //一个CoolingMachines列表
for(CoolingMachine current:coolingMachines){
current.start();
}

或者当你想允许方法使用<$的任何子类时c $ c> CoolingMachines


When I started to look for the benefits of polymorphism, I found with this question here. But here I was unable to find my answer. Let me tell what I want to find. Here I have some classes:

class CoolingMachines{
    public void startMachine(){
        //No implementationion
    }
    public void stopMachine(){
        //No implementationion
    }
}

class Refrigerator extends CoolingMachines{
    public void startMachine(){
        System.out.println("Refrigerator Starts");
    }
    public void stopMachine(){
        System.out.println("Refrigerator Stop");
    }
    public void trip(){
        System.out.println("Refrigerator Trip");
    }
}

class AirConditioner extends CoolingMachines{
    public void startMachine(){
        System.out.println("AC Starts");
    }
    public void stopMachine(){
        System.out.println("AC Stop");
    }
}

public class PolymorphismDemo {
    CoolingMachines cm = new Refrigerator();
    Refrigerator rf = new Refrigerator();
}

Now here I created two objects in the Demo class and are references of Refrigerator. I have completely understood that from the rf object I am able to call the trip() method of Refrigerator, but that method will be hidden for the cm object. Now my question is why should I use polymorphism or why should I use

CoolingMachines cm = new Refrigerator();

when I am OK with

Refrigerator rf = new Refrigerator();

Is polymorphic object's efficiency is good or light in weight? What is the basic purpose and difference between both of these objects? Is there any difference between cm.start(); and rf.start()?

解决方案

It is useful when you handle lists...A short example:

List<CoolingMachines> coolingMachines = ... // a list of CoolingMachines
for (CoolingMachine current : coolingMachines) {
    current.start();
}

Or when you want to allow a method to work with any subclass of CoolingMachines

这篇关于多态性的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 10:25