package itheima_04;
public class Demo01 {
private String name;
private int age;
public static void run(){
//go(); //静态方法不可以调用非静态方法
System.out.println("run");
}
public void go(){
run(); //非静态方法可以调用静态方法
System.out.println("go");
}
public static void main(String[] args) {
Demo01.run(); //静态方法可以用类名调用
run(); //在一个类中也可以直接调用静态方法
Demo01 demo01=new Demo01();
demo01.go(); //非静态方法必须创建对象才可以调用
}
}
package itheima_04;
public class Demo02 {
static {
System.out.println("静态代码块"); //最先执行-----只执行一次
}
{
System.out.println("匿名代码块"); //其次执行
}
public Demo02(){
System.out.println("构造方法"); //最后执行
}
public static void main(String[] args) {
Demo02 demo02=new Demo02();
System.out.println("----------------");
Demo02 demo03=new Demo02();
}
}
package itheima_04;
import static java.lang.Math.random; //静态导入包
public class Demo03 {
public static void main(String[] args) {
System.out.println(random());//静态导入包
}
}
浙公网安备 33010602011771号