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 ...
随机推荐
- [k8s]docker calico网络&docker cluster-store
docker cluster-store选项 etcd-calico(bgp)实现docker夸主机通信 配置calico网络 - 启动etcd etcd --listen-client-urls h ...
- 【Big Data - ELK】ELK(ElasticSearch, Logstash, Kibana)搭建实时日志分析平台
摘要: 前段时间研究的Log4j+Kafka中,有人建议把Kafka收集到的日志存放于ES(ElasticSearch,一款基于Apache Lucene的开源分布式搜索引擎)中便于查找和分析,在研究 ...
- Selenium Web 自动化 - 如何找到元素
Selenium Web 自动化 - 如何找到元素 2016-07-29 1. 什么是元素? 元素:http://www.w3school.com.cn/html/html_elements.asp ...
- android开发(47) 使用xml drawable 实现 局部圆角,可用作圆角边框
代码如下: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android=" ...
- CentOS7 配置免密码登陆
3台主机 192.168.30.207 Master 192.168.30.251 Node1 192.168.30.252 Node2 三台主机检查 ~/.ssh 文件夹,没有则新建 在master ...
- springcloud-04-自定义ribbon的配置方式
在dubbo项目中, zookeeper即注册中心帮我们实现了调度和负载均衡的能力, 这种方式被称为服务器端的负载均衡, springcloud中, 使用ribben实现的客户端负载均衡 什么是rib ...
- Hibernate HQL的使用
1.简单查询(查询所有) Session session=HibernateUtil.getSessionFactory().getCurrentSession(); Transaction tx=s ...
- 和我一起学Effective Java之泛型
泛型 不要在新代码中使用原始类型 泛型(generic):声明中具有一个或多个类型参数 原始类型(raw type):不带任何实际类型参数的泛型名称 格式: 类或接口的名称 < 对应于泛型形式类 ...
- 仿迅雷播放器教程 -- 基于VLC的C++播放器 (4)
经过前面的介绍,想必大家对VLC和ffmpeg都有一定印象了,还记得学习ffmpeg多么蛋疼吗?那么VLC会不会也这么蛋疼呢? 那么我们来看一段官方的Demo,Alberl精简了Demo,只留 ...
- 【代码审计】XYHCMS V3.5文件上传漏洞分析
0x00 环境准备 XYHCMS官网:http://www.xyhcms.com/ 网站源码版本:XYHCMS V3.5(2017-12-04 更新) 程序源码下载:http://www.xyhc ...