简体   繁体   English

如果我已经有一个泛型的实现,如何声明一个接口方法

[英]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.

相关问题 通用接口方法和实现类 - generic interface method and implementation classes 如何在Java中使用泛型来创建一个泛型接口,该接口定义仅接受实现作为参数的方法? - How would I use generics in Java to create a generic interface that defines a method that only accepts the implementation as a parameter? Java - 如何将未知的接口实现传递给泛型方法 - Java - How to pass unknown implementation of interface to generic method 如何获取Java中通用接口实现的方法对象 - How to obtain method object for generic interface implementation in Java 如何将接口与通用类的实现绑定? - How do I bind the Interface with Implementation for Generic Classes? 如何在接口实现中使用泛型构造函数? - How can I use a generic constructor in interface implementation? 是否可以使用泛型返回类型定义接口方法,并且具体实现定义返回类型? - Is it possible to have an interface method defined with a generic return type and a concrete implementation define the return type? 如何在Java中声明泛型方法 - How to declare generic method in Java 如何调用具有多个实现的特定接口方法 - How to call a particular interface method that have multiple implementation 在接口中,我首先声明一个扩展接口的泛型,然后是一个扩展第一个泛型和另一个接口的泛型 - In an interface, I first declare a generic that extends an interface, then a generic that extends the first generic along with another interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM