1、static 方法,可以被继承,不能被重写
package com.knowledge.myStatic;
public class Mystatic {
public static void main(String[] args) {
// 通过类名.变量名访问
// 说明:类变量是随着类的加载而创建的,所以即使没有实例化对象也能访问
// 说明:类加载的时间:
// 1.实例化对象时
// 2.通过类名调用静态变量的时候(类名.class除外)
System.out.println(A.name);
// 类方法使用
Student tom = new Student("tom");
tom.payFee(100);
Student marry = new Student("marry");
marry.payFee(200);
Student john = new Student("john");
Student.payFee(300);
Student.showFee();
}
}
class Student{
public String name;
private static double fee=0;
public Student(String name) {
this.name = name;
}
public static void payFee(double fee){
// 静态方法可以访问静态属性
Student.fee += fee;
}
public static void showFee(){
System.out.println("总费用: " + Student.fee);
}
}
class A{
// 定义一个类变量
public static String name = "123";
}



