所以如果我有两套:
Set<Integer> test1 = new HashSet<Integer>();
test1.add(1);
test1.add(2);
test1.add(3);
Set<Integer> test2 = new HashSet<Integer>();
test2.add(1);
test2.add(2);
test2.add(3);
test2.add(4);
test2.add(5);
Run Code Online (Sandbox Code Playgroud)
有没有办法比较它们只有一组4和5返回?
让我们考虑以下示例.
public interface SimpleInterface {
public void simpleMethod();
}
public class SimpleClass implements SimpleInterface{
public static void main(String[] args) {
SimpleInterface iRef = new SimpleClass();
SimpleClass cRef = new SimpleClass();
iRef.simpleMethod(); // Allowed. Calling methods defined in interface via interface reference.
cRef.specificMethod(); // Allowed. Calling class specific method via class reference.
iRef.specificMethod(); // Not allowed. Calling class specific method via interface reference.
iRef.notify(); // Allowed????
}
public void specificMethod(){}
@Override
public void simpleMethod() {}
}
Run Code Online (Sandbox Code Playgroud)
我想,在使用接口引用的Java中,我们可能只访问在此接口中定义的方法.但是,似乎允许通过任何接口引用访问类Object的方法.我的具体类"SimpleClass"继承了Object类所具有的所有方法,并且类Object确实没有实现任何接口(假设Object使用notify,wait等方法实现某些接口).我的问题是,为什么允许通过接口引用访问类Object的方法,同时考虑到我的具体类中的其他方法是不允许的?