【问题标题】:How to use CompletableFuture and functional interfaces of java8?java8的CompletableFuture和函数式接口如何使用?
【发布时间】:2017-08-28 15:40:15
【问题描述】:

我想将 CompteBancaire 类替换为功能接口,谁能告诉我该怎么做?

下面是sn-p的代码,我想在verserArgentretirerArgent方法中使用CompletableFuture进行异步任务。

package myclasses;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

public class CompteBancaire {

    private int numC;
    private int nbTraces;
    public static int nbComptes;
    public double solde;
    private String nom;
    private String prenom;

    private List<Double> tracesComptes = new ArrayList<>();

    /**
     * @Constructeur non pramétré
     */
    public CompteBancaire() {
        this.tracesComptes = new ArrayList<>();
        nbComptes++;

    }

    /**
     *
     * @param numC
     * @param solde
     * @param nom
     * @param prenom
     *
     * @Constructeur pramétré
     */
    public CompteBancaire(int numC, double solde, String nom, String prenom) {
        this.numC = numC;
        this.solde = solde;
        this.nom = nom;
        this.prenom = prenom;
        this.tracesComptes = new ArrayList<>();
        nbComptes++;
    }

    /**
     *
     * @return @getters && setters
     *
     *
     */
    public int getNumC() {
        return numC;
    }

    public void setNumC(int numC) {
        this.numC = numC;
    }

    public int getNbTraces() {
        return nbTraces;
    }

    public void setNbTraces(int nbTraces) {
        this.nbTraces = nbTraces;
    }

    public double getSolde() {
        return solde;
    }

    public void setSolde(double solde) {
        this.solde = solde;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    /**
     *
     * @param tracesCompte
     */
    public void setTracesCompte(List<Double> tracesCompte) {
        this.tracesComptes = tracesCompte;
    }

    /**
     *
     * @param montant
     * @return
     * @throws tp3.ErreurNegatif
     * @Methode qui depose de l'argent au compte (versement)
     *
     */

    public void verserArgent(double montant) throws ErreurNegatif, Throwable {
        if (montant > 0) {
            solde += montant;
            tracesComptes.add(nbTraces, solde);
            nbTraces++;
        } else {
            throw new ErreurNegatif();
        }

    }

    /**
     *
     * @Methode qui retire de l'argent du compte
     *
     */
    void retirerArgent(double montant) throws ErreurNegatif {
        if (solde - montant > 0) {
            solde -= montant;
            tracesComptes.add(nbTraces, solde);
            nbTraces++;
        } else {
            throw new ErreurNegatif();
        }

    }

    /**
     *
     * @return @Methode qui affiche les informations relatives au compte
     *
     *
     */
    public String description() {

        return "Titulaire du compte:" + nom + " " + prenom + ", Numéro compte:" + numC + ", Solde:" + solde;
    }
}

【问题讨论】:

    标签: java asynchronous interface functional-programming completable-future


    【解决方案1】:

    函数式接口是只有一个方法的接口。您的类有 (1) 多个方法,并且 (2) 包含带有主体的方法,这意味着它不能转换为功能接口。当然,您可以创建一个接口,然后实现它。

    请用英文重写你的代码并编辑你的答案!

    public void verserArgent(double montant) throws ErreurNegatif, Throwable {
       if (montant > 0) {
           solde += montant;
           tracesComptes.add(nbTraces, solde);
           nbTraces++;
       } else {
           throw new ErreurNegatif();
       }
    }
    

    这是您要执行异步的方法。 (我不完全理解你,但我认为这就是你想要做的)。执行这种异步方法没有多大意义,因为它并不昂贵。

    CompletableFuture 可以通过其方法complete(T result) 简单实例化和完成。

    例如:

        public CompletableFuture<Void> verserArgent(double montant) throws ErreurNegatif, Throwable {
       CompletableFuture<Void> future = new CompletableFuture<>();
       if (montant > 0) {
           solde += montant;
           tracesComptes.add(nbTraces, solde);
           nbTraces++;
           future.complete(null);
       } else {
           throw new ErreurNegatif();
       }
       return future;
    }
    

    同样,这可行,但没有意义。

    【讨论】:

    • 我应该在我的方法中使用 CompletableFuture,即使它们并不昂贵,请帮忙
    • 我已经回答了你的问题并编写了一个使用 CompletableFuture 的函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 2019-08-20
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多