Java回呼函數的用法__區塊鏈

來源:互聯網
上載者:User

個人理解:回呼函數就是將一部分實現拋給客戶
package com.cy.test;import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class WorkImpl {    public void work(CallBack callBack) {        System.out.println("work start...");        List<String> list = new ArrayList<String>();        list.add("1");        list.add("2");        list.add("3");        callBack.execute(list);        System.out.println(Arrays.toString(list.toArray()));    }    public static void main(String[] args) {        WorkImpl work = new WorkImpl();        work.work(new CallBack() {            public void execute(List<String> list) {                list.add("haha");            }        });    }}interface CallBack {    void execute(List<String> list);}




下面來個例子:

package com.cy.test;import java.io.*;/** * 檔案上傳時回呼函數的例子 */public class UploadService {    public void upload(UploadCallback callback) throws Exception {        File f = new File("tmp-test.txt");        FileOutputStream out = new FileOutputStream(f);        callback.send(out);    }    public static void main(String[] args) throws Exception {        UploadService uploadService = new UploadService();        InputStream in = null;        // 通過回調介面可以使實現更靈活        // 1 提供的參考實現1        uploadService.upload(new UploadStream(in));        // 2 提供的參數實現2        uploadService.upload(new UploadLocalFile("file"));        // 3 ...        // 4 使用時自己實現        uploadService.upload(new UploadCallback() {            public void send(OutputStream out) throws IOException {                // ...            }        });    }}/** * 回調介面 */interface UploadCallback {   void send(OutputStream out) throws IOException;}/** * 通過IO流方式實現的回調 */class UploadStream implements UploadCallback {    InputStream in;    public UploadStream(InputStream in) {        this.in = in;    }    public void send(OutputStream out) throws IOException {        int readBytes;        byte[] buff = new byte[256 * 1024];        try {            while ((readBytes=in.read(buff)) >= 0) {                if (readBytes == 0) {                    continue;                }                out.write(buff, 0, readBytes);            }        } finally {            in.close();        }    }}/** * 通過本地檔案實現的回調 */class UploadLocalFile implements UploadCallback {    private String local_filename;    public UploadLocalFile(String szLocalFilename) {        this.local_filename = szLocalFilename;    }    public void send(OutputStream out) throws IOException {        FileInputStream fis;        int readBytes;        byte[] buff = new byte[256 * 1024];        fis = new FileInputStream(this.local_filename);        try {            while ((readBytes = fis.read(buff)) >= 0) {                if (readBytes == 0) {                    continue;                }                out.write(buff, 0, readBytes);            }        } finally {            fis.close();        }    }}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.