How to create an interface in Java for external system invocation?
To define an interface for external system calls in Java, you can follow these steps:
- The file is named MyInterface.java.
- In this class, methods are defined using interfaces, where the method declaration only includes the method name, parameter list, and return type, without the method body. For example:
public interface MyInterface {
void myMethod();
int add(int a, int b);
}
- The file named MyClass.java
public class MyClass implements MyInterface {
@Override
public void myMethod() {
// 实现接口中的方法逻辑
}
@Override
public int add(int a, int b) {
// 实现接口中的方法逻辑
return a + b;
}
}
- In external systems, the methods in the interface can be invoked in the following ways:
MyInterface myObject = new MyClass();
myObject.myMethod();
int result = myObject.add(2, 3);
This way, the defined interface methods can be called in external systems.