第十四天,java之多态声明方式调用方法的过程

package learn20180720;

public class People {

	private String name;
	private Integer age;
	private Double height;
	
	public People(){
		this.name = "";
		this.age = 0 ;
		this.height = 0.0;
	}

	public People(String name, Integer age, Double height) {
		super();
		this.name = name;
		this.age = age;
		this.height = height;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Double getHeight() {
		return height;
	}

	public void setHeight(Double height) {
		this.height = height;
	}
	
	public void sayInformation() {
		System.err.println("我的名字叫做:"+this.name+"我的年龄是:"+this.age+"我的身高是"+this.height);
	}
}
package learn20180720;

public class Chinese extends People{
	
	private String country;
	
	public Chinese(){
		super();
		country = "";
	}
	
	public Chinese(String aname,Integer aage,Double aheight) {
		super(aname,aage,aheight);
		this.country = "中国";
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	@Override
	public void sayInformation() {
		// TODO Auto-generated method stub
		System.err.println("我的名字叫做:"+this.getName()+"   我的年龄是:"+this.getAge()+"   我的身高是:"+this.getHeight()+"   我的国家是:"+this.country);
	}
	
	
	
}
package learn20180720;

public class TestPeCh {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		People p1 = new Chinese("小花", 22, 184.0);
		
		p1.sayInformation();
	}

}

在运行时,调用p1.sayInformation( )的解析过程:

 

1.首先,虚拟机提取p1的实际类型的方法表,也就是Chinese的方法表(子类的方法表有父类中所有的方法签名)。

2.接下来,虚拟机搜索定义sayInformation签名的类。此时虚拟机已经知道应该调用哪个方法。

3.最后,虚拟机调用该方法。

猜你喜欢

转载自blog.csdn.net/qq_38006520/article/details/81138088