ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Javase06

2021-04-23 17:02:34  阅读:166  来源: 互联网

标签:instanceof System Javase06 println 异常 public out


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语句块去释放占用的资源

 

标签:instanceof,System,Javase06,println,异常,public,out
来源: https://www.cnblogs.com/yyj0702/p/14694624.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有