package jsh;
/**
* 测试继承
* 测试instanceof 的用法
* @author Jsh
*
*/
public class TestExtend {
public static void main(String[] args) {
Student stu = new Student();
stu.name = "季少华";
stu.height = 180; //注意看Student类,里面其实没有name与height两个属性,但是继承了Person类
stu.rest(); //Srudent里面也没有rest()方法却可以调用,也是继承来的
//测试instanceof
System.out.println(stu2 instanceof Student);//true
System.out.println(new Person() instanceof Student);//false
//instanceof左边对象,右边类
//当对象为右面类或子类所创建的对象时,但会true,否则返回false
}
}
class Person/*extends Object*/{
String name;
int height;
public void rest() {
System.out.println("休息一会");
}
}
class Student extends Person{
//String name;
//int height; //两个类里面有相同的东西,可以用继承来实现简便
String major;
public void study() {
System.out.println("学习两小时");
}
/*public void rest() {
System.out.println("休息一会 ");
}*/
}
09-12