in class B :public static void mothodThree(){} 称为隐藏 或叫做 hidden public void mothodTwo(){} 叫做覆盖public static void mothodOne(){} public void mothodFour(){} 编译更本不会通过 static 方法和实例方法之间不能互相覆盖隐藏 是专门针对 static 方法而言的 static 方法只能被 “覆盖” 为 static 的方法 而这个操作并不能叫做覆盖 因为原来 A 的 mothodThree() 还是可以被以 static 的方式调用 所以之称为 hidden
查了一下 The Java™ Language Specification Second Edition 有这么一段8.4.8.5 Example: Invocation of Hidden Class Methods A hidden class (static) method can be invoked by using a reference whose type is the class that actually contains the declaration of the method. In this respect, hiding of static methods is different from overriding of instance methods. The example:class Super { static String greeting() { return "Goodnight"; } String name() { return "Richard"; } } class Sub extends Super { static String greeting() { return "Hello"; } String name() { return "Dick"; } } class Test { public static void main(String[] args) { Super s = new Sub(); System.out.println(s.greeting() + ", " + s.name()); } }produces the output: Goodnight, Dickbecause the invocation of greeting uses the type of s, namely Super, to figure out, at compile time, which class method to invoke, whereas the invocation of name uses the class of s, namely Sub, to figure out, at run time, which instance method to invoke.看来要多看
上面那段贴错了 应该是这段 不好意思 8.4.6.2 Hiding (by Class Methods) If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method. In this respect, hiding of methods differs from hiding of fields (§8.3), for it is permissible for a static variable to hide an instance variable. Hiding is also distinct from shadowing (§6.3.1) and obscuring (§6.3.2). A hidden method can be accessed by using a qualified name or by using a method invocation expression (§15.12) that contains the keyword super or a cast to a superclass type. In this respect, hiding of methods is similar to hiding of fields.
称为隐藏 或叫做 hidden public void mothodTwo(){}
叫做覆盖public static void mothodOne(){}
public void mothodFour(){}
编译更本不会通过 static 方法和实例方法之间不能互相覆盖隐藏 是专门针对 static 方法而言的
static 方法只能被 “覆盖” 为 static 的方法
而这个操作并不能叫做覆盖 因为原来 A 的 mothodThree() 还是可以被以 static 的方式调用
所以之称为 hidden
The Java™
Language Specification
Second Edition
有这么一段8.4.8.5 Example: Invocation of Hidden Class Methods
A hidden class (static) method can be invoked by using a reference whose type
is the class that actually contains the declaration of the method. In this respect,
hiding of static methods is different from overriding of instance methods. The
example:class Super {
static String greeting() { return "Goodnight"; }
String name() { return "Richard"; }
}
class Sub extends Super {
static String greeting() { return "Hello"; }
String name() { return "Dick"; }
}
class Test {
public static void main(String[] args) {
Super s = new Sub();
System.out.println(s.greeting() + ", " + s.name());
}
}produces the output:
Goodnight, Dickbecause the invocation of greeting uses the type of s, namely Super, to figure
out, at compile time, which class method to invoke, whereas the invocation of
name uses the class of s, namely Sub, to figure out, at run time, which instance
method to invoke.看来要多看
应该是这段 不好意思
8.4.6.2 Hiding (by Class Methods)
If a class declares a static method, then the declaration of that method is said to
hide any and all methods with the same signature in the superclasses and superinterfaces
of the class that would otherwise be accessible to code in the class. A
compile-time error occurs if a static method hides an instance method.
In this respect, hiding of methods differs from hiding of fields (§8.3), for it is
permissible for a static variable to hide an instance variable. Hiding is also distinct
from shadowing (§6.3.1) and obscuring (§6.3.2).
A hidden method can be accessed by using a qualified name or by using a
method invocation expression (§15.12) that contains the keyword super or a cast
to a superclass type. In this respect, hiding of methods is similar to hiding of
fields.