我可以以某種方式部分實現一類泛型類型嗎?我想實現的是,只有當somone真的依賴這種類型時,我才能接受通用的。看到的是我要尋找一個示範的目的以下富/酒吧例如:我能以某種方式部分實現一類單類泛型嗎?
import java.util.Date;
public class Sample {
public static abstract class ASomeFunc<AK, AV, PK, PV> {
public void forwardTo(ASomeFunc<?, ?, PK, PV> lala) {
}
// EDIT 1:
// we do some logic here and then pass a map entry to an actual implementation
// but somethimes I do not care what the key is I am just interested in what the value is
// public abstract Map.Entry<PK, PV> compute(Map.Entry<AK, AV> data);
}
public static class SomeFunc2 extends ASomeFunc<Date, String, Number, Number> {
}
// what I would like to do:
// public static class SomeOtherFunc extends ASomeFunc<?, Number, ?, Number> {
// but I ony can:
public static class SomeOtherFunc extends ASomeFunc<Object, Number, Object, Number> {
}
public static void main(String[] args) {
// but this now clashes ... sinc object is explicitly defined
new SomeFunc2().forwardTo(new SomeOtherFunc());
}
}
您是否正在尋找C++中「模板特化」的等價物?如果積極,準備失望。搜索「type erasure Java泛型」 –
爲什麼子類不依賴於該類型?抽象類可能過於專業化了嗎? – Will
實際上它不會編譯,因爲根據'forwardTo'和'SomeFunc2'的類型,'SomeOtherFunc'預計爲'ASomeFunc <?,?,Number,Number>'類型,而其最後2個參數類型是'對象'和'數字' –