2021-09-21

instance()方法

判断两个类之间是否存在父子关系
1能否编译通过,看X和Y是否存在父子关系,存在则可以;
2结果是T OR F,看引用对象,如果X引用的对象是Y的子类,则为ture;

  **   //object >> person >> student
        //object >> person >> teacher**
         Object s1 = new student();
        System.out.println(s1 instanceof  person);
        System.out.println(s1 instanceof  student);
        System.out.println(s1 instanceof  Object);
        System.out.println(s1 instanceof  teacher);
        System.out.println(s1 instanceof  String);
        System.out.println("===========================================");
        person s2 = new student();
        System.out.println(s2 instanceof  person);
        System.out.println(s2 instanceof  student);
        System.out.println(s2 instanceof  Object);
        System.out.println(s2 instanceof  teacher);
        //person类和string不属于同一分支,比较则会报错
        //System.out.println(s2 instanceof  String);//报错
        System.out.println("===========================================");
        student s3 = new student();
        System.out.println(s2 instanceof  person);
        System.out.println(s2 instanceof  student);
        System.out.println(s2 instanceof  Object);
        //System.out.println(s2 instanceof  teacher);报错

转换

父转子需要强制转换,子转父不需要,直接转,但可能丢失自己本来的方法

静态static

1:静态的方法和属性可以直接调用,非静态的方法和属性则需要创建对象去调用
2:静态代码块:在构造其前执行,顺序为静态代码块,匿名代码块,构造方法,static代码块只执行一次,匿名代码块可以赋初值

````java
public class person {
    {
        System.out.println("匿名代码快");
    }
    static{
        System.out.println("静态代码块");
    }

    public person() {
        System.out.println("gouzaoqi");
    }
public class application {
    public static void main(String[] args) {
       person pes = new person();
        System.out.println("====================================");
        person pes1 = new person();
    }
}

`静态代码块
匿名代码快
gouzaoqi
====================================
匿名代码快
gouzaoqi


抽象类接口

抽象类:
抽象类中方法可以写抽象方法,也可以写普通方法,但不能被创建,可以被继承,继承抽象类时,需要将其的抽象方法重写
接口:
interface,就像人间的法律,制定后大家都遵守
接口中的方法都是抽象类,即写方法时,可将public abstract 省略;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值