/*
+-------------------------------------------------- ----------------------------------+
| = This article is read by Haohappy<
| = Notes from the Chapter Classes and Objects
| = Translation + personal experience
| = To avoid possible unnecessary trouble, please do not reprint, thank you
| = We welcome criticisms and corrections, and hope to make progress together with all PHP enthusiasts!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+---------- -------------------------------------------------- ------------------+
*/
Section 10--Abstract methods and abstract classes
Object-oriented programming through the hierarchical structure of classes Build it up. In single inheritance languages such as PHP, class inheritance is tree-like. A root class has one or more subclasses, and each subclass inherits one or more subclasses. Of course. , there may be multiple root classes used to implement different functions. In a well-designed system, each root class should have a useful interface that can be used by application code. If our application code is designed If it works with the root class, it can also work with any subclass that inherits from the root class.
Abstract methods are placeholders for general methods in subclasses (take up space but do not work) , which is different from general methods - there is no code. If one or more abstract methods exist in a class, then the class becomes an abstract class. You cannot instantiate abstract classes. You must inherit them and then instantiate subclasses. You You can also think of an abstract class as a template for a subclass.
If you override all abstract methods, the subclass becomes an ordinary class. If you do not override all methods, the subclass is still abstract. If a class contains an abstract method (even if there is only one), you must declare the class to be abstract by adding abstract before the class keyword.
The syntax for declaring abstract methods is different from declaring general methods. The syntax of abstract methods There is no body part enclosed in curly braces {} like a normal method, and ended with a semicolon;.
In Example 6.13, we defined a class Shape that contains the getArea method. But it is impossible because we don’t know the shape. To determine the area of the shape, we declared the getArea method as abstract. You cannot instantiate a Shape object, but you can inherit from it or use it in an expression, as in Example 6.13.
If you When you create a class with only abstract methods, you define an interface. To illustrate this situation, PHP has the interface and implements keywords. You can use interface instead of abstract class and implements instead of extends to illustrate this. Your class defines or uses an interface. For example, you can write a myClass implements myIterface. These two methods can be chosen according to personal preference.
/*Note:
The two methods refer to:
1 . abstract class aaa{} (note that there are only abstract methods in aaa, no general methods)
class bbb extends aaa{} (overwrite the abstract methods in aaa in bbb)
2. interface aaa{}
class bbb implements aaa{} (override abstract methods in aaa in bbb)
*/
Listing 6.13 Abstract classes