package org.virtualmethod;
public class A {
private String name ;
private int number;
public A(String name,int number){
this.name= name;
this.number = number;
System.out.println("this is A constutor");
}
public void getData(){
System.out.println("a's method getData");
}
}
package org.virtualmethod;
public class B extends A{
public B(String name,int number){
super(name,number);
System.out.println("this is B constutor");
}
public void getData(){
System.out.println("B's method getData");
}
}
package org.virtualmethod;
public class VirtualMethodDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
B b = new B("boo",11);
A a = new B("bian",22);
System.out.println("*** call getData using B reference **");
b.getData();
System.out.println("*** call getData using A reference **");
a.getData();
}
}
执行结果
this is A constutor
this is B constutor
=============
this is A constutor
this is B constutor
*** call getData using B reference **
B's method getData
*** call getData using A reference **
B's method getData
****************************************************************************************************
涉及到两个方面 1:虚函数的问题 2:构造函数问题
1:the program instantiates two B objects ---one using a B reference ,and the other using an A refererce a.
In the following statement ,the compiler sees getData() in the B class at compaile time,and the JVM invokes getData() in the B class at run time;
b.getData();
Invoking getData() on a is quite different because a is an A reference. When compiler sees the following statement ,the compiler sees the getData() method in
the A class
a.getData();
Does this mean that getData() in the A class is invoked at run tine? No! Howerver, the JVM invoke getData() in the B class.
Here is Why.
With vitrual method ,the JVM must look donwn the inheritance hierachy and determin if a menthod is overridden. If the method is overridden ,the
child method executes at run time ,not the one in the parent that was innoked at compile time.
************************************************************************************************************************************************************************************
+++++++++++++++++++++++++++++
Taking advantage of virtual method
package org.virtualmethod;
public class A {
private String name ;
private int number;
public A(String name,int number){
this.name= name;
this.number = number;
System.out.println("this is A constutor");
}
public A(){
System.out.println("this is A consturtor without parm");
}
public String getData(){
System.out.println("a's method getData");
return "a's method getData";
}
public void mail(){
System.out.println("this is mail() form A");
}
}
package org.virtualmethod;
public class B extends A{
public B(String name,int number){
super(name,number);
System.out.println("this is B constutor");
}
public String getData(){
System.out.println("B's method getData");
return "B's method getData";
}
public void mail(){
System.out.println("this is mail() form B");
}
}
package org.virtualmethod;
public class C extends A{
public C(String name ,int numbers){
System.out.println("this is C constutor");
}
public String getData(){
System.out.println("C's method getData");
return "B's method getData";
}
}
package org.virtualmethod;
public class SmartBoss {
public void toGetData(A aa){
System.out.println("this is toGetData() method");
String time = aa.getData();
aa.mail();
}
}
package org.virtualmethod;
public class VirtualMethodDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
SmartBoss boss = new SmartBoss();
C c = new C("boo&bian",33);
System.out.println("*****************************");
B b = new B("boo",11);
System.out.println("*****************************");
A a = new B("bian",22);
System.out.println("*****************************");
System.out.println("*** call getData using B reference **");
boss.toGetData(b);
System.out.println("*** call getData using A reference **");
boss.toGetData(a);
System.out.println("*** call getData using C reference **");
boss.toGetData(c);
}
}
执行结果:
this is A consturtor without parm
this is C constutor
*****************************
this is A constutor
this is B constutor
*****************************
this is A constutor
this is B constutor
*****************************
*** call getData using B reference **
this is toGetData() method
B's method getData
this is mail() form B
*** call getData using A reference **
this is toGetData() method
B's method getData
this is mail() form B
*** call getData using C reference **
this is toGetData() method
C's method getData
this is mail() form A