【发布时间】:2008-11-21 08:43:35
【问题描述】:
我想听听你们关于在设计 API 时如何决定何时应该使用具体参数化类型与有界参数化类型,尤其是。 (我最关心的)定义一个类/接口。
例如,
public interface Event<S>{
void setSource(S s);
}
public interface UserEvent extends EVent<User> // OR: UserEvent<S extends User> extends Event<S>
// It will therefore be void setSource(User s);
}
使用具体参数的问题是,我无法将使用 setSource() 时获得的编译时好处带到新接口,例如,
public interface AdminUserEvent extends UserEvent{
void setSource(AdminUser s); // WHERE: AdminUser extends User. This is a method overloading, we also have a void setSource(User s) inherited from UserEvent.
}
我可以解决的办法是在调用 AdminUserEvent.setSource() 时对 User 对象进行类型检查。
您在设计 API 时是否曾提出过这个问题?当这种情况出现时,你会采取什么做法或规则?谢谢。
yc
【问题讨论】: