简体   繁体   English

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

[英]can we store method passing argument in java variable

how can i store this displayPriceMessage method with argumetnt in variable 我如何将这个argumetnt的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);

You can do this in FP. 您可以在FP中执行此操作。 Perhaps you would need a custom Functional 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 ");
}

Create a functional interface having an abstract method of similar signature as that of displayPriceMessage 创建一个功能接口,该接口具有与displayPriceMessage相似的签名的抽象方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:传递对变量的引用作为方法的参数 - Java: Passing a reference to a variable as an argument for a method 我们应该直接在Java中将参数作为变量或作为新对象传递给方法吗 - Should we pass argument to a method as a variable or as a new object directly in java 如何在 Java 8 中将方法存储在变量中? - How can I store a method in a variable in Java 8? 将类作为参数传递给java中的方法 - Passing a class as an argument to a method in java Java:将队列作为方法参数传递? - Java: passing queue as a method argument? 将省略号作为参数传递给Java方法 - Passing elipses as an argument to java method 将参数传递给Java中的方法内部定义的方法 - Passing an argument to a method defined inside a method in Java Java方法可以基于带有对象变量引用的参数对数组进行排序吗? - Can a Java Method sort an Array based on an argument with an object variable reference? 我可以在java中为公共变量和方法参数使用相同的名称吗? - Can I use same name for public variable and method argument in java? 我们可以通过传递类类型作为参数来使用 Java 泛型创建不同的类实例吗 - Can we create different class instance using Java generics by passing class type as argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM