簡體   English   中英

將方法作為參數傳遞

[英]Pass method as an argument

我有兩種類似的方法:

public void methodOne() {
    final List<User> users = userService.getUsers();
    final Integer count = getCount();
    emailService.sendEmailOne(users, count);
}

public void methodTwo() {
    final List<User> users = userService.getUsers();
    final Integer count = getCount();
    emailService.sendEmailTwo(users, count);
}

不同之處在於 - 我正在發送不同類型的 email。 我可以在這里傳遞方法名稱作為參數來獲得類似的東西:

public void method(sendEmail) {
   final List<User> users = userService.getUsers();
    final Integer count = getCount();
    emailService.sendEmail(users, count);
}

您的第一個代碼片段(例如public void methodOne { )中的方法標頭在語法上不正確。 方法名后面沒有參數列表。

為了在運行時靈活地選擇方法,您可以使用一個抽象方法創建一個interface ,然后將其實現到兩個不同的類中並相應地覆蓋它。 然后你可以使用你的接口實例作為method的參數。

它應該是這樣的:

public interface Sender {
    abstract public void send();
}


public class SenderOne implements Sender {
    public void send() {
        // your implementation here
    }
}

public class SenderTwo implements Sender {
    public void send() {
        // your implementation here
    }
}

最后:

public void method (Sender sender) {
    //...
    sender.send();
}

有一種更快更容易的方法來完成它。

此處的這篇文章還包括一些以前的答案。

第一個 class 聲明只是為了使示例可編譯。 有趣的部分在方法getCount()之后開始

package snippet;

import java.io.IOException;
import java.util.List;

public class LamdaExample {
    // dummy declarations
    static class User {}
    static class EmailService {
        public void sendEmailOne(final List<User> pUsers, final Integer pCount) {}
        public void sendEmailTwo(final List<User> pUsers, final Integer pCount) {}
    }
    static class UserService {
        public List<User> getUsers() {
            return null;
        }
    }

    public LamdaExample() {}
    private final UserService   userService     = new UserService();
    private final EmailService  emailService    = new EmailService();
    private Integer getCount() {
        return null;
    }

    // interesting part comes here

    public void methodOne() {
        System.out.println("LamdaExample.methodOne()");
        final List<User> users = userService.getUsers();
        final Integer count = getCount();
        emailService.sendEmailOne(users, count);
    }
    public void methodTwo() {
        System.out.println("LamdaExample.methodTwo()");
        final List<User> users = userService.getUsers();
        final Integer count = getCount();
        emailService.sendEmailTwo(users, count);
    }

    public void method(final Runnable sendEmail) {
        final List<User> users = userService.getUsers();
        final Integer count = getCount();
        sendEmail.run();
    }

    public static void main(final String[] args) {
        // simple runnable, see code above
        final LamdaExample t = new LamdaExample();
        t.method(() -> t.methodOne());
        t.method(() -> t.methodTwo());

        // extended special functional interface, see code below
        t.methodSpecial((p, q) -> t.methodX(p, q));
        t.methodSpecial((p, q) -> t.methodY(q, p));

    }

    // extended example starts here

    @FunctionalInterface
    interface MySpecialInterface {
        double specialMethod(String param1, Class<?> param2) throws IOException;
    }

    public void methodSpecial(final MySpecialInterface sendEmail) {
        final List<User> users = userService.getUsers();
        final Integer count = getCount();

        try {
            sendEmail.specialMethod("lol", this.getClass());
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
    public double methodX(final String pP, final Class<?> pQ) {
        System.out.println("LamdaExample.methodX()");
        final List<User> users = userService.getUsers();
        final Integer count = getCount();
        emailService.sendEmailOne(users, count);
        return 123.456;
    }
    public double methodY(final Class<?> pQ, final String pP) {
        System.out.println("LamdaExample.methodY()");
        final List<User> users = userService.getUsers();
        final Integer count = getCount();
        emailService.sendEmailTwo(users, count);
        return 98.765;
    }


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM