123jgh

导航

 
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();
}
}


//static
public class Student {
private static int age;//静态的变量
private double score;//非静态的变量

public static void go(){

}

public Student run(){
go();//非静态可以调用静态方法

return null;
}

public static void main(String[] args) {
//选择代码后 Alt+enter快捷键 即可new一个类new Student().run()
Student run = new Student().run();

go();

/*
Student s1 = new Student();

System.out.println(Student.age);//静态:类名.变量
//System.out.println(Student.score);//报错
System.out.println(s1.age);
System.out.println(s1.score);
*/
}
}


//(补充)静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;

public class Test {
public static void main(String[] args) {
//System.out.println(Math.random());//导入包之后就不用加Math了
System.out.println(random());//随机数

System.out.println(PI);
}
}



posted on 2022-10-13 10:32  江江要努力  阅读(24)  评论(0)    收藏  举报