Interface (interface) technology What is an interface?
First look at the abstract class:
Abstract class class name {
attribute 1;
attribute 2;
.....
non-abstract method 1;
non-abstract method 2;
......
Abstract method 1;
Abstract Method 2;
......
}
It is envisaged that the "real members" of the above abstract classes be deleted, that is, those non-abstract members. Then, in the abstract class, there is only abstract method;
Abstract class class name {
Abstract method 1;
Abstract Method 2;
......
}
Thus, it can be understood as: This abstract class,"too abstract", almost nothing to do, just let others do what.
So:
interface is such a "natural not real" guy:
interface, is the stipulation, inside can only put "abstract method" and "constant" a similar class of grammatical structure;
It can be seen that an interface is a grammatical structure that is "more abstract than abstract class".
Interface (interface) definition form:
Interface Interface name {
constant 1;
constant 2;
.....
Abstract method 1;
Abstract Method 2;
.....
}
Description
1, visible, interface, only constants (interface constants) and abstract methods of the two members;
2, the interface constant is used as: interface name:: constant name;
3, the abstract method in the interface, do not use the abstract adornment, do not need to use the access control modifier, because its natural is public
Why do I need an interface?
The idea of object-oriented programming is a description of the "Real World" (simulation)!
The real world is often more inherited;
But:
In order to reduce the complexity of the relationship between classes and classes, the language is designed to be single-inheritance;
However, it is impossible to express the multi-inheritance characteristics of the real world;
The
Interfaces are a complement to the relationships between classes that do not have multiple inheritance;
because: Interfaces can implement "multiple inheritance"--but this is not called inheritance, but is called "implementation";
That
interface 1-->> class 1;
This is called: Class 1 implements interface 1;
its essence, in fact, is class 1 , with the interface 1 "feature information";
form of Use:
The form is:
Class name implements interface name 1, interface name 2, .... {
the definition of the class.
}
Here, called, the class implements the interface.
Among them, the interface and the interface, can also inherit, and the inheritance between the classes:
Interface Interface 1 extends interface 2{
member definition of an interface;
}
a large summary of classes and interfaces
PHP Object-oriented interface