JAVA callback function [類似 C# .NET anonymous delegate(委託)]

JAVA callback function [類似 C# .NET anonymous delegate(委託)]

JAVA callback function [類似 C# .NET anonymous delegate(委託)]


資料來源: https://stackoverflow.com/questions/443708/callback-functions-in-java

https://www.javatpoint.com/java-callback-function


Code

public class Main {

    public interface Visitor{
        int doJob(int a, int b);
    }


    public static void main(String[] args) {
        Visitor adder = new Visitor(){
            public int doJob(int a, int b) {
                return a + b;
            }
        };

        Visitor multiplier = new Visitor(){
            public int doJob(int a, int b) {
                return a*b;
            }
        };

        System.out.println(adder.doJob(10, 20));
        System.out.println(multiplier.doJob(10, 20));

    }
}

One thought on “JAVA callback function [類似 C# .NET anonymous delegate(委託)]

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *