Java 多态 (polymorphism) Virtual Methods

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值