Java 中抽象类可以有静态方法吗
可以。java 允许在抽象类中定义静态方法,调用时通过抽象类名直接访问,无需实例化对象。

子类能否重写抽象类的静态方法
不能。静态方法属于类,不支持多态,子类同名方法只是隐藏(Shadowing)而非重写。

public abstract class Base {
public static void show() {
System.out.println("Base Static");
}
}
public class Child extends Base {
public static void show() {
System.out.println("Child Static");
}
public static void main(String[] args) {
Base b = new Child();
b.show(); // 输出: Base Static
Child c = new Child();
c.show(); // 输出: Child Static
}
}
抽象类静态方法能访问非静态成员吗
不能。静态方法没有 `this` 引用,无法直接访问抽象类中的非静态字段或方法。
public abstract class Data {
private String name;
public static void printName() {
System.out.println(name); // 编译错误:Cannot make a static reference to the non-static field name
}
}
Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南











