HOWTO · Java
在 Java 中创建回调函数
本教程演示如何在 Java 中创建和使用回调函数。
本页内容
回调功能用于事件驱动编程。当相应事件发生时,引用将传递给调用的函数。
我们使用接口来实现 Java 中的回调,因为它不支持函数指针。
本教程演示如何在 Java 中创建和使用回调函数。
在 Java 中使用接口创建回调函数
Java 中的接口是指定类行为的抽象类型。它是一个类的蓝图类。
我们可以创建一个接口和多个类来演示 Java 中的回调。
下面的代码实现了一个接口和四个类来计算员工的工资。要调用函数,我们传递接口的引用;这是回调。
该代码通过从总工资中减去 10% 来计算净工资。查看每个类的输出;我们运行代码四次。
import java.util.Scanner;
// Create interface
interface Salary {
double Person_Salary();
}
// Class for Jack's Salary
class Jack implements Salary {
public double Person_Salary() {
return 5000.0;
}
}
// Class for Michelle's Salary
class Michelle implements Salary {
public double Person_Salary() {
return 4000.0;
}
}
// Class for Jhonny's Salary
class Jhonny implements Salary {
public double Person_Salary() {
return 3000.0;
}
}
// Class for Mike's Salary
class Mike implements Salary {
public double Person_Salary() {
return 3500.0;
}
}
class Employee_Salary {
public static void main(String[] args)
throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Employee Name: ");
// name of the employee
String Employee = sc.next();
// Store the employee name in an object temp
Class temp = Class.forName(Employee);
// Create the new object of the class whose name is in temp
// Interface Salary's reference is now referencing the new object created above
Salary reference = (Salary) temp.newInstance();
// Call the method to calculate net salary and pass interface reference - this is a callback.
// Here reference refers to the function Person_Salary() of both Jack and Michelle's classes
Calculate_Salary(reference);
}
static void Calculate_Salary(Salary E_Salary) {
// calculate Salary Deduction Which is 10 percent
double salary_deduction = E_Salary.Person_Salary() * 0.1;
// The gross salary
double gross_salary = E_Salary.Person_Salary();
// Calculate the net salary
double net_salary = gross_salary - salary_deduction;
// Show the net salary
System.out.println("The Net Salary of Employee is =" + net_salary);
}
}
输出:
Enter the Employee Name:
Jack
The Net Salary of Employee is =4500.0
Enter the Employee Name:
Michelle
The Net Salary of Employee is =3600.0
Enter the Employee Name:
Jhonny
The Net Salary of Employee is =2700.0
Enter the Employee Name:
Mike
The Net Salary of Employee is =3150.0
在 Java 中创建同步和异步回调函数
同步回调
代码执行将等待或阻止同步回调的事件,直到它返回响应。回调将在返回调用语句之前执行其所有工作。
同步有时似乎滞后。下面的代码为回调函数实现了一个简单的同步任务。
// Create Interface
interface Delftstack {
void delftstack_event();
}
class Delftstack_Two {
// Delftstack Field
private Delftstack Delft;
// setting the register_delftstack function
public void register_delftstack(Delftstack Delft) {
this.Delft = Delft;
}
// This is our synchronous task
public void Hello_Delftstack() {
// perform any operation
System.out.println("Hello! This is delftstack callback from before the synchronous task.");
// check if listener is registered.
if (this.Delft != null) {
// invoke the callback method of class A
Delft.delftstack_event();
}
}
// Main
public static void main(String[] args) {
Delftstack_Two Demo_Object = new Delftstack_Two();
Delftstack Delft = new Delftstack_One();
Demo_Object.register_delftstack(Delft);
Demo_Object.Hello_Delftstack();
}
}
class Delftstack_One implements Delftstack {
@Override
public void delftstack_event() {
System.out.println("Hello! This is delftstack callback from after the synchronous task.");
}
}
输出:
Hello! This is delftstack callback from before the synchronous task.
Hello! This is delftstack callback from after the synchronous task.
异步回调
另一方面,异步回调不会阻止代码执行。
在 Java 中,我们需要创建一个线程来开发一个异步任务,并在里面实现回调。当调用从事件返回时,它返回到异步的回调函数。
下面的代码示例为回调函数实现了一个简单的异步任务。
// Create Interface
interface Delftstack {
void delftstack_event();
}
class Delftstack_Two {
// Delftstack Field
private Delftstack Delft;
// setting the register_delftstack function
public void register_delftstack(Delftstack Delft) {
this.Delft = Delft;
}
// The Asynchronous
public void Hello_Delftstack() {
// An Asynchronous must be executed in a new thread
new Thread(new Runnable() {
public void run() {
System.out.println("Hello! This is delftstack operation inside the asynchronous task.");
// check if Delft is registered.
if (Delft != null) {
// invoke the callback method of class A
Delft.delftstack_event();
}
}
}).start();
}
// Main
public static void main(String[] args) {
Delftstack_Two Demo_Object = new Delftstack_Two();
Delftstack Delft = new Delftstack_One();
Demo_Object.register_delftstack(Delft);
Demo_Object.Hello_Delftstack();
}
}
class Delftstack_One implements Delftstack {
@Override
public void delftstack_event() {
System.out.println("Hello! This is delftstack callback from after the asynchronous task.");
}
}
输出:
Hello! This is delftstack operation inside the asynchronous task.
Hello! This is delftstack callback from after the asynchronous task.