当前位置: 技术问答>java相关
请问这段有关接口的英文怎么理解?SCJP
来源: 互联网 发布时间:2015-09-15
本文导语: It is also legal to have a return type that is an interface. This may seem inexplicable, because an interface is not a real object, but it fits in with Java’s object-oriented design. For example, a method can use Runnable as its return type...
It is also legal to have a return type that is an interface. This may seem inexplicable, because an interface is not a real object, but it fits in with Java’s object-oriented design. For example, a method can use Runnable as its return type even though a Thread object is being returned. This is because Thread implements the Runnable interface./** Keep in mind, however, that if the return type is a Thread, you will not be able to return an object just because it implements the Runnable interface. */Let’s take a look at a legal method with a declared return type of Runnable:
public Runnable getRunnable() {
Thread myThread = new Thread(this);
return myThread;
}
请问/** Keep in mind, however, that if the return type is a Thread, you will not be able to return an object just because it implements the Runnable interface. */中的这段话怎么理解?
public Runnable getRunnable() {
Thread myThread = new Thread(this);
return myThread;
}
请问/** Keep in mind, however, that if the return type is a Thread, you will not be able to return an object just because it implements the Runnable interface. */中的这段话怎么理解?
|
就是说如果函数返回类型为Thread,你就不能只是返回一个实现了Runnable接口的对象。其实就是必须是返回的对象实现了返回类型接口,不能反过来说。
|
学习