为什么不能覆盖静态方法?
如果可能,请举例说明.
<?php
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who(); // Here comes Late Static Bindings
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test(); // Outputs "B"
?>
Run Code Online (Sandbox Code Playgroud)
我希望在Java中得到一个等价的东西......就像这样
class A {
public static void who(){
System.out.println("A");
};
public static void test(){
who(); //<<< How to implement a static:: thing here???
}
}
class B extends A {
public static void who(){
System.out.println("B"); …Run Code Online (Sandbox Code Playgroud) 我找不到任何好的来源来解释原因:
abstract class AA {
public static void log() {}
}
class BB extends AA {
public void log() {} //error
}
Run Code Online (Sandbox Code Playgroud)
interface CC {
public static void log() {}
}
class DD implements CC {
public void log() {} //Ok
}
Run Code Online (Sandbox Code Playgroud) 据我所知,在 Java 方法重载中,我们对所有重载方法使用相同的名称。而且,它们的返回类型也不是问题。但是如果我们使用与静态和非静态形式相同的方法会发生什么,如下例所示?我们可以考虑这个方法重载吗?
class Adder {
static int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
Run Code Online (Sandbox Code Playgroud)
class Test {
public static void main(String[] args) {
Adder a1 = new Adder();
System.out.println(Adder.add(11, 11));
System.out.println(a1.add(11, 11, 51));
}
}
Run Code Online (Sandbox Code Playgroud)
我读了一些文章,但他们没有澄清我的问题。
java ×4
static ×2
c++ ×1
interface ×1
methods ×1
overloading ×1
overriding ×1
php ×1
python ×1