在一个类中:
import java.util.*;
public class DemoSet{
static void fill(Set s){
s.addAll(Arrays.asList("one tow three four five".split(" ")));
}
public static void displays(Set e){
System.out.println(s.getClass().getName().replaceAll("\\w+\\.",""));
.....
}
.....
.....
}
}
Set 接口的实例s如何调用Object中的getClass(),因为Set(而且里面没有getClass()成员方法)是一个接口,不能继承Object类,为什么能调用Object类的成员方法
import java.util.*;
public class DemoSet{
static void fill(Set s){
s.addAll(Arrays.asList("one tow three four five".split(" ")));
}
public static void displays(Set e){
System.out.println(s.getClass().getName().replaceAll("\\w+\\.",""));
.....
}
.....
.....
}
}
Set 接口的实例s如何调用Object中的getClass(),因为Set(而且里面没有getClass()成员方法)是一个接口,不能继承Object类,为什么能调用Object类的成员方法
我觉得这位大哥的博文可以解释LZ所有的疑问,呵呵。
If an interface has no direct superinterfaces, then the interface implicitly declares a public
abstract member method m with signature s, return type r,and throws clause t corresponding to
each public instance method m with signature s, return type r, and throws clause t declared in
Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.