相当于C++';Java中的std::bind? P>有一种方法将参数参数绑定到java中的函数指针,就像您可以用STD::C++中的绑定?类似这样的东西的Java等价物是什么 void PrintStringInt(const char * s, int n) { std::cout << s << ", " << n << std::endl; } void PrintStringString(const char * s, const char * s2) { std::cout << s << ", " << s2 << std::endl; } int main() { std::vector<std::function<void(const char *)>> funcs; funcs.push_back(std::bind(&PrintStringInt, std::placeholders::_1, 4)); funcs.push_back(std::bind(&PrintStringString, std::placeholders::_1, "bar")); for(auto i = funcs.begin(); i != funcs.end(); ++i){ (*i)("foo"); } return 0; } void PrintStringInt(常量字符*s,int n) { std::cout

相当于C++';Java中的std::bind? P>有一种方法将参数参数绑定到java中的函数指针,就像您可以用STD::C++中的绑定?类似这样的东西的Java等价物是什么 void PrintStringInt(const char * s, int n) { std::cout << s << ", " << n << std::endl; } void PrintStringString(const char * s, const char * s2) { std::cout << s << ", " << s2 << std::endl; } int main() { std::vector<std::function<void(const char *)>> funcs; funcs.push_back(std::bind(&PrintStringInt, std::placeholders::_1, 4)); funcs.push_back(std::bind(&PrintStringString, std::placeholders::_1, "bar")); for(auto i = funcs.begin(); i != funcs.end(); ++i){ (*i)("foo"); } return 0; } void PrintStringInt(常量字符*s,int n) { std::cout,java,c++,functional-programming,bind,Java,C++,Functional Programming,Bind,您可以创建一个具有所需签名的函数的匿名类,该类将调用转发给原始函数 假设您具有以下功能: public class StringPrinter { public void printStringInt( String s, int n ) { System.out.println( s + ", " + n ); } public void printStringString( String s, String s2 ) { System.o

您可以创建一个具有所需签名的函数的匿名类,该类将调用转发给原始函数

假设您具有以下功能:

public class StringPrinter {
    public void printStringInt( String s, int n ) {
       System.out.println( s + ", " + n );
    }

    public void printStringString( String s, String s2 ) {
       System.out.println( s + ", " + s2 );
    }
}
然后可以创建匿名类,有效地将参数绑定到这些函数

public static void main( String[] args ) {
     public interface Print1String {
         public void printString( String s );
     }

     List<Print1String> funcs = new ArrayList<Print1String);
     funcs.add( new Print1String() {
        public void printString( String s ) {
            new StringPrinter( ).printStringInt( s, 42 ); 
        }});
     funcs.add( new Print1String() {
        public void printString( String s ) {
            new StringPrinter( ).printStringString( s, "bar" ); 
        }});

     for ( Print1String func : funcs ) {
         func.print1String("foo");
     }
}
publicstaticvoidmain(字符串[]args){
公共接口打印字符串{
公共无效打印字符串(字符串s);
}

列表函数=新的ARARELISTH.P>我恐怕不行,但是这个代码是模仿C++模板(很差)的一种方法:


我认为这是逐行翻译的最接近之处,
bind
不会将1:1映射到任何Java构造

static void PrintStringInt(String s, int n)
{
    System.out.println(s + ", " + n);
}

static void PrintStringString(String s, String s2)
{
    System.out.println(s + ", " + s2);
}

interface MyCall {
    void fn(String value);
}

public static void main(String[] argv)
{
    Vector<MyCall> funcs = new Vector<MyCall>();
    funcs.add(new MyCall() { 
      @Override public void fn(String value) {PrintStringInt(value, 4); }});
    funcs.add(new MyCall() { 
      @Override public void fn(String value) {PrintStringString(value, "bar"); }});
    for(MyCall i : funcs){
        i.fn("foo");
    }
}
static void PrintStringInt(字符串s,int n)
{
系统输出打印项次(s+“,”+n);
}
静态无效PrintStringString(字符串s、字符串s2)
{
系统输出打印项次(s+“,”+s2);
}
接口MyCall{
void fn(字符串值);
}
公共静态void main(字符串[]argv)
{
向量funcs=新向量();
funcs.add(新建MyCall(){
@重写公共void fn(字符串值){PrintStringInt(值,4);}};
funcs.add(新建MyCall(){
@重写公共void fn(字符串值){PrintStringString(值,“bar”);};
用于(MyCall i:funcs){
i、 fn(“foo”);
}
}

从Java7开始,也可以对方法句柄执行相同的操作,有关详细信息,请参阅类
java.lang.invoke.MethodHandle
的API文档。

使用MethodHandle,下面是一个示例

public class HW
{
    int sum;

  public HW(int a, int b)
  {
    super();
    this.sum = a+b;
  }


  public void hello1(double c)
  {
    double newnumber = sum+c;
   System.out.println("hello from hello1, new number= " + newnumber);
  }

  public void hello2()
  {        
   System.out.println("hello from hello2, sum=" + sum);
  }
}

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;


public class TestMethodHandle
{

/**
 * @param args
 */
  public static void main(String[] args)
  {

    HW hw = new HW(10, 15);
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle mh;
    try
    {
        mh = lookup.findVirtual(HW.class, "hello2",
                                             MethodType.methodType(void.class));
        mh.invoke(hw);

        mh = lookup.findVirtual(HW.class, "hello1",
                MethodType.methodType(void.class, double.class));
        mh.invoke(hw, 20);

    } catch (NoSuchMethodException e)
    {
        e.printStackTrace();
    } catch (IllegalAccessException e)
    {
        e.printStackTrace();
    } catch (Throwable e)
    {
        e.printStackTrace();
    }

}

对我来说太多“新”了,StringPrinter的两个方法可能是静态的在这种情况下,这些只是您可能想要绑定到的一个示例。一般来说,将实例方法优先于静态方法更好地支持以后继承和注入的可能性。这与std::bind不同,std::bind在运行时是动态的,这一个是静态的t编译时间
static void PrintStringInt(String s, int n)
{
    System.out.println(s + ", " + n);
}

static void PrintStringString(String s, String s2)
{
    System.out.println(s + ", " + s2);
}

interface MyCall {
    void fn(String value);
}

public static void main(String[] argv)
{
    Vector<MyCall> funcs = new Vector<MyCall>();
    funcs.add(new MyCall() { 
      @Override public void fn(String value) {PrintStringInt(value, 4); }});
    funcs.add(new MyCall() { 
      @Override public void fn(String value) {PrintStringString(value, "bar"); }});
    for(MyCall i : funcs){
        i.fn("foo");
    }
}
public class HW
{
    int sum;

  public HW(int a, int b)
  {
    super();
    this.sum = a+b;
  }


  public void hello1(double c)
  {
    double newnumber = sum+c;
   System.out.println("hello from hello1, new number= " + newnumber);
  }

  public void hello2()
  {        
   System.out.println("hello from hello2, sum=" + sum);
  }
}

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;


public class TestMethodHandle
{

/**
 * @param args
 */
  public static void main(String[] args)
  {

    HW hw = new HW(10, 15);
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle mh;
    try
    {
        mh = lookup.findVirtual(HW.class, "hello2",
                                             MethodType.methodType(void.class));
        mh.invoke(hw);

        mh = lookup.findVirtual(HW.class, "hello1",
                MethodType.methodType(void.class, double.class));
        mh.invoke(hw, 20);

    } catch (NoSuchMethodException e)
    {
        e.printStackTrace();
    } catch (IllegalAccessException e)
    {
        e.printStackTrace();
    } catch (Throwable e)
    {
        e.printStackTrace();
    }

}