Home >Backend Development >PHP Tutorial >Section 10--Abstract methods and abstract classes--ClassesandObjectsinPHP510_PHP tutorial
/* +-------------------------------------------------- --------------------------------+ | = This article is read by Haohappy> | = Classes and Objects 1 Chapter's notes | = Translation + personal experience | = To avoid unnecessary trouble, please do not reprint, thank you | = Criticisms and corrections are welcome, and I hope to make progress together with all PHP enthusiasts! +------- -------------------------------------------------- -----------------------+ */ Section 10 - Abstract methods and abstract classes Object-oriented programs are built through the hierarchical structure of classes. In single layer In 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 lower-level subclasses. Of course, there may be multiple roots. Classes are 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 to work with the root class, Then it can also cooperate with any subclass that inherits from the root class. An abstract method is just like a placeholder for a general method in a subclass (it takes up a place but does not work). It is different from a general method - 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 can also think of abstract classes as subclasses. A template for a class. If you override all abstract methods, the subclass becomes a normal class. If you do not override all methods, the subclass is still abstract. If a class contains abstract methods (even if only a), 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. Abstract methods do not have a body enclosed in curly braces {} like general methods. parts, and ends with a semicolon;. In Example 6.13, we defined a class Shape that contains a getArea method. But since it is impossible to determine the area of the figure without knowing the shape, we declared the getArea method as an abstract method. You cannot Instantiates a Shape object, but you can inherit from it or use it in an expression, as in Example 6.13. If you create a class with only abstract methods, you define an interface. To illustrate In this case, PHP has the interface and implements keywords. You can use interface instead of abstract class, and implements instead of extends to describe your class definition or use an interface. For example, you can write a myClass implements myIterface. These two One method 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{} (overridden in bbb Abstract methods in aaa) 2. interface aaa{} class bbb implements aaa{} (override abstract methods in aaa in bbb) */ Listing 6.13 Abstract classes base * $this->height)/2) ; } public function getNumberOfSides() //Override side count method { return(3); } } //concrete class entity class quadrilateral class Rectangle extends Polygon { public $width; public $height; public function getArea() { return ($this->width * $this->height); } public function getNumberOfSides() { return(4); } } //concrete class entity class circle class Circle extends Shape { public $radius; public function getArea() { return(pi() * $this->radius * $this->radius); } } //concrete root class defines a color class class Color { public $name; } $myCollection = array(); //Create a shape collection, put into an array //make a rectangle $r = new Rectangle; $r->width = 5; $r->height = 7; $myCollection[] = $r; unset($r); //make a triangle $t = new Triangle; $t->base = 4; $t->height = 5; $myCollection[] = $t; unset($t); //make a circle $c = new Circle; $ c->radius = 3; $myCollection[] = $c; unset($c); //make a color $c = new Color; $c->name = "blue"; $myCollection[] = $c; unset($c); foreach($myCollection as $s) { if($s instanceof Shape) //If $s is an instance of the Shape class { print("Area: " . $s->getArea() . "