Advanced learning of of abstract classes interfaces final class constants

  • 2020-05-17 04:54:37
  • OfStack

1. Image extraction class (abstract)
In our actual development process, some classes do not need to be instantiated, such as the parent classes we learned earlier, which are mainly inherited by subclasses to improve code reuse
Grammatical structure:
 
abstract class  The name of the class { 
 attribute  $name; 
 methods (){} // The method can also be abstract  The modifier  function  The method name (){} 
} 

Ex. :
 
abstract class animal{ 
public $name; 
public $age; 
// Abstract methods cannot have method bodies, mainly for subclasses to implement ; 
abstract public function cry(); 
// Abstract classes can contain abstract methods as well as instance class methods  
public function getname(){ 
echo $this->name; 
} 
} 
class Cat{ 
public function cry(){ 
echo 'ok'; 
} 
} 

Understanding: the animal class is actually an abstract concept, which specifies what common attributes and behaviors some animals have, but in fact it does not confiscate those attributes and behaviors. Another example: vehicle class, plant class and so on

Note:
1. If a class is decorated with abstract, the class is an abstract class; if a method is decorated with abstract, the method is an abstract method, and the abstract method cannot have method body = > abstract function cry (); You can't even have {}
2. Abstract class 1 must not be instantiated. Abstract class 1 can have no abstract method, but if a class contains any abstract method, the class 1 must be declared as abstract class.
3. If one class inherits another abstract class, that subclass must implement all abstract methods in the abstract class (unless it is declared as an abstract class itself);

2. Interface (interface)
Interface is to encapsulate 1 some unimplemented methods in 1, to a certain class to use, and then write out these methods according to the specific situation;
Grammatical structure
interface interface name {
// properties, methods
// no method in the interface can have a method body;
}
How to implement an interface
class class name implements interface name {

}
Understanding: an interface is a more abstract abstract class. A method in an abstract class can have a method body, but a method in an interface must have no method body. The interface realizes the design idea of polymorphic programming, high cohesion and low coupling.

Ex. :
 
// Interfaces define specifications, properties, 1 Usually in lowercase i At the beginning.  
interface iUsb{ 
public function start(); 
public function stop(); 
} 
// Write the camera class to implement the interface  
// when 1 Each class implements an interface, so the class must implement all the methods of the interface  
class Camera implements iUsb{ 
public function start(){ 
echo 'Camera Start Work'; 
} 
public function stop(){ 
echo 'Camera Stop Work'; 
} 
} 
// write 1 A mobile phone class  
class Phone implements iUsb{ 
public function start(){ 
echo 'Phone Satrt Work'; 
} 
public function stop(){ 
echo 'Phone Stop Work'; 
} 
} 
$c=new Camera(); 
$c->start(); 
$p=new Phone(); 
$p->start(); 

When to use the interface:
1. Set the specification and let other programmers implement it
2. When there are multiple parallel classes, a certain function needs to be implemented, but the way of implementation is not the same;

Summary:
1. The interface cannot be instantiated, and all methods in the interface cannot have bodies;
2. One class can implement multiple interfaces, separated by comma (,) class demo implements if1,if2,if3{}
3, interface can have properties, but must be constant, constant can not have modifier (default is public modifier)
Such as: interface iUsb {
const A=90;
}
echo iUsb::A;
4. All methods in the interface must be public, the default is public;
5. An interface cannot inherit other classes, but it can inherit other interfaces. An interface can inherit multiple other interfaces
For example: interface interface name extends if1,if2{}
6. A class can inherit from its parent and implement other interfaces at the same time
class test extends testbase implements test1,test2{}

Implement the interface VS inheritance class
The inheritance of php is single 1 inheritance, that is, one class can only inherit one parent class, which has a definite influence on the extension of subclass functions. The implementation interface can be seen as a complement to the inheritance class. Inheritance is a hierarchical relationship, which is not very flexible, while the implementation interface is a horizontal relationship, which can be very flexible to extend a certain function without breaking the inheritance relationship.

3. Final

1, if we want a class not to be inherited by other classes (for security reasons, etc.). , then you can consider using final
Grammar:
final class A{}
2. If we want a method not to be subclassed, we can consider modifying it with final. The method modified with final can still be inherited, because the inheritance of the method depends on the modification of public
Such as:
 
class A{ 
final public function getrate($salary){ 
return $salary*0.08; 
} 
} 
class B extens A{ 
// This is the parent getrate The method USES final So I can't rewrite this getrate 
//public function getrate($salary){ 
// return $salary*0.01; 
//} 
} 

3. final cannot be used to modify properties

4. Class constants (const)

In some cases, there may be a requirement that you use the const constant when you don't want a member variable to be modified and you want the value of that variable to be fixed.
Grammar:
const constant name = constant value; // must be given an initial value because constants cannot be changed
Call:
Class name :: constant name [self:: constant name used internally] or interface name :: constant name // only constants can be used in the interface, not variables

Such as:
 
class A{ 
const TAX_RATE=0.08; 
function paytax($salary){ 
return $salary*self::TAX_RATE; 
} 
} 
$a=new A(); 
echo $a->paytax(100); 

Note:
1. Constants can be inherited by subclasses
2. Constants belong to a class, not to an object

Related articles: