Article From:

In most object-oriented programming languages, the concept of Interface (Interface) is provided. If you have learned this concept beforehand, will you think of this concept when it comes to “interface testing”? This article briefly introduces the Interface in object oriented programming language..





  Defining interfaces in Java uses the interface keyword to declare it as a special abstract class, which can specify what a class must do, not how to do it.


  Why use the interface?

  In large project development, it is possible to insert a class from the middle of the inheritance chain, so that its subclasses have some functions without affecting their parent classes. For example, A, -&gt, B, -&gt, C, -&gt, D, -&gt, E and A are ancestors.The simplest way to add some generic functions to a class is to let the C class inherit another class. But the problem is that Java is a single inherited language that can no longer allow C to inherit another parent, only to move to the top of the inheritance chain, and let A inherit a parent class. So, for C, D,The modification of E class affects the whole inheritance chain and does not have the design of insertable nature.

  The interface is a guarantee of intercalability. Any class in an inheritance chain can implement an interface that affects all subclasses of such a class, but does not affect any parent of such a class. This class will have to implement the method specified by this interface, and subclasses can inherit them automatically from this class.At this time, these subclasses have intercalability.

  What we care about is not what specific class is, but whether the class implements the interface we need.

  The interface provides Association and the intercalability on the method call. The larger the scale of the software system, the longer the life cycle, the interface makes the flexibility and extensibility of the software system, and the pluggable aspect of the software system.

  Interface plays an important role in object-oriented Java programming. In fact, one of the most important tasks in the design phase is to design the interfaces of all parts, and then form the basic framework of the program through the combination of interfaces.

  Therefore, a brief summary of its purpose is to achieve multiple inheritance of classes so as to solve the problem that Java can inherit only one thing and does not support multiple inheritance.

  Below is an example of the use of interfaces in Java.

  Define the interface (

Copy code
package mypor.interfaces.demo;

public interface IAnimal {

    public String Behavior(); //A method of behavior that describes the characteristics of various animals.}
Copy code


Implement the interface one (

Copy code
package mypor.interfaces.demo;
import mypor.interfaces.demo.IAnimal;

//Class: dogPublic class Dog implements IAnimal{Public String Behavior (){String ActIveTime = "I sleep at night, daytime activity";Return ActiveTime;}}
Copy code


Implement interface two (

Copy code
package mypor.interfaces.demo;
import mypor.interfaces.demo.IAnimal;

//Class: catPublic class Cat implements IAnimal{Public String Behavior (){String ActiveTiMe = "I sleep during the day and catch mice at night. ";Return ActiveTime;}}
Copy code


The implementation of the test interface:

Copy code
package mypor.interfaces.demo;
import mypor.interfaces.demo.Dog;
import mypor.interfaces.demo.Cat;

public class Test {
    public static void main(String[] args) {         
        //The behavior of calling dog and catDog d = new Dog ();Cat C = new Cat ();System.out.println (d.BehAvior ());System.out.println (c.Behavior ());}}
Copy code

  Note that the test here is not the interface of the test, because the interface itself is simply a simple definition, nothing to be tested, where the real test is the class that inherits the interface, or the object that has been instantiated.






  If you are as familiar with Python as I am, do you want to know if there is an interface (Interface) concept in Python, and Python itself does not provide the creation and use of the port, but we can use the interface through the third party extension library, that is, ZopE.interface.

  Download address:

    Look at an ordinary example:

Copy code
class Host(object):

    def goodmorning(self, name):
        """Say good morning to guests"""
        return "Good morning, %s!" % name

if __name__ == '__main__':
    h = Host()
    hi = h.goodmorning('zhangsan')
Copy code


The following use the interface in this example:

Copy code
from zope.interface import Interface
from zope.interface import implements

# Defining interfaceClass IHost (Interface):Def GoodMorning (self, guest):"" "Say good morning to guest"""Class Host (object):Implements (IHost) implementation interfaceDef GoodMorning (self, guest):""Say good morning to guests" "" "Return "Good morning,%s!"% guestIf __name__ = ='__main_":"H = Host ()Hi = h.goodmorning ('zhangsan')Print (HI)
Copy code


  By looking at the example of this article, I think the interface is a very boring concept, and I feel the same way. Haha! If you really encounter specific projects that use interfaces to make the structure more elegant, you will realize its significance.

Leave a Reply

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