Home Backend Development PHP Tutorial Section 10--Abstract methods and abstract classes--ClassesandObjectsinPHP510_PHP tutorial

Section 10--Abstract methods and abstract classes--ClassesandObjectsinPHP510_PHP tutorial

Jul 13, 2016 pm 05:24 PM
and abstract method kind

/* +-------------------------------------------------- --------------------------------+ | = 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() . "


n"); } if($s instanceof Polygon) { print("Sides: " . $s->getNumberOfSides() . "
n"); } if($s instanceof Color) { print( "Color: $s->name
n"); } print("
n"); } ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532155.htmlTechArticle/* +--------------------- -------------------------------------------------- --------+ | = This article is read by Haohappy> | = Notes from the Chapter Classes and Objects| = Translation + personal experience...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel.

How to delete WeChat friends? How to delete WeChat friends How to delete WeChat friends? How to delete WeChat friends Mar 04, 2024 am 11:10 AM

How to delete WeChat friends? How to delete WeChat friends

How to enter bios on Colorful motherboard? Teach you two methods How to enter bios on Colorful motherboard? Teach you two methods Mar 13, 2024 pm 06:01 PM

How to enter bios on Colorful motherboard? Teach you two methods

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts)

Summary of methods to obtain administrator rights in Win11 Summary of methods to obtain administrator rights in Win11 Mar 09, 2024 am 08:45 AM

Summary of methods to obtain administrator rights in Win11

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Mar 23, 2024 am 10:42 AM

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed!

Detailed explanation of Oracle version query method Detailed explanation of Oracle version query method Mar 07, 2024 pm 09:21 PM

Detailed explanation of Oracle version query method

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)

See all articles