可能重复:
为什么Java不允许覆盖静态方法?
有没有合理的理由为什么人们希望派生类重写隐藏static基类的方法?
当在子类中重写静态方法时,我对它的行为感到困惑.
以下是代码:
public class SuperClass {
public static void staticMethod() {
System.out.println("SuperClass: inside staticMethod");
}
}
public class SubClass extends SuperClass {
//overriding the static method
public static void staticMethod() {
System.out.println("SubClass: inside staticMethod");
}
}
public class CheckClass {
public static void main(String[] args) {
SuperClass superClassWithSuperCons = new SuperClass();
SuperClass superClassWithSubCons = new SubClass();
SubClass subClassWithSubCons = new SubClass();
superClassWithSuperCons.staticMethod();
superClassWithSubCons.staticMethod();
subClassWithSubCons.staticMethod();
}
}
Below is the output which we are getting :
1) SuperClass: inside staticMethod
2) …Run Code Online (Sandbox Code Playgroud) 隐藏静态字段时,字段对子类的访问级别没有限制,它甚至可以是非静态的,也可以是其他数据类型.
另一方面,当隐藏静态方法时,从超类隐藏静态方法的子类中的静态方法可以允许比隐藏方法更多但不能更少的访问.
AFAIK,静态方法链接无论如何都是在编译时完成的,那么为什么会有这样的限制呢?
PS问题只是出于好奇.
根据Java,静态变量可以通过类名访问,但它们也可以通过类对象访问,即使Java没有建议它,它给出了相同的答案.
我知道变量只有一个副本,它的值对于所有对象和其他东西都是相同的.为什么Java建议使用类名而不是类对象?
abstract class AbstractCase2 {
abstract static void simpleMethod1();//giving error
}
class Case2 extends AbstractCase2 {
static void simpleMethod1() {
System.out.println("Within simpleMethod1");
}
public static void main(String args[]) {
simpleMethod1();
System.out.println("with AwM");
}
}
Run Code Online (Sandbox Code Playgroud)
得到错误:
C:\>javac Case2.java
Case2.java:8: error: illegal combination of modifiers: abstract and static
abstract static void simpleMethod1();
^
1 error
Run Code Online (Sandbox Code Playgroud) 我想做的就是创建一个类,当你扩展它时,你会自动获得getInstance类.问题是,当我扩展它时,我不能引用子类.我能看到的唯一方法是通过类型转换((ClassName)class.getInstance()),但它不是非常用户友好.有什么建议?
我有一个Preference类(模块),用于几个不同的应用程序.基本上它是首选项的缓存,因此系统不必一直调用后端.它类似于缓存,但有一些额外的细节,如isPreferenceSelected,有一些辅助方法等.
问题是我想在类中包含一个savePreference,以便任何使用它的人都可以覆盖该方法,无论是数据库,平面文件等等.关键是这个模块不想要照顾.问题是它不是一个抽象类,所以我不能覆盖静态方法,即使它是,我也不想创建一百万个实例,因为我不想每次都加载首选项.我也不能创建一个抽象的单例.
因此我不知道该怎么做.这是我想要评论的代码片段:
// Please ignore the missing Generics, etc.
public class Preference
{
private static HashMap preferences = new HashMap();
public static ...
// Some preferences are objects, such as images, etc.
public static setPreference(String name, Object value)
{
.. some helper code
preferences.put(name, value); // ignoring issues with if it already exists ;)
savePreference(name, value); // saves to database, flatfile, etc.
}
}
Run Code Online (Sandbox Code Playgroud)
这是不同系统利用的核心类/代码.现在我想做的是在webapp,桌面应用程序等中说,能够在我的代码中使用这个类,例如:
public someFunction(...)
{
.. do some cool code
Preference.savePreference("logoImage", image); …Run Code Online (Sandbox Code Playgroud) 我很困惑为什么不允许以下内容:
public interface MyInterface {
MyInterface getInstance(String name);
}
public class MyImplementation implements MyInterface {
public MyImplementation(String name) {
}
@Override
public static MyInterface getInstance(String name) { // static is not allowed here
return new MyImplementation(name)
}
}
Run Code Online (Sandbox Code Playgroud)
我理解为什么接口中的方法不能是静态的,但为什么不能覆盖这个方法呢?
我希望所有类都实现该getInstance(String name)方法,但是我目前仅限于只有在对象已经被实例化时才能调用该方法哪种类型会失败...
*更新:*感谢您的回答,我现在明白了.基本上我不应该试图使实用程序类(或工厂类)实现一个接口(或至少,不是这样)...
我知道孩子不能降低非静态方法的可视性,而且我理解为什么会这样。
但是,我读过“可以通过重新声明静态方法来隐藏它”。但是我不明白如何用Java实现。
这真的有可能吗?如果是,该如何做(代码示例),为什么要引入它(这似乎与不降低接口可见性的原理相矛盾)?
让我们假设我有这样的代码:
public interface JustAnInterface {
void doSomething();
}
public class JustAnInterfaceImplementation implements JustAnInterface {
@Override
public static void doSomething() {
}
}
Run Code Online (Sandbox Code Playgroud)
为什么static doSomething()方法显示错误" 方法不覆盖其超类方法 "?
java ×10
static ×6
inheritance ×3
abstract ×1
extends ×1
interface ×1
oop ×1
overriding ×1
singleton ×1