Java_76_父类引用指向子类对象_extends

父类引用指向子类对象//为方便理解一下举例
例如:人类是父类,而子类就是中国人,美国人,俄罗斯人
也就是:
    人类 包含各项相同基础属性也就是父类,而各国人包含独特属性也就是子类。
    人类 = new 中国人(中国人各项属性)
    人类 = new 美国人(美国人各项属性)
    人类 = new 俄罗斯人(俄罗斯人各项属性)
objName instanceof obj  //检测是否是该类的对象,是true,否false
package Test;

public class Extends {
	public static void main(String[] args){
		Student p=new Student();
		p.rest();
		p.name="张三";
		Person ps=new Student();
		System.out.println(p instanceof Person);
		System.out.println(p instanceof Student);
		System.out.println(p instanceof Object);
	}
}
class Person{
	Person(int s){
		System.out.println("父类");
	}
	String name;
	int height;
	public void rest(){
		System.out.println("休息一会儿。");
	}
}
class Student extends Person{
	Student(){
		super(0);
		System.out.println("子类");
	}
//	String name;
//	int height;
	String major;
	public void study(){
		System.out.println("学习两小时。");
	}
//	public void rest(){
//		System.out.println("休息一会儿。");
//	}
}
发布了136 篇原创文章 · 获赞 11 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/pmcasp/article/details/104940448