[JDK 8 - 関数型プログラミング] 4.4 サプライヤー

1. サプライヤーインターフェース

2.実戦

ステージ 1: Student クラスの作成

ステージ 2: メソッドの作成

ステージ 3: メソッドの呼び出し

ステージ 4: 実行結果


1. サプライヤーインターフェース

  • 供給 インターフェース:入力パラメーターなし、戻り値 (T: 出力パラメーターのタイプ)

  • 呼び出しメソッド:  T get();

  • 目的: たとえば、パラメーターのないファクトリ メソッド、つまりファクトリ デザイン パターンは、単なるプロバイダーであるオブジェクトを作成します。

/**
 * * @param <T> the type of results supplied by this supplier
 */
@FunctionalInterface
public interface Supplier<T> {
    T get();
}

2.実戦

ステージ 1: Student クラスの作成

public class Student {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

ステージ 2: メソッドの作成

    public static Student newStudent() {
        Supplier<Student> supplier = () -> {
            Student student = new Student();
            student.setName("默认名称");
            return student;
        };
        return supplier.get();
    }

ステージ 3: メソッドの呼び出し

    public static void main(String[] args) {

        Student student = newStudent();
        log.info(student.getName());
    }

ステージ 4: 実行結果

おすすめ

転載: blog.csdn.net/ladymorgana/article/details/132986316