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:

  1. The file is named MyInterface.java.
  2. 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);
}
  1. 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;
    }
}
  1. 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.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds