instanceof关键字、Static关键字、抽象类、接口、异常

instanceof

System.out.println(A instanceof B);
//当A和B存在父子关系时,输出为true

//Object->Person->Student
//Object->Person->Teacher
//Object->String
Person person = new Student();
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Student);//true
System.out.println(person instanceof Teacher);//false
//System.out.println(person instanceof String);//编译就报错

Static关键字

package com.oop.demo02;

public class Student {

    //3、最后加载对象的构造器
    public Student() {
        System.out.println("构造器");

    }

    //2、然后加载匿名代码块
    {
        System.out.println("匿名代码块");
    }

    //1、static和类一起加载,最先加载,整个过程只加载一次
    static {
        System.out.println("静态代码块");

    }

    public static void main(String[] args) {
        Student student = new Student();
        System.out.println("==============");
        Student student1 = new Student();

    }
}

输出结果为:

静态代码块
匿名代码块
构造器
==============
匿名代码块
构造器

抽象类

//抽象类,extends :单继承 (接口可以多继承)
public abstract class Person {
    //abstract,抽象方法,只有方法名字,没有方法实现
    public abstract  void study();
    
    //1.不能new这个抽象类,只能靠子类去实现它!
    //2.抽象类中可以写普通方法
    //3.抽象方法必须在抽象类中
}

//抽象类的所有方法,继承了他的子类,都必须要实现它的方法
public class Student extends Person {
    @Override
    public void study() {

    }
}

接口

public interface TimerService {
    void timer();
}


//interface定义关键字,接口都需要实现类
public interface UserService {
    //接口中的所有定义其实都是抽象的 public abstract
    void add(String name);
    void delete(String name);
    void update(String name);
    void query(String name);
}


//类实现接口
//利用接口实现多继承
public class UserServiceImpl implements UserService ,TimerService{
    @Override
    public void add(String name) {

    }

    @Override
    public void delete(String name) {

    }

    @Override
    public void update(String name) {

    }

    @Override
    public void query(String name) {

    }

    @Override
    public void timer() {

    }
}

接口的作用:

  1. 相当于一个约束,定义一些方法,让不同的人去实现

  2. 方法:public abstract

  3. 常量:public static final

  4. 接口不能被实例化,接口中没有构造方法

  5. implements可以实现多个接口

  6. 必须要重写接口中的方法

异常

public class demo01 {
    public static void main(String[] args) {
        int a=1,b=0;

        //快捷键:Ctrl + Alt + t
        //假设要捕获多个异常,从小到大
        try{ //try监控区域
            System.out.println(a/b);
        }catch (Error e){ //catch想捕获的异常类型
            System.out.println("Error");
        }catch (Exception e){
            System.out.println("Exception");
        }catch (Throwable t){
            System.out.println("Throwable");
        }finally { //处理善后工作
            System.out.println("finally");
        }
    }

    //假设方法中处理不了这个异常,在方法上抛出
    public void test(int a,int b) throws ArithmeticException{
        if (b==0){
            throw new ArithmeticException(); //主动抛出的异常,一般在方法中使用
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值