2021-10-16(java)

instanceof:

//Object > String

//Objiect > Father > Son1

//Objiect > Father > Son2

Object o = new Son1();

o instanceof Son1              true

o instanceof Father             true

o instanceof Object              true

o instanceof Son2                false

o instanceof String              false

static:(静态 类加载顺序)

        变量:静态变量,可以直接用类名访问。

        方法:静态方法,可以直接调用

public class Demo {
    public static void main(String[] args) {
        Demo demo = new Demo();
    }
    {
        System.out.println("匿名代码块");                 //  2    赋初始值
    }
    static{
        System.out.println("静态代码块");               //  1   只执行一次
    }
    public Demo() { 
        System.out.println("构造方法");               //   3
    }
}

执行速度:静态代码块 > 匿名代码块 > 构造方法

静态导入包:import static java.lang.Math.random (可以直接使用)

抽象类:(abstract)

        抽象类的所有方法,继承它的子类,必须实现它所有的抽象方法。

接口:(interface)

        接口中所有的定义都是抽象的

        接口不能被实例化

void run();     相当于   public abstract void run();

int age = 10;   相当于  public static final int age;

内部类:

        成员内部类:内部类可以获得外部类的私有属性和方法。

public class Outer {
    private int id=1;
    public void out(){
        System.out.println("out");
    }
    public class Inner{
        public void in(){
            System.out.println("inner");
        }
        public void getID(){
            System.out.println(id);
            out();
        }
    }

}
public class app {
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.out();
        Outer.Inner inner = outer.new Inner();
        inner.in();
        inner.getID
    }
}

异常:(try、catch、finally、throw、throws)

         Throwable:错误:Error

                                        一般由java虚拟机生成抛出,一般不是编写。

                              异常:Exception

                                        通常是能被解决的。

        如果try代码块出现catch括号里面的错误,则会执行catch代码块里面的代码。

        finally无论是否异常最后都执行。

        throw主动抛出异常。(抛出不是处理,捕获要处理)。

        throws方法上抛出异常。

自定义异常类:

        继承Exception

        

猜你喜欢

转载自blog.csdn.net/qq_45688193/article/details/120796899