Java 中运行时多态的例子?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28961957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Example of Runtime polymorphism in Java?
提问by Namit Sinha
How is Runtime polymorphism different from Static polymorphism ?
运行时多态性与静态多态性有何不同?
Can this be an example of Runtime polymorphism ?
这可以是运行时多态性的一个例子吗?
public class X
{
public void methodA() // Base class method
{
System.out.println ("hello, I'm methodA of class X");
}
}
public class Y extends X
{
public void methodA() // Derived Class method
{
System.out.println ("hello, I'm methodA of class Y");
}
}
public class Z
{
public static void main (String args []) {
X obj1 = new X(); // Reference and object X
X obj2 = new Y(); // X reference but Y object
obj1.methodA();
obj2.methodA();
}
}
The code has been picked from here
代码是从这里挑选的
回答by NCA
Yes this is Runtime polymorphism
in Java
是的,这是Runtime polymorphism
在 Java 中
In static polymorphism
, compiler itself determines which method should call. Method overloading
is an example of static polymorphism.
在 中static polymorphism
,编译器自己决定应该调用哪个方法。Method overloading
是静态多态的一个例子。
In runtime polymorphism
, compiler cannot determine the method at compile time. Method overriding
(as your example) is an example of runtime polymorphism
.
Because in Runtime polymorphism
(as your example), the signature of methodA()
is similar in both the class X(base class)
and Y(child class)
. So compiler cannot determine method at compile time which should execute.
Only after object creation(which is a run time process), the runtime environment understand the exact method to call.
在 中runtime polymorphism
,编译器无法在编译时确定方法。Method overriding
(作为您的示例)是runtime polymorphism
. 因为在Runtime polymorphism
(如您的示例)中, 的签名methodA()
在类X(base class)
和Y(child class)
. 因此编译器无法在编译时确定应该执行的方法。只有在对象创建(这是一个运行时过程)之后,运行时环境才知道要调用的确切方法。
It is because of that in this case, obj1.methodA()
calls methodA()
in Class X
since obj1
is reference variable of object created for class X
这是因为,在这种情况下,obj1.methodA()
呼叫methodA()
在Class X
因为obj1
为创建的对象的参考变量class X
AND
obj2.methodA()
calls methodA()
in Class Y
since obj2
is reference variable of object created for class Y
与
obj2.methodA()
呼叫methodA()
在Class Y
因为obj2
为创建的对象的参考变量class Y
回答by codechefvaibhavkashyap
For your better understanding i've tried modulating your code. Note the call for constructor for both the classes.
为了您更好地理解,我尝试调整您的代码。请注意对两个类的构造函数的调用。
class X
{
X(){
System.out.println("X constructor called");
}
public void methodA() //Base class method
{
System.out.println ("hello, I'm methodA of class X");
}
}
class Y extends X
{
Y(){
System.out.println("Y constructor called");
}
public void methodA() //Derived Class method
{
System.out.println ("hello, I'm methodA of class Y");
}
}
public class Z
{
public static void main (String args []) {
X obj1 = new X(); // Reference and object X
X obj2 = new Y(); // X reference but Y object
obj1.methodA();
obj2.methodA();
}
}
output :-
输出 :-
X constructor called
X constructor called
Y constructor called
hello, I'm methodA of class X
hello, I'm methodA of class Y
X 构造函数调用
X 构造函数调用
Y 构造函数调用
你好,我是X类的methodA
你好,我是Y班的methodA
Carefully, look where objects have been created. It seems reference of X is being created using y. Method for X's is expected to be called but constructor call of Y for X reference creation says indirectly that memory has been allocated to Y's Object before X's reference is created. Take a look at the consoles for clarification.
仔细查看创建对象的位置。似乎正在使用 y 创建 X 的引用。X 的方法预计会被调用,但 X 引用创建的 Y 构造函数调用间接表示在创建 X 的引用之前已将内存分配给 Y 的对象。查看控制台以进行澄清。
回答by nitz
Its runtime polymorphism as the compiler won't know till the run time about which object method to be called.
它的运行时多态性,因为编译器直到运行时才知道要调用哪个对象方法。
however the following line will give you cast exception:
但是以下行会给您强制转换异常:
Y obj1 = new X(); //Incorrect way
Y obj1 = 新 X(); //方式不对
X obj1 = new Y(); //Correct way
X obj1 = 新 Y(); //正确的方法
Now obj1.methodA() calls methodA() in Class Y since obj1 is reference variable of object created for class Y
现在 obj1.methodA() 在 Y 类中调用 methodA() 因为 obj1 是为 Y 类创建的对象的引用变量
回答by user2048204
I changed the main method a bit as below:
我稍微改变了主要方法,如下所示:
public class X
{
public void methodA() // Base class method
{
System.out.println ("hello, I'm methodA of class X");
}
}
public class Y extends X
{
public void methodA() // Derived Class method
{
System.out.println ("hello, I'm methodA of class Y");
}
}
public class Z
{
public static void main (String args []) {
//this takes input from the user during runtime
System.out.println("Enter x or y");
Scanner scanner = new Scanner(System.in);
String value= scanner.nextLine();
if(value.equals("x"))
X obj1 = new X(); // Reference and object X
else if(value.equals("y"))
X obj2 = new Y(); // X reference but Y object
else
System.out.println("Invalid param value");
obj1.methodA();
obj2.methodA();
}
}
Now, looking at the code you can never tell which method will be called. Because it depends on what value the user gives during runtime. So, it is only decided during the runtime as to which method will be called. Hence, Runtime polymorphism.
现在,查看代码,您永远无法确定将调用哪个方法。因为这取决于用户在运行时给出的值。因此,只有在运行时才决定调用哪个方法。因此,运行时多态性。
回答by Marcus Widegren
Yes your example is an example of runtime polymorphism. An example of static polymorphism would be method overloading. Here are some good examples: What is the difference between dynamic and static polymorphism in Java?
是的,您的示例是运行时多态性的示例。静态多态的一个例子是方法重载。这里有一些很好的例子: Java 中的动态多态和静态多态有什么区别?
Cheers,
干杯,
Marcus
马库斯