PHP5 object model [10] - abstract method and abstract class

zhaozj2021-02-16  117

Section 10 - Abstract method and abstract class-oriented objects are built by the hierarchical structure. In a single inheritance language such as PHP, the inheritance of the class is a tree. A root class has one or more subclasses Then, then inherit one or more next level subclasses from each subclass. Of course, there may be multiple root classes to achieve different functions. In a well design system, each root should have one Useful interfaces can be applied by the application code. If our application code is designed to work with the root class, then it can also cooperate with any subclass that inherited from the root class. Abstract method is like subclass The general method's placeholder (depending on the place but does not work), it is different from the general method - there is no code. If there is one or more abstract methods in the class, then this class will become an abstract class. You can't instance Abstract class. You must inherit them, then instantiate the subclasses. You can also see the abstract class as a template for subclasses. If you override all abstract methods, subclasses become a normal class. If There is no way to overwrite all methods. If a class contains an abstract method (even if only one), you must declare this class is abstract, add Abstract to Abstract before the Class keyword. The syntax is different from the general method of statement. The abstract method is not contained in the body part of the braces {} as the general method, and ends with a semicolon; in Example 6.13, we define a class shape containing the Getarea method. However, since it is not known that the shape is not possible to determine the area of ​​the graphic, we declare that the getArea method is an abstract method. You can't instantiate a shape object, but you can inherit it or use it in an expression, just like this in an example 6.13. That. If you have established a class with an abstract method, you define an interface (Interface). In this case, there is interface and imports keywords in PHP. You can use Interface instead of abstract classes, use Implements Instead of Extends, you will clear your class definition or use an interface. For example, you can write a Myclass Implements MyIiterface. These two methods can be selected according to personal preferences. / * Note: Two methods refer to: 1. Abstract class aaa { (Note Only Abstract Method in AAA, there is no general method) Class BBB Extends AAA {} (Abstract Methods over AAA in BBB) 2. Interface AAA {} Class BBB IMPLEments AAA {} (in BBB overridden AAA Abstract method in * / liSTING 6.13 Abstract C lasses

转载请注明原文地址:https://www.9cbs.com/read-8133.html

New Post(0)