6

私たちが知っているように、Java 16 には、レコード、封印されたインターフェイスとクラス、パターン マッチングなどの新機能が備わっています。

今日、私はトレーニングプロジェクトでそれらを使用したかった. しかし、私は問題に遭遇しました。おそらく私は何かを理解していません。

私の Intellij Idea プロジェクトを表す特定のコードでは、状況は次のようになります:

package com.example.records.domain.client;
public sealed interface Client permits Client.Regular, Client.SuperVip, Client.Vip {
        
            Limit currentLimit();
        
            record Regular() implements Client {
        
                @Override
                public Client.Limit currentLimit() {
                    return Client.Limit.of((short) 10);
                }
            }
        
        
            record Vip() implements Client {
        
                @Override
                public Client.Limit currentLimit() {
                    return Client.Limit.of((short) 20);
                }
            }
        
            record SuperVip() implements Client {
        
                @Override
                public Client.Limit currentLimit() {
                    return Client.Limit.of((short) 100);
                }
            }
        
        
            record Limit(short value) {
                public Limit {
                    if (value < 0) throw new IllegalArgumentException("Values below 0 not allowed!");
                }
        
                static Limit of(short value) {
                    return new Limit(value);
                }
            }
        }

Order クラスは、DDD から認識される単純な集約です: しかし、switch ステートメントに問題があります:

package com.example.records.domain.order;

import com.example.records.domain.OrderId;
import com.example.records.domain.client.Client;
import com.example.records.domain.client.Client.*;
import com.example.records.shared.Money;
import com.example.records.shared.Result;

import java.util.LinkedList;
import java.util.List;

public class Order() {
    private final OrderId orderId;
    private final Client client;
    private final LinesItems linesItems = LinesItems.empty();

    Order(OrderId orderId, Client client) {
        this.orderId = orderId;
        this.client = client;
    }

    public Result add(LineItem lineItem) {
       return switch (client) {
            case Regular r -> addAnItemIfTheLimitIsNotExceeded(r.);
            case Vip v -> addAnItemIfTheLimitIsNotExceeded(v.);
            case SuperVip sv -> addAnItemIfTheLimitIsNotExceeded(sv.);
        };
    }

    private Result addAnItemIfTheLimitIsNotExceeded(Limit limit) {
        // TODO need impl
        return Result.OK;
    }


    private static class LinesItems {
        private final List<LineItem> lines = new LinkedList<>();

        private LinesItems() {}

        void add(LineItem lineItem) {
            this.lines.add(lineItem);
        }

        Money cost() {
            return lines.stream().map(LineItem::cost).reduce(Money.ZERO, Money::add);
        }

        static LinesItems empty() {
            return new LinesItems();
        }
    }
}

record LineItem(Money cost) {}

その他のクラス:

package com.example.records.domain;

public record OrderId(String value) {

    public OrderId {
        validate();
    }

    private void validate() {
        if (value == null)
            throw new IllegalArgumentException("Null value not allowed!");
        if (value.isBlank())
            throw new IllegalArgumentException("Blank value not allowed!");
    }
}
package com.example.records.shared;

import java.math.BigDecimal;

public record Money(BigDecimal value) {

    public static final Money ZERO = new Money(new BigDecimal("0.00"));

    public Money add(Money money) {
        return new Money(this.value.add(money.value));
    }
}
package com.example.records.shared;

public sealed interface Result {

    Success OK = new Success();

    record Success() implements Result{}
    record Failure(String message) implements Result {}
}

"(Expression expected)" が表示されます どこで間違ったのですか? (実験的な機能が有効になっています。Java 16 open jdk をインストールしました)

ここに画像の説明を入力

ここに画像の説明を入力

4

2 に答える 2

14

の型パターンはinstanceof、Java 16 の最終的な (プレビューではない) 機能です。ただし、型パターンswitchはまだ Java 16 にはありません。彼らはすぐに到着する予定です。

于 2021-03-18T15:57:01.393 に答える