php.net

 <?php
class Base {
public function sayHello() {
echo 'Hello';
}
} trait SayWorld {
public function sayHello() {
parent :: sayHello();
echo 'World!';
}
} class MyHelloWorld extends Base {
use SayWorld;
} $o = new MyHelloWorld();
$o->sayHello(); trait HelloWorld {
public function sayHello() {
echo 'Hello World';
}
}
class TheWorldIsNotEnough {
use HelloWorld;
public function sayHello() {
echo 'Hello Universe!';
}
} $ob = new TheWorldIsNotEnough();
$ob->sayHello(); trait Hello {
public function sayHello() {
echo 'Hello';
}
} trait World {
public function sayWorld() {
echo 'World';
}
} class MyHelloWorld_c {
use Hello, World;
public function sayExclamationMark() {
echo '!';
}
} $oc = new MyHelloWorld_c();
$oc->sayHello();
$oc->sayWorld();
$oc->sayExclamationMark();

Precedence
An inherited member from a base class is overridden by a member inserted  by a Trait. The precedence order is that members from the current class override Trait methods, which in turn override inherited methods.

The precedence order is that methods from the current class override Trait methods, which in turn override methods from the base class.

A Trait is similar to a class, but only intended to group functionality in a  fine-grained and consistent way. It is not possible to instantiate a Trait on  its own. It is an addition to traditional inheritance and enables horizontal composition of behavior; that is, the application of class members without requiring inheritance.

a mechanism for code reuse in single inheritance languages的更多相关文章

  1. [AngularJS] Decorator pattern for code reuse

    Imaging you have a large application, inside this large application you have many small individual a ...

  2. [JS Compose] 1. Refactor imperative code to a single composed expression using Box

    After understanding how Box is, then we are going to see how to use Box to refacotr code, to un-nest ...

  3. Java Interview Reference Guide--reference

    Part 1 http://techmytalk.com/2014/01/24/java-interview-reference-guide-part-1/ Posted on January 24, ...

  4. 每个JavaScript开发人员应该知道的33个概念

    每个JavaScript开发人员应该知道的33个概念 介绍 创建此存储库的目的是帮助开发人员在JavaScript中掌握他们的概念.这不是一项要求,而是未来研究的指南.它基于Stephen Curti ...

  5. JavaScript- The Good Parts Chapter 5 Inheritance

    Divides one thing entire to many objects;Like perspectives, which rightly gazed uponShow nothing but ...

  6. Classical Inheritance in JavaScript

    JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance in ...

  7. Linux Communication Mechanism Summarize

    目录 . Linux通信机制分类简介 . 控制机制 0x1: 竞态条件 0x2: 临界区 . Inter-Process Communication (IPC) mechanisms: 进程间通信机制 ...

  8. Think Python - Chapter 18 - Inheritance

    In this chapter I present classes to represent playing cards, decks of cards, and poker hands.If you ...

  9. how to avoid inheritance abuse

    Liskov Principle: if S is a subtype of Type T, then any objects of type T may be repalced by objects ...

随机推荐

  1. HttpURLConnection与HttpClient比较和使用示例

    1. GET请求与POST请求 HTTP协议是现在Internet上使用得最多.最重要的协议了,越来越多的Java应用程序需要直接通过HTTP协议来访问网络资源. 在介绍HttpURLConnecti ...

  2. python工具 - 从文件名读取特定信息到excel表格

    情景:文件名中包含学号和用户名,其中用户名在前学好在后,学号为2位,如harry33.txt.natasha12.txt. 要求:将多个文件名中的用户名与学号分开并保存到excle中. 代码部分: i ...

  3. Selenium Web 自动化 - Selenium常用API

    Selenium Web 自动化 - Selenium常用API 2016-08-01 目录 1 对浏览器操作  1.1 用webdriver打开一个浏览器  1.2 最大化浏览器&关闭浏览器 ...

  4. Hack mind

    试 卷 姓名________ 学号________ 首先我们来看这个,试卷的名字和编号填写,这个部分有“漏洞”吗?有 学生的姓名和编号都写在这儿,没有做任何保护措施,因此,你只要偷看了某人的试卷上的这 ...

  5. android开发(49) Android 下拉刷新的实现。使用 SwipeRefreshLayout 代替 pull-to-refesh

    概述 谷歌官方推出了SwipeRefreshLayout 来实现下拉刷新的效果.对比以前我们常用的 pull-to-refesh ,这个方案显得更加的简单方便. 关联项目引用(管理依赖) 在你的 应用 ...

  6. IP分片与重组详解

    大家对IP数据包头,应该不陌生吧 分片便是与图中圈出来的两个地址有关,本文也是将主要围绕他们展开. 那我们先来了解他们的概念. 标志一个三比特字段遵循与用于控制或识别片段.他们是(按顺序,从高分以低位 ...

  7. CentOS 7 设置静态IP

    cd /etc/sysconfig/network-scripts/ sudo vi ifcfg-eno16777736 BOOTPROTO=static #dhcp改为static(修改) IPAD ...

  8. Git -- 搭建git服务器

    在远程仓库一节中,我们讲了远程仓库实际上和本地仓库没啥不同,纯粹为了7x24小时开机并交换大家的修改. GitHub就是一个免费托管开源代码的远程仓库.但是对于某些视源代码如生命的商业公司来说,既不想 ...

  9. IOC容器特性注入第三篇:Attribute封装

    Attribute(特性)=>就是对类,方法,字段的自定义属性的基类.可以利用Attribute对类,方法等进行自定义描述,方便区分. 既然如此,那我们就可以那些需要注入IOC容器和不需要注入I ...

  10. .net core 开发接口前端调用时提示错误 405

    解决方法: 在StartUp.cs文件中Configure(IApplicationBuilder app, IHostingEnvironment env)方法中添加如下代码: //跨域 app.U ...