static

package com.oop.demo07;
//static
public class Student {

    private static int age;//静态变量
    private double score;//非静态变量

    public void run(){

    }
    public static void go(){

    }

    public static void main(String[] args) {
//        Student s1 = new Student();
//
//        System.out.println(Student.age);
//        //System.out.println(Student.score);  非静态不可以
//        System.out.println(s1.age);
//        System.out.println(s1.score);
        new Student().run();
        Student.go();
        go();//非静态方法可以调用静态方法里的东西
    }

}
package com.oop.demo07;

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 person1 = new Person();
        System.out.println("/****************");
        Person person2= new Person();

    }
}
package com.oop.demo07;
//静态导入包
import static java.lang.Math.random;
public class Test {
    public static void main(String[] args) {
        System.out.println(random());
    }
}
/*
用final后,不能被继承了,无儿子
 */

 

posted @ 2022-07-31 08:27  是貓阿啊  阅读(72)  评论(0)    收藏  举报