I. Abstract classes and Interfaces
1. Abstract classes: Classes that have abstract methods are abstract ( without abstract member properties )
Abstract method: A method without a method body is an abstract method.
What is no method body? No curly braces
All abstract and abstract methods require the abstract keyword decoration
An abstract class can have multiple abstract methods, or you can have multiple non-abstract methods
Abstract methods cannot be declared as private
When inheriting an abstract class, the method also adheres to the 3,2,1 principles
When inheriting an abstract class, the number of formal parameters in the method body must be consistent with the abstract method
When inheriting an abstract class, if the formal parameter in the abstract method has a default value, the implemented method must also have a default value, which can be a different value.
Abstract classes can have member properties
Example: Abstract class demo{
Abstract function Show ($n = ' abc ');
Abstract function test ();
function say () {
echo " green hills do not change the long stream, you will come back!! ";
}
}
Class Son extends demo{
Function Show ($abc =10) {}
function Test () {}
}
$s =new Son;
$s->say ();
2. Interface Technology
Interface Interface
All methods are abstract, this class is interface, interface we do not use class declaration, with interface Declaration
Interfaces cannot have member properties, but can have constants
An interface can be implemented by a class (implements), which must all implement all the abstract methods in the interface
A class can inherit only one parent class (abstract class), but multiple interfaces may be implemented
A class can implement an interface in addition to inheriting it.
A class is the implementation of an interface after first inheriting
Interfaces and interfaces can also inherit
Example: interface demo{
function Show ();
function test ();
}
Interface Demo2 extends demo{
function Show2 ();
}
Class Father {
function say () {
Echo ' said ';
}
Function Show () {
}
}
Class Son extends Father implements demo2{
Function Show () {
}
function Test () {
}
function Show2 () {
}
}
$s =new Son;
$s->say ();
PHP Object-oriented-abstract classes and interfaces