Java 中的运行时多态性示例?
Example of Runtime polymorphism in Java?
运行时多态与静态多态有何不同?
这可以作为运行时多态性的一个例子吗?
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();
}
}
代码已从 here
中选取
是的,您的示例是运行时多态性的示例。静态多态的一个例子是方法重载。这里有一些很好的例子:
What is the difference between dynamic and static polymorphism in Java?
干杯,
马库斯
为了让您更好地理解,我已尝试修改您的代码。请注意 类.
对构造函数的调用
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();
}
}
输出:-
X constructor called
X constructor called
Y constructor called
hello, I'm methodA of class X
hello, I'm methodA of class Y
仔细查看创建对象的位置。似乎正在使用 y 创建 X 的引用。 X 的方法应该被调用,但 Y 的构造函数调用用于 X 引用创建间接表示在创建 X 的引用之前内存已分配给 Y 的对象。查看控制台以进行说明。
是的,这是 Java
中的 Runtime polymorphism
在static polymorphism
中,编译器自己决定应该调用哪个方法。 Method overloading
是静态多态的一个例子。
在runtime polymorphism
中,编译器无法在编译时确定方法。 Method overriding
(如您的示例)是 runtime polymorphism
的示例。
因为在 Runtime polymorphism
(如您的示例)中,methodA()
的签名在 class X(base class)
和 Y(child class)
中都是相似的。因此编译器无法在编译时确定应该执行的方法。
只有在创建对象(这是一个 运行 时间过程)之后,运行时间环境才能理解要调用的确切方法。
因为在这种情况下,obj1.methodA()
在 Class X
中调用 methodA()
因为 obj1
是为 class X
创建的对象的引用变量
和
obj2.methodA()
在 Class Y
中调用 methodA()
因为 obj2
是为 class Y
创建的对象的引用变量
它的 运行 时间多态性,因为编译器直到 运行 时间才知道要调用哪个对象方法。
但是下面这行会给你强制转换异常:
Y obj1 = new X(); //错误的方式
X obj1 = 新 Y(); //正确的方式
现在 obj1.methodA() 在 Class Y 中调用 methodA() 因为 obj1 是为 class Y
创建的对象的引用变量
我对主要方法做了如下改动:
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();
}
}
现在,查看代码您永远无法判断将调用哪个方法。因为这取决于用户在运行时给出的值。因此,只有在运行时才决定调用哪个方法。因此,运行时多态性。
运行时多态与静态多态有何不同?
这可以作为运行时多态性的一个例子吗?
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();
}
}
代码已从 here
中选取是的,您的示例是运行时多态性的示例。静态多态的一个例子是方法重载。这里有一些很好的例子: What is the difference between dynamic and static polymorphism in Java?
干杯,
马库斯
为了让您更好地理解,我已尝试修改您的代码。请注意 类.
对构造函数的调用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();
}
}
输出:-
X constructor called
X constructor called
Y constructor called
hello, I'm methodA of class X
hello, I'm methodA of class Y
仔细查看创建对象的位置。似乎正在使用 y 创建 X 的引用。 X 的方法应该被调用,但 Y 的构造函数调用用于 X 引用创建间接表示在创建 X 的引用之前内存已分配给 Y 的对象。查看控制台以进行说明。
是的,这是 Java
中的Runtime polymorphism
在static polymorphism
中,编译器自己决定应该调用哪个方法。 Method overloading
是静态多态的一个例子。
在runtime polymorphism
中,编译器无法在编译时确定方法。 Method overriding
(如您的示例)是 runtime polymorphism
的示例。
因为在 Runtime polymorphism
(如您的示例)中,methodA()
的签名在 class X(base class)
和 Y(child class)
中都是相似的。因此编译器无法在编译时确定应该执行的方法。
只有在创建对象(这是一个 运行 时间过程)之后,运行时间环境才能理解要调用的确切方法。
因为在这种情况下,obj1.methodA()
在 Class X
中调用 methodA()
因为 obj1
是为 class X
创建的对象的引用变量
和
obj2.methodA()
在 Class Y
中调用 methodA()
因为 obj2
是为 class Y
它的 运行 时间多态性,因为编译器直到 运行 时间才知道要调用哪个对象方法。
但是下面这行会给你强制转换异常:
Y obj1 = new X(); //错误的方式
X obj1 = 新 Y(); //正确的方式
现在 obj1.methodA() 在 Class Y 中调用 methodA() 因为 obj1 是为 class Y
创建的对象的引用变量我对主要方法做了如下改动:
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();
}
}
现在,查看代码您永远无法判断将调用哪个方法。因为这取决于用户在运行时给出的值。因此,只有在运行时才决定调用哪个方法。因此,运行时多态性。