a mechanism for code reuse in single inheritance languages
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的更多相关文章
- [AngularJS] Decorator pattern for code reuse
Imaging you have a large application, inside this large application you have many small individual a ...
- [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 ...
- Java Interview Reference Guide--reference
Part 1 http://techmytalk.com/2014/01/24/java-interview-reference-guide-part-1/ Posted on January 24, ...
- 每个JavaScript开发人员应该知道的33个概念
每个JavaScript开发人员应该知道的33个概念 介绍 创建此存储库的目的是帮助开发人员在JavaScript中掌握他们的概念.这不是一项要求,而是未来研究的指南.它基于Stephen Curti ...
- JavaScript- The Good Parts Chapter 5 Inheritance
Divides one thing entire to many objects;Like perspectives, which rightly gazed uponShow nothing but ...
- Classical Inheritance in JavaScript
JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance in ...
- Linux Communication Mechanism Summarize
目录 . Linux通信机制分类简介 . 控制机制 0x1: 竞态条件 0x2: 临界区 . Inter-Process Communication (IPC) mechanisms: 进程间通信机制 ...
- Think Python - Chapter 18 - Inheritance
In this chapter I present classes to represent playing cards, decks of cards, and poker hands.If you ...
- 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 ...
随机推荐
- 安装配置OSA运维管理平台
1.下载完整包V1.0.2wget http://www.osapub.com/download/OSA_BETA_V1.0.2.tar.gzV1.0.5wget http://www.osapub. ...
- AtomicInteger 源码阅读
Package java.util.concurrent.atomic 这是一个小工具包,它的实际作用是提供了很多个无阻塞的线程安全的变量操作工具. 无阻塞的线程安全:其含义就是不使用 synchro ...
- Django 源码小剖: Django 中的 WSGI
Django 其内部已经自带了一个方便本地测试的小服务器, 所以在刚开始学习 Django 的时候并不需搭建 apache 或者 nginx 服务器. Django 自带的服务器基于 python w ...
- 解决:github上传时出现error: src refspec master does not match any
原因分析 引起该错误的原因是,目录中没有文件,空目录是不能提交上去的 解决方法 touch README git add README git commit -m 'first commit' git ...
- supervisor开机自动启动脚本+redis+MySQL+tomcat+nginx进程自动重启配置
[root@mongodb-host supervisord]# cat mongo.conf [program:mongo]command=/usr/local/mongodb/bin/mongod ...
- Vue.js常用指令:v-for
一.什么是v-for指令 在Vue.js中,我们可以使用v-for指令基于源数据重复渲染元素.也就是说可以使用v-for指令实现遍历功能,包括遍历数组.对象.数组对象等. 二.遍历数组 代码示例如下: ...
- 嵌入式开发之hi3519---进程线程间的同步和互斥,条件变量、信号了、互斥锁等
sem_post 最安全 sem 有序,会卡顿 阻塞 mutex 无序,不能同步 http://blog.chinaunix.net/uid-20671208-id-4935154.html ht ...
- Python内置类型——set
Python中,内置类型set和frozenset用来表示集合,我们首先查看这两个类型支持的特殊对象,从而可以理解他们的特性. >>> dir(set) ['__and__', '_ ...
- VMware 虚拟机磁盘
创建磁盘时,会进行两个操作:分配空间.置零 1.厚置备延迟置零: 默认的创建格式,创建磁盘时,直接从磁盘分配空间,但对磁盘保留数据不 置零.所以当有I/O操作时,只需要做置零的操作. 磁盘性能较好,时 ...
- springboot+shiro+redis(单机redis版)整合教程-续(添加动态角色权限控制)
相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3. springboot+shiro+redis(集群re ...