Java C C++回呼函數

來源:互聯網
上載者:User

標籤:回調

1、定義CALLBACK類型的函數指標   typedef int (CALLBACK*)(int x, int y)


2、定義函數method,將CALLBACK類型的函數指標作為函數參數   void method(CALLBACK FP, int a, int b)

void method(CALLBACK FP, int a, int b)

{

    FP(a, b);

}


3、定義main函數,調用method方法

int main()

{

    // 調用method方法需要參數FP,所以自己寫一個該類型的函數,就實現回調了

    int ret = method(add, 3, 5)

    return 0;

}


4、int (*add)(int x, int y)

{

    return x + y;

}




java中用介面類比函數指標實現第一步

public interface FunctionPoint {

    int callback(int x, int y);

}


定義一個類,將該介面對象作為方法參數實現第二步

public class Function {

    public int method(FunctionPoint FP, int x, int y) {

        FP.callback(x, y);

    }

}


定義帶main方法的類

public class CalllBack implements FunctionPoint {

    public static void main(String[] args) {

        // 調用method方法

        Function f = new Function();

        f.method(new imp(), 2, 5);

    }    

}


public class imp implements FunctionPoint {

    public int callback(int x, int y) {

        return x + y;

    }

}



C++回調參考:http://blog.csdn.net/xie1xiao1jun/article/details/8262902


本文出自 “AI-Life” 部落格,請務必保留此出處http://ai00life.blog.51cto.com/2198685/1932493

Java C C++回呼函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.