继承,instanceof 关键字的使用

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;
    }
    
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值