问题描述
我发现关于多态和重载有很多定义.有人说,重载是多态性的一种类型.虽然有些人说他们不一样.因为在重载中将只分配一个功能.虽然多态性需要为每个重新定义的成员函数分配内存.我对此感到非常困惑,任何人都可以为我解释一下吗?另外,重载是否在编译时发生而多态性在运行时发生?
I found there has many definition about polymorphism and overloading. Some people said that overloading is one type of polymorphism. While some people said they are not the same. Because only one function will be allocate in overloading. While the polymorphism need allocate the memory for each redefined member function. I really feel confusion about this, any one could explain this for me?Further, whether overloading happens at compile time while the polymorphism happens at running time?
推荐答案
您无法确定方法是多态方法,还是仅基于其签名的简单重写方法.您需要查看该方法的调用方式.
You cannot determine whether a method is a polymorphic method, or simply an overridden method based solely upon its signature. You need to see how the method is invoked.
以下示例代码可能有助于阐明答案:
Here is some sample code which may help illuminate the answer:
public class parentClass {
//Overridden method
public void disp()
{
System.out.println("Output: disp() method of parent class");
}
}
public class childClass extends parentClass {
//You cannot determine whether these methods are polymorphic
//or static polymorphic (aka overridden) simply by their signatures.
//It is by the way they are invoked which determines this.
public void disp(){
System.out.println(" Output: disp() method of Child class");
}
public long add(long a, long b){
return a + b;
}
public int add(int a, int b){
return a+b;
}
public String add(String a, String b){
return a + b;
}
public static void main( String args[]) {
//Here a child class has overridden the disp() method of the
//parent class. When a child class reference refers to the child
//class overriding method this is known as static polymorphism
//or more simply, overriding.
System.out.println("childClass referencing the childClass's overridden, or static polymorphic method");
childClass myChildObj = new childClass();
myChildObj.disp();
//Another example of static polymorphic, or overridden methods
System.out.println("The following are overridden, or static polymorphic methods:");
System.out.printf(" Long add()override method results: %d \n",
myChildObj.add(5999999, 1));
System.out.printf(" Integer add() override method results: %d \n", myChildObj.add(3,2));
System.out.printf(" String add() override method results: %s \n",
myChildObj.add(" First and ...", " Second"));
//When the parent class reference refers to the child class object
//then the overriding method is called.
//This is called dynamic method dispatch and runtime polymorphism
System.out.println("True polymorphism, when the parent class object calls the child class's method:");
parentClass myParentObj = new childClass();
myParentObj.disp();
}
}
预期输出:
childClass引用childClass的重写或静态多态方法
childClass referencing the childClass's overridden, or static polymorphic method
Output: disp() method of Child class
以下是重写的或静态的多态方法:
The following are overridden, or static polymorphic methods:
Long add()override method results: 6000000
Integer add() override method results: 5
String add() override method results: First and ... Second
当父类对象调用子类的方法时,真实多态的一个示例:
One example of true polymorphism, when the parent class object calls the child class's method:
Output: disp() method of Child class
这篇关于多态和重载之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!