Family mission is the unswerving goal of some families. In the process of object-oriented programming, the abstract class Abstract is such a family mission. If the goal is not completed one day, it will always be an abstract class. The purpose of the abstract class Abstract is to use the parent class to enforce the specification of what the subclass must complete.
1. Definition:
Abstract class, a class modified with the abstract
keyword, indicates that the class can only be inherited and cannot be instantiated. change.
2. Basic syntax:
Use the abstract
keyword to modify the class.
<?php abstract class People{} //$a=new People();//抽象类不能够被实例化会报错 class Man extends People{}//正确 ?>
Using the abstract
keyword can also be used to modify methods (Abstract Method
). The abstract
modified method cannot have a method body, and Classes with abstract methods must be declared as abstract classes.
<?php abstract class People{ public function show(){} //普通方法有方法体 abstract public function eat(); //定义抽象方法:没有方法体 // 两者的主要区别:{},修饰符(因为抽象方法要被实现, //所以不能为private) } ?>
After a subclass inherits abstract class
, if there are abstract methods in the abstract class, then the subclass must implement all abstract methods
. If not all abstract methods are implemented, It will still be abstract class
(until all methods are implemented, otherwise it will always be abstract class
).
abstract class People{ abstract public function eat(); public function show(){} //普通方法有方法体 } abstract class Man extends People{} //正常继承,(未实现抽象方法)抽象类继承抽象类 class Boy extends Man{ //子类实现父类所有抽象方法 //实现从祖父类继承的eat抽象方法 public function eat(){ echo 'eat'; } }
Recommended: php tutorial,php video tutorial
The above is the detailed content of The continuation of the spirit of Yu Gong——Abstract class. For more information, please follow other related articles on the PHP Chinese website!