gpt4 book ai didi

java - 我们可以将传递参数的方法存储在java变量中吗

转载 作者:行者123 更新时间:2023-12-02 09:36:44 25 4
gpt4 key购买 nike

如何将带有参数的 displayPriceMessage 方法存储在变量中

//method of displayPriceMethod
public void displayPriceMessage(String name, String msg, boolean cream, boolean coke)
{
priceamoutntxt.setText("\n Name: "+Ename.getText().toString()+"\n total : "+priceamoutntxt.getText()+"\n Add Ice-Cream ?"+ cream +"\n Add Coca-Cola ?"+coke+"\n than you ");
}

// displayPriceMessage argumetnt
displayPriceMessage("ename","",isIceCream, cocaCola);

最佳答案

您可以在 FP 中执行此操作。也许您需要一个自定义功能接口(interface)。

DisplayPriceMessage displayPriceMessage =  this::displayPriceMessage;

public void displayPriceMessage(String name, String msg, boolean cream, boolean coke)
{
priceamoutntxt.setText("\n Name: "+Ename.getText().toString()+"\n total : "+priceamoutntxt.getText()+"\n Add Ice-Cream ?"+ cream +"\n Add Coca-Cola ?"+coke+"\n than you ");
}

创建一个具有与displayPriceMessage类似签名的抽象方法的功能接口(interface)

@FunctionalInterface
interface DisplayPriceMessage {
void displayPriceMessage(String name, String msg, boolean cream, boolean coke);
}

关于java - 我们可以将传递参数的方法存储在java变量中吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57448389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com