[英]How to declare an interface method if i already have an implementation with generic
I want to make EventsRepository class to implement an Interface. 我想创建EventsRepository类来实现一个接口。 So it has some method:
所以它有一些方法:
public class EventsRepository implements IModelLayer {
...
public void getEvents(ICallback<EventBrite> resultEvents) {
WebEventsDataSource.getInstance().getEvents(resultEvents);
}
}
and this method has to implement some interface. 而且这个方法必须实现一些接口。 On the basis of this method, what kind of interface method's semantic do i have to declare?
在这个方法的基础上,我必须声明什么样的接口方法的语义? I tried to declare next interface:
我试图声明下一个界面:
public interface IModelLayer<T> {
void getEvents(T resultEvents);
}
but it is not correct. 但这不正确。
Thanks a lot! 非常感谢!
The generic implementation and declaration would look like that: 通用实现和声明看起来像这样:
public interface IModelLayer<T> {
void getEvents(T resultEvents);
}
The interface is already right defined just the implementation of it needs to be changed. 接口已经正确定义,只需要更改它的实现。
Class implementation: 课程实施:
public class EventsRepository implements IModelLayer<ICallBack<EventBrite>> { //Generic type definition
...
@Override
public void getEvents(ICallback<EventBrite> resultEvents) {
WebEventsDataSource.getInstance().getEvents(resultEvents);
}
}
For example in other cases: 例如在其他情况下:
public class StringRepository implements IModelLayer<ICallBack<String>> { //Generic type definition
...
@Override
public void getEvents(ICallback<String> resultEvents) {
//todo do something with the callback stuff.
}
}
The thing here is you haven't defined the generic type you want to use. 这里的事情是你还没有定义你想要使用的泛型类型。 So you change
implements IModelLayer
to implements ModelLayer<YourType>
and the method will use the type you defined. 因此,您将
implements IModelLayer
更改为implements ModelLayer<YourType>
并且该方法将使用您定义的类型。
You are almost there, you forgot the type, try with: 你几乎就在那里,你忘记了类型,尝试:
public class EventsRepository implements IModelLayer<ICallback<EventBrite>>{
@Override
public void getEvents(ICallback<EventBrite> resultEvents) {
WebEventsDataSource.getInstance().getEvents(resultEvents);
}
}
implements like below..this may help 如下所示......这可能有所帮助
public class EventsRepository implements IModelLayer<ICallback<EventBrite>> {
public void getEvents(ICallback<EventBrite> resultEvents) {
WebEventsDataSource.getInstance().getEvents(resultEvents);
}
}
If getEvents
parameter is always an ICallback
, you can define the interface and class as follows: 如果
getEvents
参数始终是ICallback
,则可以按如下方式定义接口和类:
public interface IModelLayer<T> {
void getEvents(ICallback<T> resultEvents);
}
public class EventsRepository implements IModelLayer<EventBrite> {
...
public void getEvents(ICallback<EventBrite> resultEvents) {
WebEventsDataSource.getInstance().getEvents(resultEvents);
}
}
Of course, the definition of WebEventsDataSource.getInstance().getEvents()
might also affect the answer. 当然,
WebEventsDataSource.getInstance().getEvents()
的定义也可能会影响答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.