package com.wuming.oop.demo07;public class Person {//2:赋初值{System.out.println("匿名代码块");}//1:只执行一次static{System.out.println("静态代码块");}//3public Person() {System.out.println("构造方法");}public static void main(String[] args) {Person person1 = new Person();//上一行执行完,输出顺序// 静态代码块//匿名代码块//构造方法System.out.println("=============");Person person2 = new Person();/*上一行执行完,输出顺序* 匿名代码块构造方法* */} }
package com.wuming.oop.demo07; //static public class Student{private static int age;//静态的变量 多线程!private double score;//非静态的变量public void run(){go();}public static void go(){//是静态方法, Student.go();s1.go(); go();调用ok,非静态方法可以调用静态方法第7行//run();静态方法不能调用非静态方法}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);Student.go();s1.go();go();// run();不能直接调用非静态方法new Student().run();}}
package com.wuming.oop.demo07;//静态导入包 import static java.lang.Math.random;//加static,拓展:Math是被final修饰的,凡是被final修饰的类不能被继承 import static java.lang.Math.PI; public class Test {public static void main(String[] args) {System.out.println(random());System.out.println(Math.random());//没有导包就这样写System.out.println(PI);} }