直接上代码了
//DeviceRepository类
public abstract class DeviceRepository<T extends Device> extends DomainRepository<T>
//DeviceEventHandler类第一种写法
public abstract class DeviceEventHandler<M extends Device,R extends DeviceRepository<M>>
//DeviceEventHandler的子类针对第一种写法的继承
public class DefaultSmartControllerEventHandler extends DeviceEventHandler<SmartController, SmartControllerRepository>
//DeviceEventHandler类第二种写法
public abstract class DeviceEventHandler<R extends DeviceRepository<? extends Device>>
//DeviceEventHandler的子类针对第二种写法的继承
public class DefaultSmartControllerEventHandler extends DeviceEventHandler<SmartControllerRepository>
这两种写法在Eclipse中都编译通过了,但我不清楚他们之间有什么不同,如果哪位朋友知道请告知我,谢谢。
另外,针对第二种写法,我在DeviceEventHandler中通过R获取的是具体的Device类的对象,而第一种写法似乎可以写成通过R获取M类的对象。
根据情况来看,第一种写法是适合的,因为一般Domain中的Model是从Repository中取出的,所以第一种写法中的Model可以根据M来变化,而第二种写法会麻烦一些,因为不论Repository中取出的是什么Model,最终只能被转型为Device,这在通常情况下是不可接受的。
另外,针对第二种写法,我在DeviceEventHandler中通过R获取的是具体的Device类的对象,而第一种写法似乎可以写成通过R获取M类的对象。
泛型的定义和使用
http://blog.csdn.net/lonelyroamer/article/details/7864531