更新時(shí)間:2022-11-21 10:17:42 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1679次
Java中的回調(diào):但是Java中不存在回調(diào)函數(shù)的概念,因?yàn)镴ava沒(méi)有指針概念。但是,在某些情況下,我們可以談?wù)摶卣{(diào)對(duì)象或回調(diào)接口。不是傳遞函數(shù)的內(nèi)存地址,而是傳遞引用函數(shù)位置的接口。
讓我們舉個(gè)例子來(lái)理解在哪里可以使用回調(diào)。假設(shè)一個(gè)程序員想要設(shè)計(jì)一個(gè)稅收計(jì)算器來(lái)計(jì)算一個(gè)州的總稅收。假設(shè)只有兩種稅,中央稅和州稅。中央稅很普遍,而州稅因州而異??偠愵~為兩者之和。這里為每個(gè)州實(shí)現(xiàn)了像 stateTax() 這樣的單獨(dú)方法,并從另一個(gè)方法 calculateTax() 調(diào)用此方法,如下所示:
static void calculateTax(stateTax() 函數(shù)的地址)
{
ct = 1000.0
st = 根據(jù)地址計(jì)算州稅
總稅額 = ct+st;
}
在前面的代碼中,stateTax() 的地址被傳遞給 calculateTax()。calculateTax() 方法將使用該地址調(diào)用特定州的 stateTax() 方法,并計(jì)算州稅“st”。
由于 stateTax() 方法的代碼從一種狀態(tài)變?yōu)榱硪环N狀態(tài),因此最好將其聲明為接口中的抽象方法,如:
接口標(biāo)準(zhǔn)稅
{
雙州稅();
}
以下是旁遮普邦的 stateTax() 的實(shí)現(xiàn):
類旁遮普實(shí)現(xiàn) STax{
公共雙州稅(){
返回 3000.0;
}
}
以下是 HP 狀態(tài)的 stateTax() 的實(shí)現(xiàn):
類 HP 實(shí)現(xiàn) STax
{
公共雙重 stateTax()
{
返回 1000.0;
}
}
現(xiàn)在,calculateTax() 方法可以設(shè)計(jì)為:
static void calculateTax(STax t)
{
// 計(jì)算中央稅
雙 ct = 2000.0;
// 計(jì)算州稅
雙 st = t.stateTax();
雙總稅 = st + ct;
// 顯示總稅額
System.out.println(“總稅額=”+totaltax);
}
在這里,觀察 calculateTax() 方法中的參數(shù)“STax t”。't' 是作為參數(shù)傳遞給方法的 'STax' 接口的引用。使用此引用,調(diào)用 stateTax() 方法,如下所示:
雙 st = t.stateTax();
此處,如果“t”引用 Punjab 類的 stateTax() 方法,則調(diào)用該方法并計(jì)算其稅金。同樣,對(duì)于類 HP。這樣,通過(guò)將接口引用傳遞給 calculateTax() 方法,就可以調(diào)用任何州的 stateTax() 方法。這稱為回調(diào)機(jī)制。
通過(guò)傳遞引用方法的接口引用,可以從另一個(gè)方法調(diào)用和使用該方法。
// Java program to demonstrate callback mechanism
// using interface is Java
// Create interface
import java.util.Scanner;
interface STax {
double stateTax();
}
// Implementation class of Punjab state tax
class Punjab implements STax {
public double stateTax()
{
return 3000.0;
}
}
// Implementation class of Himachal Pradesh state tax
class HP implements STax {
public double stateTax()
{
return 1000.0;
}
}
class TAX {
public static void main(String[] args)
throws ClassNotFoundException, IllegalAccessException, InstantiationException
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the state name");
String state = sc.next(); // name of the state
// The state name is then stored in an object c
Class c = Class.forName(state);
// Create the new object of the class whose name is in c
// Stax interface reference is now referencing that new object
STax ref = (STax)c.newInstance();
/*Call the method to calculate total tax
and pass interface reference - this is callback .
Here, ref may refer to stateTax() of Punjab or HP classes
depending on the class for which the object is created
in the previous step
*/
calculateTax(ref);
}
static void calculateTax(STax t)
{
// calculate central tax
double ct = 2000.0;
// calculate state tax
double st = t.stateTax();
double totaltax = st + ct;
// display total tax
System.out.println("Total tax =" + totaltax);
}
}
輸出:
輸入州名
旁遮普語(yǔ)
總稅額 = 5000.0
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743