Consider the two calls
public class D extends B
{
public void f()
{
this.g(); // 1
}
public void g()
{
super.g(); // 2
}
. . .
}
Which of them is an example of polymorphism?为什么
public class D extends B
{
public void f()
{
this.g(); // 1
}
public void g()
{
super.g(); // 2
}
. . .
}
Which of them is an example of polymorphism?为什么
A中有一个方法public void fun()
B是继承A的,B中重写了fun方法。这时,用A的引用去调用fun方法,就存在了多态了。当这个引用指向A类对象,就调用A的fun,指向B的对象,就调用B的fun
public void print(){
System.out.println("This is A's function");
}
}
class B extends A{
public void print(){
System.out.println("This is B's function");
}
}
public class Polymorphism {
public static void main(String args[]){
A p;
p=new A(); //A类引用指向A类对象
p.print();
p=new B(); //A类引用指向B类对象
p.print();
}
}
public void print(){
System.out.println("This is A's function");
}
}
class B extends A{
public void print(){
System.out.println("This is B's function");
}
}
public class Polymorphism {
public static void main(String args[]){
A p;
p=new A(); //A类引用指向A类对象
p.print();
p=new B(); //A类引用指向B类对象
p.print();
}
}