菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
279
0

Javase06

原创
05/13 14:22
阅读数 31157

1.instanceof  判断两个是否存在继承关系

public class Demo01 {

    public static void main(String[] args) {
        //Object>String
        //Object>Person>Teacher
        //Object>Person>Student
        Object o1 = new Student();
        System.out.println(o1 instanceof Student);//True
        System.out.println(o1 instanceof Person);//True
        System.out.println(o1 instanceof Object);//True
        System.out.println(o1 instanceof Teacher);//False
        System.out.println(o1 instanceof String);//False
        System.out.println("===========================");
        Person p = new Student();
        System.out.println(p instanceof Student);//True
        System.out.println(p instanceof Person);//True
        System.out.println(p instanceof Object);//True
        System.out.println(p instanceof Teacher);//False
        //System.out.println(p instanceof String);//编译报错,有继承关系才会编译通过
        System.out.println("===========================");
        Student s = new Student();
        System.out.println(s instanceof Student);//True
        System.out.println(s instanceof Person);//True
        System.out.println(s instanceof Object);//True
        //System.out.println(s instanceof Teacher);//编译报错
        //System.out.println(s instanceof String);//编译报错



    }

}

2.Static详解

  (1)静态变量或者方法可以直接用类名调用,也成类变量或方法;

  (2)静态变量或方法只执行一次,可以用来初始化;

3.抽象

  (1)抽象类的抽象方法必须得由子类实现

  (2)抽象类本质是类,只能单继承

  (3)抽象类不能实例化,只能由子类实现

  (4)抽象类可以写普通方法,抽象方法必须在抽象类中

4.接口(Interface)

  (1)接口是规范;

  (2)接口中的方法都是public abstract,可以不用写;

  (3)类 implements 接口;

  (4)接口中定义的变量是常量(public static final);

  (5)接口不能被实例化,没有构造方法

5.内部类

  (1)成员内部类可以获得外部类private的变量

  (2)局部内部类

  (3)匿名内部类,不用将实例保存到变量中

6.异常(分为运行时异常和非运行时异常)(异常要抛出和捕获)

 

 

 

public static void main(String[] args) {
        int a = 1;
        int b = 0;
        try{//监控区域
            System.out.println(a / b);
        }catch (ArithmeticException e){//有异常才会执行
            System.out.println("程序出现异常,变量b不能为0");
        }finally {//无论有没有异常都会执行,善后工作,可以不写
            System.out.println("finally");
        }

    }

选中一行代码按 Ctrl+Alt+t 自动生成try catch

if(b == 0){//主动的抛出异常
                throw new ArithmeticException();
            }
 public void fun(int a,int b){
        if(b == 0){//主动的抛出异常
            throw new ArithmeticException();//一般用在方法中
        }
        System.out.println(a / b);
    }
public static void main(String[] args) {

        try {
            new Demo01().fun(1,0);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }

    }


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

7.自定义异常

package 异常.Demo01;

public class Test {

    //可能会存在异常的方法

    static void test(int a) throws MyException {

        System.out.println("传递的参数为:" + a);

        if (a > 10){
            throw new MyException(a);//抛出
        }
        System.out.println("ok!");
    }

    public static void main(String[] args) {
        try {
            test(110);
        } catch (MyException e) {
            System.out.println("MyException->" + e);
        }
    }

}
package 异常.Demo01;

public class MyException extends Exception{

    //传递数字>10抛出异常
    private int detail;

    public MyException(int detaila){
        this.detail = detaila;
    }

    //toString:异常的打印信息
    @Override
    public String toString() {
        return "MyException{" +
                "detail=" + detail +
                '}';
    }
}

8.异常总结

  (1)处理运行时异常时,采用逻辑去合理规避同时辅助try-catch处理

  (2)在多重catch块后面,可以加一个catch(Exception)来处理可能会被遗漏的异常

  (3)对于不确定的代码,也可以加上try-catch,处理潜在的异常

  (4)尽量去处理异常,切记只是简单的调用printStackTrace()去打印输出

  (5)具体如何处理异常,要根据不同的业务需求和异常类型去决定

  (6)尽量添加finally语句块去释放占用的资源

 

发表评论

0/200
279 点赞
0 评论
收藏