简体   繁体   中英

Creating two generic parameters in an interface

I am trying to create a generic interface for my classes that will satisfy them all. I have created base classes to link them all together but the interface I'm trying to create isn't working like I'd expect it to and I cant find the right words to type into the search to find them.

The interface so far is:

public <T extends SDOBase> T entityToSDO(<? extends BaseEntity> entity, T sdo) throws Exception;

How do I make entity a second generic type?

尝试以这种方式声明您的方法:

public <T extends SDOBase, E extends BaseEntity> T entityToSDO(E entity, T sdo) throws Exception;

I think you want something more along the lines of:

public interface MyInterface<T extends SDOBase>
{
    public T entityToSDO(<? extends BaseEntity> entity, T sdo) throws Exception;
}

You just need

public <T extends SDOBase> T entityToSDO(BaseEntity entity, T sdo) throws Exception;

Anything that is an instance of a subtype of BaseEntity automatically is an instance of BaseEntity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM