为什么构建器丢失了类型?

huangapple 未分类评论55阅读模式
英文:

Why builder lost type?

问题

以下是翻译好的内容:

  1. 当我尝试这样做时
  2. public class Example<T> {
  3. private T value;
  4. private Example() {
  5. }
  6. public T getValue() {
  7. return value;
  8. }
  9. public void setValue(T value) {
  10. this.value = value;
  11. }
  12. public static <T> Builder<T> builder() {
  13. return new Builder<>();
  14. }
  15. public static class Builder<T> {
  16. public Example<T> build() {
  17. return new Example<>();
  18. }
  19. }
  20. public static void main(String[] args) {
  21. Example<String> example = Example.builder().build();
  22. example.setValue("test");
  23. System.out.println(example.getValue().toUpperCase());
  24. }
  25. }
  26. 我得到一个错误
  27. Example.java:27: 错误不兼容的类型无法将 Example<Object> 转换为 Example<String>
  28. Example<String> example = Example.builder().build();
  29. 但是当我在 builder 方法的参数中添加类型时一切正常运作
  30. public static <T> Builder<T> builder(T type) {
  31. return new Builder<>();
  32. }
  33. 为什么我如何消除这个虚拟的类型参数
  34. 附加问题如何将复杂类型传递给像这样的方法
  35. public static <T> Builder<T> builder(Class<T> type) {
  36. return new Builder<>();
  37. }
  38. Example<Optional<String>> example = Example.builder(???).build();
  39. **更新** 这只是一个示例实际应用中将会有多个构建器在构建过程的不同阶段相互创建并且当然它们将具有接受泛型类型的方法我希望有一种方法可以帮助编译器推断类型我甚至同意使用反射的技巧如果有的话它们可以帮助我获得一个漂亮的 DSL
英文:

When I try something like this

  1. public class Example&lt;T&gt; {
  2. private T value;
  3. private Example() {
  4. }
  5. public T getValue() {
  6. return value;
  7. }
  8. public void setValue(T value) {
  9. this.value = value;
  10. }
  11. public static &lt;T&gt; Builder&lt;T&gt; builder() {
  12. return new Builder&lt;&gt;();
  13. }
  14. public static class Builder&lt;T&gt; {
  15. public Example&lt;T&gt; build() {
  16. return new Example&lt;&gt;();
  17. }
  18. }
  19. public static void main(String[] args) {
  20. Example&lt;String&gt; example = Example.builder().build();
  21. example.setValue(&quot;test&quot;);
  22. System.out.println(example.getValue().toUpperCase());
  23. }
  24. }

I get an error

  1. Example.java:27: error: incompatible types: Example&lt;Object&gt; cannot be converted to Example&lt;String&gt;
  2. Example&lt;String&gt; example = Example.builder().build();

But when I add type to arguments of builder method, everything works fine

  1. public static &lt;T&gt; Builder&lt;T&gt; builder(T type) {
  2. return new Builder&lt;&gt;();
  3. }

Why? And how I can rid off this dummy type argument?

Bonus question: How to pass complex type to the method like this?

  1. public static &lt;T&gt; Builder&lt;T&gt; builder(Class&lt;T&gt; type) {
  2. return new Builder&lt;&gt;();
  3. }
  4. Example&lt;Optional&lt;String&gt;&gt; example = Example.builder(???).build();

UPDATE: It's only an example. The real-life application will have several builders creating each other on different stages of the building process and of course, they will have methods accepting generic type. I hope there is a way to help the compiler infer types. I even agree to tricks with reflection, if any exist and they can help me to get a beautiful DSL.

答案1

得分: 2

"Example"是一个原始类,因此隐式地,T被视为Object。您可以在调用方法之前使用尖括号(&lt;&gt;)将泛型类型指定给方法:

  1. Example<String> example = Example.<String>builder().build();
  2. // 这里 --------------------------^
英文:

Example is a raw class, so implicitly T is Object. You can specify the generic type when calling the method by putting it in angle-brackets (&lt;&gt;) before the method call:

  1. Example&lt;String&gt; example = Example.&lt;String&gt;builder().build();
  2. // Here --------------------------^

答案2

得分: 1

编译器可以推断出您要创建的Example是一个Example<String>,但它无法推断出builder()方法返回的Builder应该是一个Builder<String>

您可以通过将语句分为两个语句来帮助编译器:

  1. Builder<String> builder = Example.builder();
  2. Example<String> example = builder.build();

或者通过指定您从builder()方法中期望的Builder类型来帮助编译器:

  1. Example<String> example = Example.<String>builder().build();
英文:

The compiler can infer that the Example you are trying to create is an Example&lt;String&gt;, but it can't infer that the Builder returned by builder() method should be a Builder&lt;String&gt;.

You can help the compiler by breaking the statement into two statements:

  1. Builder&lt;String&gt; builder = Example.builder();
  2. Example&lt;String&gt; example = builder.build();

Or by specifying the type of Builder you expect from the builder() method:

  1. Example&lt;String&gt; example = Example.&lt;String&gt;builder().build();

答案3

得分: 1

只要您不介意未检查的赋值警告,您可以仅进行以下更改:

  1. public static Builder builder() {
  2. return new Builder<>();
  3. }

编辑开始:

此外,如果您想要省略警告,可以将Builder类更改为:

  1. public static class Builder {
  2. private <T> Example<T> build() {
  3. return new Example<>();
  4. }
  5. }

并将静态的Builder方法更改为:

  1. public static Builder builder() {
  2. return new Builder();
  3. }

编辑结束。

然后您可以像这样使用它:

  1. Example<String> example = Example.builder().build();
英文:

if you don't mind an Unchecked assignment warning you could add just the following change:

  1. public static Builder builder() {
  2. return new Builder&lt;&gt;();
  3. }

EDIT Start:

Additionally, if you want to omit the warning you can change the Builder class into:

  1. public static class Builder {
  2. private &lt;T&gt; Example&lt;T&gt; build() {
  3. return new Example&lt;&gt;();
  4. }
  5. }

and change the static Builder method to:

  1. public static Builder builder() {
  2. return new Builder();
  3. }

EDIT End.

and then you can use it like this:

  1. Example&lt;String&gt; example = Example.builder().build();

huangapple
  • 本文由 发表于 2020年4月5日 19:02:21
  • 转载请务必保留本文链接:https://java.coder-hub.com/61041551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定