<?php
/*
PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature - they cannot define the implementation. PHP 5 支持抽象类和抽象方法。定义为抽象的类不能被实例化。任何一个类,如果它里面至少有一个方法是被声明为抽象的,那么这个类就必须被声明为抽象的。被定义为抽象的方法只是声明了其调用方式(参数),不能定义其具体的功能实现。 When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. Furthermore the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same. For example, if the child class defines an optional argument, where the abstract method's signature does not, there is no conflict in the signature. This also applies to constructors as of PHP 5.4. Before 5.4 constructor signatures could differ.
继承一个抽象类的时候,子类必须定义父类中的所有抽象方法;另外,这些方法的访问控制必须和父类中一样(或者更为宽松)。例如某个抽象方法被声明为受保护的,那么子类中实现的方法就应该声明为受保护的或者公有的,而不能定义为私有的。此外方法的调用方式必须匹配,即类型和所需参数数量必须一致。例如,子类定义了一个可选参数,而父类抽象方法的声明里没有,则两者的声明并无冲突。 这也适用于 PHP 5.4 起的构造函数。在 PHP 5.4 之前的构造函数声明可以不一样的。 */ abstract class AbstractClass
{
//Force Extending class to define this method
// 强制要求子类定义这些方法
abstract protected function getValue();
abstract protected function prefixValue($prefix); // Common method 普通方法(非抽象方法)
public function printOut(){
print $this->getValue().'<br>';
}
} class ConcreteClass1 extends AbstractClass
{
protected function getValue(){
return 'ConcreteClass1';
} public function prefixValue($prefix){
return "{$prefix}".'ConcreteClass1';
}
} class ConcreteClass2 extends AbstractClass
{
public function getValue(){
return 'ConcreteClass2';
} public function prefixValue($prefix){
return "{$prefix}".'ConcreteClass2';
}
} /*
class ConcreteClass3 extends AbstractClass
{
private function getValue(){
return 'ConcreteClass3';
}//Fatal error: Access level to ConcreteClass3::getValue() must be protected (as in class AbstractClass) or weaker in public function prefixValue($prefix){
return "{$prefix}".'ConcreteClass3';
}
}
*/ $class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_').'<br>'; $class2 = new ConcreteClass2;
$class2->printOut();
echo $class2->prefixValue('FOO_').'<br>'; abstract class AbstractClassB
{
// Our abstract method only needs to define the required arguments
// 我们的抽象方法仅需要定义需要的参数
abstract protected function prefixNameB($name);
} class ConcreteClassB extends AbstractClassB
{
// Our child class may define optional arguments not in the parent's signature
// 我们的子类可以定义父类签名中不存在的可选参数
public function prefixNameB($name, $separator = '.'){
if ($name == 'Pacman') {
$prefix = 'Mr';
} elseif ($name == 'Pacwoman') {
$prefix = 'Mrs';
} else {
$prefix = '';
}
return "{$prefix}{$separator} {$name}";
}
} $classB = new ConcreteClassB;
echo $classB->prefixNameB('Pacman'), '<br>';
echo $classB->prefixNameB('Pacwoman'), '<br>'; /*
Object Interfaces
Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.
Interfaces are defined in the same was as a class, but with the interface keyword replacing the class keyword and without any of the methods having their contents defined.
All methods declared in an interface must be public; this is the nature of an interface.
对象接口
使用接口(interface),可以指定某个类必须实现哪些方法,但不需要定义这些方法的具体内容。
接口是通过 interface 关键字来定义的,就像定义一个标准的类一样,但其中定义所有的方法都是空的。
接口中定义的所有方法都必须是公有,这是接口的特性。 implements
To implement an interface, the implements operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal error. Classes may implement more than one interface if desired by separating each interface with a comma.
Note:
Prior to PHP 5.3.9, a class could not implement two interfaces that specified a method with the same name, since it would cause ambiguity. More recent versions of PHP allow this as long as the duplicate methods have the same signature.
Note:
Interfaces can be extended like classes using the extends operator.
Note:
The class implementing the interface must use the exact same method signatures as are defined in the interface. Not doing so will result in a fatal error.
Constants
It's possible for interfaces to have constants. Interface constants works exactly like class constants except they cannot be overridden by a class/interface that inherits them.
实现(implements)
要实现一个接口,使用 implements 操作符。类中必须实现接口中定义的所有方法,否则会报一个致命错误。类可以实现多个接口,用逗号来分隔多个接口的名称。
Note:
实现多个接口时,接口中的方法不能有重名。
Note:
接口也可以继承,通过使用 extends 操作符。
Note:
类要实现接口,必须使用和接口中所定义的方法完全一致的方式。否则会导致致命错误。
常量
接口中也可以定义常量。接口常量和类常量的使用完全相同,但是不能被子类或子接口所覆盖。 */ // Declare the interface 'iTemplate'
interface iTemplate
{
public function setVariable($name, $var);
public function getHtml($template);
} // Implement the interface
// This will work class Template implements iTemplate
{
private $vars = array(); public function setVariable($name, $var)
{
$this->vars[$name] = $var;
} public function getHtml($template)
{
foreach ($this->vars as $name => $value) {
$template = str_replace('{'.$name.'}', $value, $template);
}
return $template;
}
} /*
class BadTemplate implements iTemplate
{
private $var = array();
public function setVariable($name, $var)
{
$this->vars[$name] = $var;
}
}
Fatal error: Class BadTemplate contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (iTemplate::getHtml) */ /*
class BadTemplate implements iTemplate
{
private $vars = array(); public function setVariable($name, $var,$echo)
{
//Fatal error: Declaration of BadTemplate::setVariable() must be compatible with iTemplate::setVariable($name, $var) $this->vars[$name] = $var;
echo $echo;
} public function getHtml($template)
{
foreach ($this->vars as $name => $value) {
$template = str_replace('{'.$name.'}', $value, $template);
}
return $template;
}
}
*/ /*
class BadTemplate implements iTemplate
{
private $vars = array(); // Fatal error: Access level to BadTemplate::setVariable() must be public (as in class iTemplate) protected function setVariable($name, $var)
{ $this->vars[$name] = $var;
} public function getHtml($template)
{
foreach ($this->vars as $name => $value) {
$template = str_replace('{'.$name.'}', $value, $template);
}
return $template;
}
} */ interface a
{
public function foo();
} interface b extends a
{
public function baz(Baz $baz);
} class c implements b
{
public function foo()
{ } public function baz(Baz $baz)
{ }
} /*
Fatal error: Declaration of d::baz() must be compatible with b::baz(Baz $baz) class d implements b
{
public function foo()
{ } public function baz(Foo $foo)
{ }
}
*/ //Multiple interface inheritance 继承多个接口 interface a1
{
public function foo();
} interface b1
{
public function bar();
} interface c1 extends a1, b1
{
public function baz();
} class d1 implements c1
{
public function foo()
{
} public function bar()
{
} public function baz()
{
}
} //Interfaces with constants 使用接口常量
interface a2
{
const b2 = 'Interface constant';
} echo a2::b2; /*
Fatal error: Cannot inherit previously-inherited or override constant b2 from interface a2
错误写法,因为常量不能被覆盖。接口常量的概念和类常量是一样的。 class c2 implements a2
{
const b2 ='Class constant';
} */

http://php.net/

小结:

0-子类需定义抽象类所有方法,方法参数个数可以添加,访问控制同或弱,而对象接口的实现也需要实现全部方法,但是参数个数不可更改,且访问控制必须public。

发问:

0-框架中的实例?

//2016/8/29-9:11

Abstract Class vs. Interface
The difference between an interface and an abstract class may seem subtle. Remember that an abstract class is meant to be extended by a more specific class, of which you’ll probably create an object instance. As you’ve already seen, an abstract class might define a generic object, such as a shape.
Conversely, an interface is not inherited by a class, so you should not think of an interface as a way of loosely defining an entire object. Instead, an interface establishes a contract for the functionality that a class must have, regardless of the class type. For
example, in Chapter 8, “Using Existing Classes,” you’ll learn about the Iterator interface defined within the Standard PHP Library (SPL). The Iterator interface dictates the methods that must exist in a class in order for PHP to be able to loop through an
instance of that class.
Another way of distinguishing between abstract classes and interfaces is that abstract classes still have an “is a” relationship with the derived class. Interfaces do not have “is a” relationships with derived classes, although you could say that the derived class has a “has the same behaviors as” relationship with an interface.
In the next example, let’s create an interface for standard CRUD
functionality. The acronym CRUD refers to the ability to Create, Read, Update, and Delete data—the four basic actions required for many different types of content used in sites and applications.
Any class you use in an application that requires CRUD functionality could then implement this interface, whether it’s a User,Page, or Rectangle.
 
//一个抽象类会被扩展成一个特定的类,抽象类和它的继承类之间有一种“是一个”的关系,而接口是不同类的合约,‘契约’。
// 建立动物的抽象类,它的继承类都是动物而不能是植物;建立‘死亡’的接口,它的继承类既可以是动物也可以是植物,但必须执行‘死亡’方法。
 
 
Programming by Contract
In simple terms, programming by contract is the practice of declaring an interface before writing a class. This can be particularly useful for guaranteeing the encapsulation of your classes.Using the programming by contract technique, you will be able to identify the capabilities you are trying to implement before building your application, much in the same way an architect creates plans for a building before it is constructed.
Development teams frequently program by contract because of the many workflow improvements this technique brings. By defining the interaction of classes before any implementation begins, the team members know exactly what their objects must do; it is then fairly trivial to implement the required methods. When the interface is fully implemented, testing of the class will be conducted using only the rules defined in the interface.In the car example you’ve seen in previous sections, the ISpeedInfo interface could be considered a contract, as it is the only point of API interaction of which either class, Car or Street, needs to be aware. The Street class will test for this contract before accepting the object for interaction. One developer could then be assigned to create a Car class and another to create a Street class, and the two would not need to collaborate on the implementation beyond the IStreetInfo interface.
契约式编程

Class Abstraction -- Object Interfaces的更多相关文章

  1. Object Pascal中文手册 经典教程

    Object Pascal 参考手册 (Ver 0.1)ezdelphi@hotmail.com OverviewOverview(概述)Using object pascal(使用 object p ...

  2. .net Framework Class Library(FCL)

    from:http://msdn.microsoft.com/en-us/library/ms229335.aspx 我们平时在VS.net里引用的那些类库就是从这里来的 The .NET Frame ...

  3. Windows Python Extension Packages

    备注: 1.先要安装wheel库:pip install wheel 2.下载wheel,切换至下载路径,然后安装:pip install wheel库名.whl Windows Python Ext ...

  4. python 不同版本下载资源

    Unofficial Windows Binaries for Python Extension Packages by Christoph Gohlke, Laboratory for Fluore ...

  5. Atitit cms wordpress get_post  返回的WP_Post 规范 标准化

    Atitit cms wordpress get_post  返回的WP_Post 规范 标准化 public $ID; public $post_author = 0; * The post's l ...

  6. Google 如何修复 TrustManager 实施方式不安全的应用

    引用谷歌市场的帮助说明:https://support.google.com/faqs/answer/6346016 本文面向的是发布的应用中 X509TrustManager 接口实施方式不安全的开 ...

  7. JVMInternals--reference

    This article explains the internal architecture of the Java Virtual Machine (JVM). The following dia ...

  8. Delphi GDI+ Library

    GDI+ LibraryThis library enables GDI+ functionality for Delphi 2009 and later. It differs from other ...

  9. Scala入门指南与建议

    最近在学习使用Scala语言做项目,感觉这门语言实在是太优美了!作为一个本科数学.研究生机器学习专业的混合人才(哈哈),这门语言真的是满足了普通计算机编程(告诉计算机怎么做)和函数式编程(告诉计算机做 ...

随机推荐

  1. TP-Link 无线路由器设置图文教程----怎么设置TP-Link无线路由器图解

    转自:http://www.jb51.net/softjc/39399.html 无线路由器的基础配置 在我们第一次配置无线宽带路由器时,参照说明书找到无线宽带路由器默认的IP地址是192.168.1 ...

  2. 电赛菜鸟营培训(零)——Keil环境搭建

    一.Keil开发软件安装 1.安装keil软件 2.使用注册机进行破解 将方框内的ID号复制到注册机,然后得到License,放到最底下就可以完成了. 二.Keil工程搭建 表示参考数据手册,在这里建 ...

  3. vi-11

    vi编辑器linux命令大全 作者:xiaoru  出处:本站整理  发布时间:2013-04-29 13:20:23 -     vi就是linux命令行下的最著名的编辑器之一,Vim常被称作“程序 ...

  4. git学习 本地常用操作01

    注意: Microsoft的Word格式是二进制格式,因此,版本控制系统是没法跟踪Word文件的改动 不要使用Windows自带的记事本编辑任何文本文件 开始git项目: 初始化本地项目: 初始化:g ...

  5. Swift语言中为外部参数设置默认值可变参数常量参数变量参数输入输出参数

    Swift语言中为外部参数设置默认值可变参数常量参数变量参数输入输出参数 7.4.4  为外部参数设置默认值 开发者也可以对外部参数设置默认值.这时,调用的时候,也可以省略参数传递本文选自Swift1 ...

  6. BZOJ3289 Mato的文件管理(莫队算法+树状数组)

    题目是区间逆序数查询. 莫队算法..左或右区间向左或右延伸时加或减这个区间小于或大于新数的数的个数,这个个数用树状数组来统计,我用线段树超时了.询问个数和数字个数都记为n,数字范围不确定所以离散化,这 ...

  7. ThinkPHP添加模板时,犯的三个错

    错误一:低级错误,将n打成看m,如图1 图1 这个找错,花了我将近2小时.扫了好几遍与之相关的代码,上网查了好些. 错误二:这个算是个低能的高级错误了.具体模板显示的效果如图2 图2 只要将相对地址及 ...

  8. mysql 常用知识

    1.uuid guid UUID是一个由4个连字号(-)将32个字节长的字符串分隔后生成的字符串,总共36个字节长.比如:550e8400-e29b-41d4-a716-446655440000 CH ...

  9. 当一回Android Studio 2.0的小白鼠

    上个星期就放出了Android studio出2.0的消息,看了一下what's new 简直抓到了那个蛋疼的编译速度痛点.在网上稍微搜索了一下后发现基本都是介绍视频.一番挣扎后(因为被这IDE坑过几 ...

  10. OpenCV学习笔记——视频的边缘检测

    使用Canny算子进行边缘检测,并分开输出到三个窗口中,再给每一个窗口添加文字 代码: #include"cv.h" #include"highgui.h" / ...