2
我有這種設計,我不確定它爲什麼不起作用。泛型有界類型參數
interface BaseType {}
interface TypeA extends BaseType {}
interface TypeB extends BaseType {}
interface Query<T extends BaseType> {
public String get();
}
interface Result<T extends BaseType> {
public String get();
}
interface Service<T extends BaseType> {
public Result<T> get(Query<T> query);
}
class SomeResult implements Result<TypeA> {
private String s;
public SomeResult(String s) { this.s = s; }
public String get() { return this.s; }
}
class SomeQuery implements Query<TypeA> {
public String get() { return "blah"; }
}
class SomeQuery2 implements Query<TypeA> {
public String get() { return "blah2"; }
}
class SomeService implements Service<TypeA> {
/** OK -- but notice the ambiguous parameter type */
/*
public SomeResult get(Query<TypeA> query) {
if (query instanceof SomeQuery) return new SomeResult(query.get());
else return null;
}
*/
/** NOT OK -- but this is the parameter I want to keep; notice SomeQuery IS-A Query<TypeA> */
public SomeResult get(SomeQuery query) { return new SomeResult(query.get()); };
/**
* Main.java:27: error: SomeService is not abstract and does not override abstract method get(Query<TypeA>) in Service
* class SomeService implements Service<TypeA> {
*^
* 1 error
*/
}
public class Main {
public static void main(String args[]) {
SomeQuery someQuery = new SomeQuery();
SomeQuery2 someQuery2 = new SomeQuery2();
SomeService someService = new SomeService();
System.out.println(someService.get(someQuery).get());
}
}
我是新來的泛型,並不完全明白我在這裏違反什麼合同。我希望服務能夠緊密綁定,即使我可以綁定返回類型,但似乎無法爲參數設置。這意味着,我需要在服務內部執行instanceof
檢查,以確保獲得正確的參數。我想避免這種情況。有任何想法嗎?
+1。或者,換句話說,'SomeService'是'服務'和'SomeQuery2''是'Query ',所以這個必須能夠工作:'服務 service = new SomeService(); service.get(new SomeQuery2());' –
謝謝。這是有道理的。 – bdas