Java关于父类引用指向子类对象

1. 首先, java的多态是指子类重写了父类的方法,在程序运行期间(非编译),根据引用指向的“实际对象”来调用对象的方法,子类是不能重写父类的变量的。

class Father{
	int age = 30;
	String name = "雷盼是父亲";
	public Father(String AName){
		System.out.println(Father.class.getName());
	}
	public Father(){
		System.out.println("Father Constructor!");
	}
	public int getAge(){
		return this.age;
	}
	public String getName(){
		return this.name;
	}
};
class Son extends Father{
	int age = 1;
	String name = "雷梓诺是儿子";
	public Son(){
//		super("12");//会隐式调用,不管你写不写super
		System.out.println(Son.class.getName());
	}
	public int getAge(){
		return this.age;
	}
	public String getName(){
		return this.name;
	}
}
class Extends {
	public static void main(String[] args) {
		Father f = new Son();//子类构造时必然会调用父类的构造方法先
		System.out.println(f.getName());//方法是在运行时动态绑定
		System.out.println(f.name);//属性是在编译时指定(不会知道father指向了谁)
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值