search
Homephp教程php手册PHP5.0对象模型探索之抽象方法和抽象类
PHP5.0对象模型探索之抽象方法和抽象类Jun 13, 2016 pm 12:46 PM
layeredandexistobjectabstractexploremethodConstructModelofprogramkindstructurepassFor

  面向对象程序通过类的分层结构构建起来,在单重继承语言如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

//abstract root class 抽象根类
abstract class Shape
{
 abstract function getArea(); //定义一个抽象方法
}

//abstract child class 抽象子类
abstract class Polygon extends Shape //多边形
{
 abstract function getNumberOfSides();
}

//concrete class 实体类 三角形类
class Triangle extends Polygon
{
 public $base;
 public $height;

 public function getArea() //覆写计算面积方法
 {
  return(($this->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
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
使用PHP的json_encode()函数将数组或对象转换为JSON字符串使用PHP的json_encode()函数将数组或对象转换为JSON字符串Nov 03, 2023 pm 03:30 PM

JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式,已经成为Web应用程序之间数据交换的常用格式。PHP的json_encode()函数可以将数组或对象转换为JSON字符串。本文将介绍如何使用PHP的json_encode()函数,包括语法、参数、返回值以及具体的示例。语法json_encode()函数的语法如下:st

源码探秘:Python 中对象是如何被调用的?源码探秘:Python 中对象是如何被调用的?May 11, 2023 am 11:46 AM

楔子我们知道对象被创建,主要有两种方式,一种是通过Python/CAPI,另一种是通过调用类型对象。对于内置类型的实例对象而言,这两种方式都是支持的,比如列表,我们即可以通过[]创建,也可以通过list(),前者是Python/CAPI,后者是调用类型对象。但对于自定义类的实例对象而言,我们只能通过调用类型对象的方式来创建。而一个对象如果可以被调用,那么这个对象就是callable,否则就不是callable。而决定一个对象是不是callable,就取决于其对应的类型对象中是否定义了某个方法。如

使用Python的__contains__()函数定义对象的包含操作使用Python的__contains__()函数定义对象的包含操作Aug 22, 2023 pm 04:23 PM

使用Python的__contains__()函数定义对象的包含操作Python是一种简洁而强大的编程语言,提供了许多强大的功能来处理各种类型的数据。其中之一是通过定义__contains__()函数来实现对象的包含操作。本文将介绍如何使用__contains__()函数来定义对象的包含操作,并且给出一些示例代码。__contains__()函数是Pytho

使用Python的__le__()函数定义两个对象的小于等于比较使用Python的__le__()函数定义两个对象的小于等于比较Aug 21, 2023 pm 09:29 PM

标题:使用Python的__le__()函数定义两个对象的小于等于比较在Python中,我们可以通过使用特殊方法来定义对象之间的比较操作。其中之一就是__le__()函数,它用于定义小于等于比较。__le__()函数是Python中的一个魔法方法,并且是一种用于实现“小于等于”操作的特殊函数。当我们使用小于等于运算符(<=)比较两个对象时,Python

详解Javascript对象的5种循环遍历方法详解Javascript对象的5种循环遍历方法Aug 04, 2022 pm 05:28 PM

Javascript对象如何循环遍历?下面本篇文章给大家详细介绍5种JS对象遍历方法,并浅显对比一下这5种方法,希望对大家有所帮助!

Python中如何使用getattr()函数获取对象的属性值Python中如何使用getattr()函数获取对象的属性值Aug 22, 2023 pm 03:00 PM

Python中如何使用getattr()函数获取对象的属性值在Python编程中,我们经常会遇到需要获取对象属性值的情况。Python提供了一个内置函数getattr()来帮助我们实现这个目标。getattr()函数允许我们通过传递对象和属性名称作为参数来获取该对象的属性值。本文将详细介绍getattr()函数的用法,并提供实际的代码示例,以便更好地理解。g

使用Python的isinstance()函数判断对象是否属于某个类使用Python的isinstance()函数判断对象是否属于某个类Aug 22, 2023 am 11:52 AM

使用Python的isinstance()函数判断对象是否属于某个类在Python中,我们经常需要判断一个对象是否属于某个特定的类。为了方便地进行类别判断,Python提供了一个内置函数isinstance()。本文将介绍isinstance()函数的用法,并提供代码示例。isinstance()函数可以判断一个对象是否属于指定的类或类的派生类。它的语法如下

PHP代码封装技巧:如何使用类和对象封装可重复使用的代码块PHP代码封装技巧:如何使用类和对象封装可重复使用的代码块Jul 29, 2023 pm 11:19 PM

PHP代码封装技巧:如何使用类和对象封装可重复使用的代码块摘要:在开发中,经常遇到需要重复使用的代码块。为了提高代码的可维护性和可重用性,我们可以使用类和对象的封装技巧来对这些代码块进行封装。本文将介绍如何使用类和对象封装可重复使用的代码块,并提供几个具体的代码示例。使用类和对象的封装优势使用类和对象的封装有以下几个优势:1.1提高代码的可维护性通过将重复

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),