package Demo04;
?
public class Student extends Person {
public static int age;//静态属性
public String name;//非静态属性
?
public static void main(String[] args) {
Student student=new Student();
//静态和非静态属性都可以通过new一个对象进行调用
System.out.println(student.name);
System.out.println(student.age);
//静态方法可以通过类直接调用
System.out.println(Student.age);
//因为在此类中甚至可以直接省略类
System.out.println(age);
}
}
?
package Demo04;
?
public class Student extends Person {
public static void go(){//静态方法
System.out.println("go");
}
public void run(){//非静态方法
System.out.println("run");
}
?
public static void main(String[] args) {
Student student = new Student();
//静态和非静态方法都可以通过对象来调用
student.go();
student.run();
//静态方法的调用与静态属性的调用类似
Student.go();//通过类来调用
go();//因为在此类中直接调用
?
}
/*
静态方法只能调用静态方法
非静态方法都可以调用
*/
}
?
package Demo04;
?
public class Teacher extends Person{
{//第二个加载
System.out.println("匿名代码块");
}
static {//第一个加载且只加载一次
System.out.println("静态代码块");
}
?
public Teacher() {//最后加载
System.out.println("无参构造");
}
?
public static void main(String[] args) {
Teacher teacher = new Teacher();
System.out.println("================");
Teacher teacher2 = new Teacher();
?
?
}
}
?
package Demo04;
//静态导入包
import static java.lang.Math.random;
public class Teacher extends Person{
?
?
public static void main(String[] args) {
System.out.println(random());
//加入静态导入包导入的方法后,可以直接调用方法
//不用写Math.random()了,但是调用其他的Math的方法还是需要调用
?
?
}
}
?
原文:https://www.cnblogs.com/continue-student/p/14545452.html