package cn;
/**
* 继承,instanceof 关键字的使用
* @author 110
*继承使用要点
*1:父类也成为超类,基类,派生类
*2:java中只有单继承,没有像C++那样的多继承。多继承会引起混乱,
*使得继承过于复杂,系统难以维护。
*3:java中没有多继承,接口有多继承。
*4:子类继承父类,可以得到父类的全部属性和方法(除了父类的构造方法),
*但不见得可以直接访问(比如,父类私有的属性和方法)。
*5:如果定义一个类时,没有调用entends,则它的父类时:java.lang.Object。
*可用ctrl+T查看类的继承层次结构。
*/
public class Jicheng {
public static void main(String[] args) {
Student s = new Student("郭能能",180,"英语");
s.rest();
Student s1= new Student("xixi",6,"wajuehji");
System.out.println(s1 instanceof Student);
//instanceof关键字判断左边的对象是不是右边类的对象,该结果返回为true
System.out.println(s1 instanceof Person);
//返回值也为true
System.out.println(s1 instanceof Object);
//返回值也为true
System.out.println(new Person() instanceof Student);
//返回值为false
System.out.println(new Student("xixi",6,"wajuehji") instanceof Person);
//返回值为true
}
}
class Person {
String name;
int height;
public void rest(){
System.out.println("休息一会!");
}
}
//使用entends就可以进行继承,
class Student extends Person{
String name;
int height;
String major;
public void study(){
System.out.println("学习两个小时");
}
public Student(String name,int height,String major) {
this.name=name;
this.height=height;
this.major=major;
}
}