What are interfaces and API interfaces!

Today a friend asked me what interface? You usually say that you are writing interfaces, but what the hell are you writing? At the beginning, I thought, why did Xiao Chen suddenly understand the programming interface? But when I heard his last question, I knew that he was thinking of the API interface, but my subjective consciousness was used to thinking of it as an interface in the programming definition.

So I suddenly thought of writing something to explain the interface defined by programming and the meaning of API interface.

In order to distinguish and unify the two concepts, in the following full text接口 refers to the programmatically defined interface, and API refers to the API interface.

1. What is an interface?

1. The concept of interface
Give a definition, 接口是指定一组函数成员而不实现成员的引用类型,其他类型-类和结构可以实现接口。
Interface is something unique to strongly typed object-oriented programming languages, such as C#,Java, in layman's terms, its emergence is to define a standard entry for these strongly typed languages ​​and then use it for subsequent descendants to inherit.

To put it in a simpler way based on life, it is similar to the charging ports of mobile phones nowadays: Micro USB接口, USB Type C接口 and IOS mobile phones< a i=3>, these three interfaces are an interface definition, which stipulates the shape and size of the charging head, and then I don’t care how to implement charging. It doesn’t matter whether you made it a normal charging or a fast charging, and it doesn’t matter what you give. Mobile phone brand, what model of mobile phone is used. Lightning接口

2. Characteristics of interfaces
C# interface characteristics 1: They are all "virtual" and cannot be instantiated;
C# interface feature 2: Because the interface is virtual, the indexes, attributes, time, etc. within the interface can only be declared but cannot be implemented within the interface. How to implement it specifically? Derived interfaces or derived classes;
C# interface feature 3: They all have the nature of templates. If an interface or class inherits from a certain interface, it will Automatically have the characteristics of the integrator (including indexes, properties, functions, practices, etc.);
C# interface feature 4: The interface supports multiple inheritance, while C# In , classes support single inheritance, and interfaces actually represent a carrying capacity.

3. Interface definition and implementation
An interface can inherit from zero or more interfaces, and those are called explicit base interfaces of this interface. When an interface has more than zero explicit base interfaces, the interface definition takes the form: the interface identifier is followed by a colon ":" and a base interface separated by a comma "," List of identifiers.
If a class inherits an interface, it must implement all members defined by its base interface.
Look at the following code example:

interface IAnimal  
{   
   void Alive ( ) ;  
} 

interface IFlyingAnimals:IAnimal
{
  //定义一个索引
  string this[int index] { get; set; }
  //定义一个属性
  bool HasFeather { get; set;}
  //定义一个事件
 event EventHandler Changed;
  //定义一个方法
  bool CanFly(string s);
}
//支持多继承
interface IBird:IAnimal,IFlyingAnimals
{
  void CanCatchFish();
}

public class Animal:IAnimal
{
  public void Alive(){..实现内容..}
}

public class Bird:IBrid
{
  private void Alive ( ) ;  
  //实现一个索引
  public string this[int index] { get; set; }
  //实现一个属性
  public bool HasFeather { get; set;}
  //实现一个事件
 public event EventHandler Changed {..实现内容..};
  //实现一个方法
  public bool CanFly(string s){..实现内容..};
  public void CanCatchFish(){..实现内容..};
}
现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:310357728【暗号:csdn999】

2. What is API interface?

API:Application Program InterfaceThe API is a calling interface left by the operating system to the application program. The application program calls the operating system's API to cause the operating system to execute the application program's commands.

In layman's terms, API means that the system reserves a channel and performs some operations on the system according to the agreement. For example, the common one is to add, delete, modify and check the system data.

Maybe the above statements will still be confusing to non-programmers. Let’s take another example from life. If the bank is an application system now, and we want to go to the bank to get some money, then what operations do we need to do:

  1. go to bank
  2. Go to the bank warehouse and get some money
  3. Write this business down in your accounting book
  4. Leave the bank

So, obviously there is a problem:

It takes a lot of effort to open the warehouse door. Not everyone can open it, and some people are illiterate and cannot keep accounts.
In this model, we assume that everyone is honest, which is obviously not true. (Maybe someone took 10,000 yuan and wrote 100 yuan when accounting).
Now you are in trouble.

So how should we solve this problem?

At this time, we actually only need to find a professional bank employee Xiaona to be responsible for these operations, and build a counter to separate the warehouse and accounting books from the customers, so that customers no longer need to manage the warehouse and accounting books. If you want to save money, just contact Xiaona.

Under this model, if someone wants to withdraw money, they must:

  1. go to bank
  2. Tell Xiaona that I am MMM and I want to withdraw ¥¥¥.
  3. Xiaona opened the warehouse and took out the money
  4. Xiaona records the business (transaction)
  5. Tell you that the business is done
  6. Customer leaves the bank

Now there will be no more problems such as being unable to open the warehouse door or making mistakes in accounting. You feel very satisfied.

This bank employee Xiaona is equivalent to an API interface. We hire a cashier to help customers solve a series of tedious processes such as opening warehouse doors, depositing money, and keeping accounts. This greatly saves customers' time and improves the bank's efficiency. In the same way, if we abstract the specific implementation steps of the operation, this will greatly reduce the burden on developers (the effort it takes to remember all the steps), thus greatly improving efficiency.

APIs have a wide range of applications: from simple fork() in the operating system to the Baidu Map API and weather API we are exposed to, these APIs greatly simplify the work of programmers, without having to reinvent the wheel.

When we use the API, we need to follow the API protocol, otherwise we will report an error or not get the desired effect. It's like your deposit in the bank is only 100 yuan, but you insist on withdrawing 1,000 yuan. In this way, you are following the agreement.

In API terms, a protocol is a set of rules that specify how parts communicate with each other. To complete the interaction, each part must understand and abide by the same protocol.

When we design an API for our own system, generally we need to establish a firewall for the API to protect your resources from abuse while allowing legitimate requests to pass.
Firewall principles:

  1. The API can confirm that all operations are legal.
  2. When an error occurs, the API will issue instructions based on the error reporting mechanism to reduce the burden on developers.
  3. Authorization and access controls are built into the API to ensure that only authorized personnel can access specific data.
  4. We can implement rate limits to control the use of server resources to ensure users do not abuse the service.

3. Summary

Therefore, the functions of interfaces and APIs are to define a specification. Interfaces are defined at the code level, while APIs are defined at the implementation level, making work more efficient and reusable.

Finally, I would like to thank everyone who read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are from the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
 

Insert image description here

Guess you like

Origin blog.csdn.net/IT_LanTian/article/details/134950810