Home php教程 php手册 第十节--抽象方法和抽象类--ClassesandObjectsinPHP510

第十节--抽象方法和抽象类--ClassesandObjectsinPHP510

Jun 13, 2016 am 10:24 AM
and abstract method kind

/* +-------------------------------------------------------------------------------+ | = 本文为Haohappy读> | = 中Classes and Objects一章的笔记 | = 翻译为主+个人心得 | = 为避免可能发生的不必要的麻烦请勿转载,谢谢 | = 欢迎批评指正,希望和所有PHP爱好者共同进步! +-------------------------------------------------------------------------------+ */ 第十节--抽象方法和抽象类 面向对象程序通过类的分层结构构建起来. 在单重继承语言如PHP中, 类的继承是树状的. 一个根类有一个或更多的子类,再从每个子类继承出一个或更多下一级子类. 当然,可能存在多个根类,用来实现不同的功能. 在一个良好设计的体系中,每个根类都应该有一个有用的接口, 可以被应用代码所使用. 如果我们的应用代码被设计成与根类一起工作,那么它也可以和任何一个从根类继承出来的子类合作. 抽象方法是就像子类中一般的方法的占位符(占个地方但不起作用),它与一般方法不同—没有任何代码. 如果类中存在一个或更多抽象方法, 那么这个类就成了抽象类. 你不能实例化抽象类. 你必须继承它们,然后实例化子类. 你也可以把抽象类看成是子类的一个模板. 如果你覆写所有的抽象方法, 子类就变成一个普通的类. 如果没有覆写所有方法, 子类仍是抽象的. 如果一个类中中包含有抽象方法(哪怕只有一个), 你必须声明这个类是抽象的, 在class关键字前加上abstract. 声明抽象方法的语法与声明一般方法不同. 抽象方法的没有像一般方法那样包含在大括号{}中的主体部份,并用分号;来结束. 在例子6.13中, 我们定义了一个含有getArea方法的类Shape. 但由于不知道形状不可能确定图形的面积,确良我们声明了getArea方法为抽象方法. 你不能实例化一个Shape对象,但你可以继承它或在一个表达式中使用它, 就像例6.13中那样. 如果你建立了一个只有抽象方法的类,你就定义了一个接口(interface). 为了说明这种情况, PHP中有interface 和implements关键字. 你可以用interface来代替抽象类, 用implements来代替extends来说明你的类定义或使用一个接口. 例如, 你可以写一个myClass implements myIterface. 这两种方法可以依个人偏爱来选择. /*注: 两种方法即指: 1. abstract class aaa{} (注意aaa中只有抽象方法,没有一般方法) class bbb extends aaa{} (在bbb中覆写aaa中的抽象方法) 2. interface aaa{} class bbb implements aaa{} (在bbb中覆写aaa中的抽象方法) */ Listing 6.13 Abstract classes base * $this->height)/2); } public function getNumberOfSides() //覆写边数统计方法 { return(3); } } //concrete class 实体类四边形 class Rectangle extends Polygon { public $width; public $height; public function getArea() { return($this->width * $this->height); } public function getNumberOfSides() { return(4); } } //concrete class 实体类 圆形 class Circle extends Shape { public $radius; public function getArea() { return(pi() * $this->radius * $this->radius); } } //concrete root class 定义一个颜色类 class Color { public $name; } $myCollection = 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) //如果$s是Shape类的实例 { 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"); } ?>
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!

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)

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

See all articles