Java 泛型 + 生成器模式

2022-09-01 02:22:14

我如何拨打下面的电话?start()

package com.example.test;

class Bar {}

public class Foo<K>
{
    final private int count;
    final private K key;

    Foo(Builder<K> b)
    {
        this.count = b.count;
        this.key = b.key;
    }

    public static class Builder<K2>
    {
        int count;
        K2 key;

        private Builder() {}
        static public <K3> Builder<K3> start() { return new Builder<K3>(); }
        public Builder<K2> setCount(int count) { this.count = count; return this; }
        public Builder<K2> setKey(K2 key) { this.key = key; return this; }
        public Foo<K2> build() { return new Foo(this); }
    }

    public static void main(String[] args)
    {
        Bar bar = new Bar();
        Foo<Bar> foo1 = Foo.Builder.start().setCount(1).setKey(bar).build();
        // Type mismatch: cannot convert from Foo<Object> to Foo<Bar>

        Foo<Bar> foo2 = Foo.Builder<Bar>.start().setCount(1).setKey(bar).build();
        // Multiple markers at this line
        // - Bar cannot be resolved
        // - Foo.Builder cannot be resolved
        // - Syntax error on token ".", delete this token
        // - The method start() is undefined for the type Foo<K>
        // - Duplicate local variable fooType mismatch: cannot convert from Foo<Object> to Foo<Bar>

        Foo<Bar> foo3 = Foo<Bar>.Builder.start().setCount(1).setKey(bar).build();
        // Multiple markers at this line
        // - Foo cannot be resolved
        // - Syntax error on token ".", delete this token
        // - Bar cannot be resolved     
    }
}

答案 1

您很接近:

Foo.Builder.<Bar> start().setCount(1).setKey(bar).build();

干杯!:)

PS 如果编译器无法自行推断出方法的类型参数,则可以通过调用 来强制它。obj.<Type> method(...)

您可能希望使用的 P.P.S:

public Foo<K2> build() {
    return new Foo<K2>(this);
}

避免使用原始类型。


答案 2

Andrei的方法是可以的,但大多数程序员可能会与相当未知的语法作斗争。以这种方式使用可能更容易:

static public <K3> Builder<K3> start(Class<K3> cls) { return new Builder<K3>(); }

Foo<Bar> foo1 = Foo.Builder.start(Bar.class).setCount(1).setKey(bar).build();

传递该类只是为了帮助泛型类型。它并不漂亮,但至少语法是常识。

另一种选择是立即从泛型类型的对象开始:

Foo<Bar> foo1 = Foo.Builder.startWithKey(bar).setCount(1).build();

推荐