[英]Java: Using generics to implement a class that operates on different kinds of numbers
[英]Is there a way in Java generics to return different kinds of datatype?
所以我正在嘗試為 Java 中的某些 DSL 開發一個流暢的界面,但遇到了問題。 該接口由構建器模式類組成,每個構建器模式類都構成 DSL 的一部分。 問題是特定的構建器有時需要將控制權轉移到不同的構建器,而后者在某個時候返回到“父”構建器。 例如,有一個創建語句列表的“SequenceBuilder”,但它有時需要將控制權轉移到用於創建“if”語句的“IfBuilder”。 當 IfBuilder 完成后,它需要返回到 SequenceBuilder。 現在有些構建器並不總是由相同類型的其他構建器調用,因此需要能夠返回不同數據類型的構建器。 下面的示例程序演示了這一點:
package com.example.fluent;
public class Test {
public class Type1 {
public Type1 test1() {
System.out.println("test1");
return this;
}
public Type3 gotype3() {
System.out.println("gotype3");
return new Type3<Type1>(this);
}
public void endtype1() {
System.out.println("endtype1");
}
}
public class Type2 {
public Type2 test2() {
System.out.println("test2");
return this;
}
public Type3 gotype3() {
System.out.println("gotype3");
return new Type3<Type2>(this);
}
public void endtype2() {
System.out.println("endtype2");
}
}
public class Type3<T> {
private T parent;
public Type3(T parent) {
this.parent = parent;
}
public Type3 test3() {
System.out.println("test3");
return this;
}
public T endtype3() {
System.out.println("endtype3");
return parent;
}
}
public static void main(String[] args) {
new Test().run();
}
private void run() {
new Type1()
.test1()
.gotype3()
.test3()
.endtype3()
.test1()
.endtype1();
}
}
您可以在 .run() 方法中看到,我首先創建 Type1 class 的新實例,它遵循構建器模式。 在某些時候,我正在調用 .goType3() 方法,該方法將控制權轉移給 Type3 構建器。 因為它必須在某個時候再次將控制返回給 Type1,所以對該構建器的引用通過構造函數傳遞給 Type3。 當返回到 Type1 時,將調用 method.endtype3()。 這就是問題所在。 我正在使用 generics 來(嘗試)返回 Type1 的數據類型,但它被轉換為 Object 類型。 該數據類型顯然沒有 Type1 具有的方法,因此模式被破壞了。
問題:是否有其他方法可以將正確的數據類型返回給父構建器?
您沒有按照自己的意願使用 generics。 您正在使用返回類型Type3
。 您需要改用Type3<Type1>
、 Type3<Type2>
和Type3<T>
。
多虧了 Rob Spoor,我的代碼終於可以運行了,盡管起初我並不理解。 解決方案在於通過以下方式更改代碼:
package com.example.fluent;
public class Test {
public class Type1 {
public Type1 test1() {
System.out.println("test1");
return this;
}
public Type3<Type1> gotype3() {
System.out.println("gotype3");
return new Type3<Type1>(this);
}
public void endtype1() {
System.out.println("endtype1");
}
}
public class Type2 {
public Type2 test2() {
System.out.println("test2");
return this;
}
public Type3<Type2> gotype3() {
System.out.println("gotype3");
return new Type3<Type2>(this);
}
public void endtype2() {
System.out.println("endtype2");
}
}
public class Type3<T> {
private T parent;
public Type3(T parent) {
this.parent = parent;
}
public Type3<T> test3() {
System.out.println("test3");
return this;
}
public T endtype3() {
System.out.println("endtype3");
return parent;
}
}
public static void main(String[] args) {
new Test().run();
}
private void run() {
// new Type1().test1().test1().endtype1();
new Type1().test1().gotype3().test3().endtype3().test1().endtype1();
// new Type2().test2().gotype3().test3().endtype3().test2().endtype2();
}
}
查看Type1和Type2類中.gotype3()方法的返回類型的變化,以及.test3()方法的返回類型。 現在一切正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.