Day11
代码一:
package oop.Demo10;
/**
* @author senko
* @date 2022/9/28 10:14
*/
//static
public class Student {
private static int age;//静态的变量
private double score;//非静态变量
public void run(){
go();//非静态方法可以直接调用静态方法的
}
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();
}
}
代码二:
package oop.Demo10;
/**
* @author senko
* @date 2022/9/28 10:27
*/
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 p = new Person();
}
}
代码三:
package oop.Demo10;
import static java.lang.Math.random; //静态导入包
import static java.lang.Math.PI;
/**
* @author senko
* @date 2022/9/28 10:35
*/
public class Test {
public static void main(String[] args) {
System.out.println(random());//使用静态导入包掉用的时候就不用写Math.random()
System.out.println(PI);//使用静态导入包掉用的时候就不用写Math.PI
}
}

浙公网安备 33010602011771号