Java forName));//若要获取重载方法,必须在泛型中指定参数 assertEquals(“isEmpty”,Fun.getName(String::isEmpty))//使用Fun的静态getName方法获取方法名。从toMethod返回的方法对象将不会返回正确的字符串。

Java forName));//若要获取重载方法,必须在泛型中指定参数 assertEquals(“isEmpty”,Fun.getName(String::isEmpty))//使用Fun的静态getName方法获取方法名。从toMethod返回的方法对象将不会返回正确的字符串。,java,c#,delegates,Java,C#,Delegates,Java中的代理 Delegate.With1Param<String, String> greetingsDelegate = new Delegate.With1Param<>(); greetingsDelegate.add(str -> "Hello " + str); greetingsDelegate.add(str -> "Goodbye " + str); DelegateInvocat

Java中的代理

  Delegate.With1Param<String, String> greetingsDelegate = new Delegate.With1Param<>();
  greetingsDelegate.add(str -> "Hello " + str);
  greetingsDelegate.add(str -> "Goodbye " + str);

  DelegateInvocationResult<String> invocationResult = greetingsDelegate.invokeAndAggregateExceptions("Sir");

  invocationResult.getFunctionInvocationResults().forEach(funInvRes -> System.out.println(funInvRes.getResult()));
  //prints: "Hello sir" and "Goodbye Sir"
Delegate.With1Param greetingsDelegate=newdelegate.With1Param();
greetingsDelegate.add(str->“Hello”+str);
添加(str->“再见”+str);
DelegateInvocationResult invocationResult=greetingsDelegate.InvokeAndagRegateExceptions(“Sir”);
forEach(funivres->System.out.println(funivres.getResult());
//打印:“你好先生”和“再见先生”
  • 事件

      //Create a private Delegate. Make sure it is private so only *you* can invoke it.
      private static Delegate.With0Params<String> trimDelegate = new Delegate.With0Params<>();
    
      //Create a public Event using the delegate you just created.
      public static Event.With0Params<String> trimEvent= new Event.With0Params<>(trimDelegate);
    
    //创建一个私有委托。确保它是私有的,这样只有*你*可以调用它。
    私有静态委托。With0Params trimDelegate=新委托。With0Params();
    //使用刚创建的委托创建公共事件。
    公共静态事件.With0Params trimEvent=新事件.With0Params(trimDelegate);
    
  • 类型安全方法创建

      Method m1 = Fun.toMethod(Thread::isAlive)  // Get final method
      Method m2 = Fun.toMethod(String::isEmpty); // Get method from final class
      Method m3 = Fun.toMethod(BufferedReader::readLine); // Get method that throws checked exception
      Method m4 = Fun.<String, Class[]>toMethod(getClass()::getDeclaredMethod); //to get vararg method you must specify parameters in generics
      Method m5 = Fun.<String>toMethod(Class::forName); // to get overloaded method you must specify parameters in generics
      Method m6 = Fun.toMethod(this::toString); //Works with inherited methods
    
    methodm1=Fun.toMethod(线程::isAlive)//获取最终方法
    方法m2=Fun.toMethod(字符串::isEmpty);//从最终类获取方法
    方法m3=Fun.toMethod(BufferedReader::readLine);//Get方法引发选中的异常
    方法m4=Fun.toMethod(getClass()::getDeclaredMethod)//要获取vararg方法,必须在泛型中指定参数
    方法m5=Fun.toMethod(类::forName);//若要获取重载方法,必须在泛型中指定参数
    方法m6=Fun.toMethod(this::toString)//使用继承的方法
    

  • 免责声明:我是这个项目的作者。

    Java使用一个方法接口来实现这个概念。您可以选择应用于这样一个接口。您能举一个例子说明如何像下面的代码那样在类之间进行通信吗?当调用操作A而不是任务A时,使用这个概念调用任务B,因为我是java新手,我从中了解到的只是确保函数接口不能有多个抽象方法。Java使用一个方法接口来实现这个概念。您可以选择应用于这样一个接口。您能举一个例子说明如何像下面的代码那样在类之间进行通信吗?当调用操作A而不是任务A时,使用这个概念调用任务B,因为我是java新手,我从中了解到的只是确保功能接口不能有多个抽象方法。感谢您的回答,它帮助了我,似乎是观察者模式实现。感谢您的回答,它帮助了我,似乎是观察者模式实现。
    import java.util.ArrayList;
    public class HelloWorld{
    
         public static void main(String []args){ 
    
                TaskA taskA = new TaskA();
                TaskB task = new TaskB();
                Action action = new Action();
                action.doAction();
         }
    }
    
    interface ActionDelegate {
        void doAction();
    }
    
    class Action{
    
        static public ArrayList<ActionDelegate> onAction = new ArrayList<>();
        public void doAction(){
            System.out.println("Action A");
            for(ActionDelegate ad: onAction){
                ad.doAction();
            }
        }
    }
    class TaskA implements ActionDelegate{
    
        TaskA(){
            Action.onAction.add(this);
        }
        public void doAction(){ 
                System.out.println("Do Task A");
        }
    
    }
    
    class TaskB implements ActionDelegate{
    
        TaskB(){
            Action.onAction.add(this);
        }
        public void doAction(){ 
                System.out.println("Do Task B");
        }
    
    }
    
    <dependency>
        <groupId>com.github.hervian</groupId>
        <artifactId>safety-mirror</artifactId>
        <version>3.0.0</version>
    </dependency>
    
      public void foo(Fun.With1ParamAndVoid<String> printer) throws Exception {
          printer.invoke("hello world);
      }  
      foo(System.out::println);   //This signature match the the Fun defined by method Foo. If it did not, the compiler would emit an error.  
    
      Method m1 = Fun.toMethod(String::isEmpty)
      Method m2 = Fun.<String>toMethod(Class::forName)); // to get overloaded method you must specify parameters in generics  
    
      assertEquals("isEmpty", Fun.getName(String::isEmpty)); //use Fun's static getName method to get the method name. The Method objects returned from toMethod will not return the correct String.
    
      Delegate.With1Param<String, String> greetingsDelegate = new Delegate.With1Param<>();
      greetingsDelegate.add(str -> "Hello " + str);
      greetingsDelegate.add(str -> "Goodbye " + str);
    
      DelegateInvocationResult<String> invocationResult = greetingsDelegate.invokeAndAggregateExceptions("Sir");
    
      invocationResult.getFunctionInvocationResults().forEach(funInvRes -> System.out.println(funInvRes.getResult()));
      //prints: "Hello sir" and "Goodbye Sir"
    
      //Create a private Delegate. Make sure it is private so only *you* can invoke it.
      private static Delegate.With0Params<String> trimDelegate = new Delegate.With0Params<>();
    
      //Create a public Event using the delegate you just created.
      public static Event.With0Params<String> trimEvent= new Event.With0Params<>(trimDelegate);
    
      Method m1 = Fun.toMethod(Thread::isAlive)  // Get final method
      Method m2 = Fun.toMethod(String::isEmpty); // Get method from final class
      Method m3 = Fun.toMethod(BufferedReader::readLine); // Get method that throws checked exception
      Method m4 = Fun.<String, Class[]>toMethod(getClass()::getDeclaredMethod); //to get vararg method you must specify parameters in generics
      Method m5 = Fun.<String>toMethod(Class::forName); // to get overloaded method you must specify parameters in generics
      Method m6 = Fun.toMethod(this::toString); //Works with inherited methods