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. 3D建模软件的选择(UG,Solidworks,ProE)

    转自:3D建模软件的选择(UG,Solidworks,ProE) 自述 咱是一个码农,和web.软件.控制台打交道太多了,很想玩玩炫的东西,于是学了点点PS,结果发现完全没有美术细胞TT.最近有碰到对 ...

  2. linux source code search

    https://elixir.bootlin.com/linux/latest/source/fs/eventpoll.c#L1120

  3. Ubuntu python3 安装pip

    python2:sudo apt-get install python-pip python3:sudo apt-get install python3-pip 切换python2和python3的版 ...

  4. remmina如何上传文件到linux服务器

    1.安装filezilla 2. remmina中选择列表中的主机,点击“外部工具 -> filezilla-xfp.sh” 3. 弹出filezilla,输入主机,端口,用户名,密码,连接, ...

  5. 【Unity】不能新建项目

    问题:Unity5.5.2f1今天遇到个Bug,在启动器点击新建项目没有反应. 办法:先点击新建项目(没有反应),再点击Sign Out退出登录,然后再登录进来,就能跳到新建项目页面.

  6. Java连接各种数据库写法

    # 示例配置参考,涵盖几乎所有的主流数据库 ############# Oracle数据库 ######################## #   数据库驱动名 driver=oracle.jdbc ...

  7. 牛客网_Go语言相关练习_判断&选择题(4)

    题目来源于牛客网 一.判断题 成员变量或者函数的首字母表示是否对外部可见. switch后面的声明语句和表达式语句都是可以选择的.例如: //可以什么都不加 switch: break; 错误指的是可 ...

  8. Docker GitLab镜像部署

    环境说明 系统环境: CentOS Linux release 7.4 docker Version: 18.03.1-ce 运行镜像 docker run --detach \ --hostname ...

  9. 详解 Redis 应用场景及应用实例

    redis(二)高级用法 详解 Redis 应用场景及应用实例 Redis 它是什么?它用来做什么?它的优势与短板如何? 告诉你Redis是一个牛逼货

  10. Java中的引用类型Scanner类和随机类型Random

    Scanner类 我们要学的Scanner类是属于引用数据类型,我们先了解下引用数据类型.   引用数据类型的使用 与定义基本数据类型变量不同,引用数据类型的变量定义及赋值有一个相对固定的步骤或格式. ...