为什么 Retrofit 使用接口而不是普通的 java class?

Why does Retrofit use Interface instead of a normal java class?

在我的 Android 中,我正在使用 Retrofit 来实现一个 http 客户端。
Retrofit 使用接口来定义可能的 http 操作。

userInterface.java

UserInterface.java
public interface UserInterface {
   // Retrofit callback
   @POST("login")
   Call<Integer> signin(@Body LoginActivity.UserInfo userInfo);
}

此用户界面随后在 loginActivity.java 中通过改造使用:

loginActivity.java

// It makes sense. Retrofit will use the interface as a configuration file
UserInterface userInterface = ApiClient.getApiClient().create(UserInterface.class);

ApiClient.java

public static Retrofit getApiClient(){
   if (retrofit==null){
       retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
               .addConverterFactory(GsonConverterFactory.create())
               .build();
   }
   return retrofit;
};

我完全了解 Retrofit Client 配置及其工作原理。但是,我不明白为什么 userInterface 是 java 界面而不是普通的 java class?
我知道简而言之,这就是接口:

An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.

Writing an interface is similar to writing a class. But a class describes the attributes and behaviors of an object. And an interface contains behaviors that a class implements.

Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.

我也了解了Retrofit接口的特殊作用:

You can see the interface as a configuration file, it holds info (method declaration , return type and params, annotations) about each endpoint (link a method name to url, GET or POST, parameters and parameter types, return value type , ... and more). the engine uses those info to serialize parameters (if needed), execute the request and deserialize the response (if needed).

但是,我仍然无法向自己解释使用接口而不是普通接口java class?

如果您查看在运行时创建 classes 的 Retrofit library, in particular the create() method of Retrofit.java class, you can see that they're using Java's Dynamic Proxy 方法的源代码。

此机制需要一个接口(或接口列表)才能创建具有特定行为的代理 classes。这就解释了为什么使用接口而不是正常的 java class.