[英]Java Reflection on Interfaces
我主要的問題是關於如何創建一個“動態”方法,該方法可以覆蓋不同的對象。
所以有了這個界面
public interface GenericInterface{
public List<Object> getAll() throws Exception;
public Object addObject(Object object) throws Exception; // This should use reflection
}
現在在另一個接口中擴展GenericInterface
public interface CarInterface extends GenericInterface{
@Override
List<Object> getAll() throws Exception;
@Override
public Object addObject(Object object) throws Exception;
public Object newSpecificMethod() throws Exception;
}
現在有一個公開課
pulic Car implements CarInterface{
//...
@Override
public Object addObject(Object object) throws
//.. here some logic
return object;
}
//...
}
那么如何才能“覆蓋”方法的名稱?
而不是
Car.addObject(對象);
我想擁有
Car.addCar((Car)object);
我知道這與反射有關,但我不知道該怎么做。 有什么建議嗎?
使用通用:
public interface GenericInterface<T>{
public List<T> getAll() throws Exception;
public T addObject(T object) throws Exception;
}
public interface CarInterface extends GenericInterface<T>{
public Object newSpecificMethod() throws Exception;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.