public class Student extends Person{
private static int age;//静态的变量
private double score;//非静态的变量
public void run(){
go();//非静态方法可以调用静态方法
}
public static void go(){
}
public static void main(String[] args) {
Student student = new Student();
System.out.println(Student.age);
System.out.println(student.age);
System.out.println(student.age);
student.go();//通过对象来调用go方法
Student.go();//通过当前Student类调用go方法
go();//通过当前Student类调用go方法
//静态方法可以调用静态方法
//run();静态方法不能调用非静态方法
}
}
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();
}
}
//执行结果
/*
静态代码块
匿名代码块
构造方法
=====================
匿名代码块
构造方法
*/
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Text{
public static void main(String[] args) {
//System.out.println(Math.random());
System.out.println(random());
System.out.println(PI);
}
}
//被final定义的类不能被继承:final之后断子绝孙