static详解

package com.oop.Static;
//静态导入包~  很少用
import static java.lang.Math.random;
import static java.lang.Math.PI;

public class Application {
    public static void main(String[] args) {
        System.out.println(random());
        System.out.println(PI);
    }


}

package com.oop.Static;
//static
public class Student {
    private static int age;//静态的变量   多线程!
    private double score;//非静态的变量
    public void run(){
        System.out.println("run");
    }
    public static void go (){
        System.out.println("go");

    }

    public static void main(String[] args) {
        go();
        new Student().run();
    }



}

 

package com.oop.Static;

public class Person {
    //2: 赋初值~
    {
        System.out.println("匿名代码块");
    }
    // 1:  只执行一次   和类一起加载
    static {
        System.out.println("静态代码块");
    }
    // 3
    public Person() {
        System.out.println("构造方法");
    }

    public static void main(String[] args) {
        Person person = new Person();
        System.out.println("=============");
        Person person1 = new Person();
        //看一下执行顺序
    }


}

 

 

 



 

posted @ 2022-04-07 19:21  HFUUwzy  阅读(54)  评论(0编辑  收藏  举报