我试图覆盖从父类继承抽象方法的子类中的方法。我试图更改通用返回类型,但我有点困惑。
基础通用类:
public abstract class BaseAdapter<T extends IEvent> {
private List<T> events;
public BaseAdapter(List<T> events) {
this.events = events;
}
public List<T> sort() {
// Some sorting...
return newFilteredEvents;
}
}
儿童班:
public class AdapterEvent extends BaseAdapter<Event> {
public AdapterEvents(List<Event> events) {
super(events);
}
}
实施:
public abstract class BaseView extends View {
private BaseAdapter<IEvent> events;
@Override
protected View onCreate(Context context) {
View view = super.onCreate(context);
this.events = getAdapter();
return view;
}
/**
* The child class should provide an adapter
*/
protected abstract BaseAdapter<IEvent> getAdapter();
}
我希望覆盖参数化方法的子View类:
public class EventView extends BaseView {
@Override
protected View onCreate(Context context) {
return super.onCreate(context):
}
@Override
protected BaseAdapter<IEvent> getAdapter() {
List<Event> events = EventManager.getInstance().getEvents();
return new AdapterEvent(events);
}
}
这是Eclipse抛出错误消息的地方:
Type mismatch: cannot convert from AdapterEvents to BaseAdapter<IEvent>
注意类和接口:Event和IEvent:
public interface IEvent {
public void setId(long id);
public long getId();
public void setStartMillis(long start);
public long getStartMillis();
public void setEndMillis(long end);
public long getEndMillis();
}
继承自IEvent
public class Event implements IEvent {
private long id;
private long startTime, endTime;
@Override
public void setId(long id) {
this.id = id;
}
@Override
public long getId() {
return this.id;
}
// The other inherited methods...
}
我想要实现的是编写更抽象的代码,因为这些类,接口将由多个类扩展(在我的情况下,不同的View类型)。这些是抽象类:BaseAdapter
,BaseView
,IEvent
,这些是实现:AdapterEvent
,EventView
,Event
。