Blogger Information
Blog 35
fans 2
comment 0
visits 22730
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
trait实例--2018年5月8日
小学僧的博客
Original
601 people have browsed it

 1.trait 是为单继承语言量身定制的代码复用机制

 2.trait 简单理解为一个方法集合

 3.trait可以看做是一个特殊的类,但不嫩被实例化,仅允许被类调用

实例

<?php 
header("content-type:text/html;charset=utf-8");
class Person
{
	protected $name;
	public function __construct($name='test')
	{
		$this->name=$name;
	}
	public function study($course='progamming')
	{
		return $this->name.' is studing'.$course;
	}
}

trait Course
{
	public $friend='john';
	public function sport($name='mysql')
	{
		 $this->name.$this->friend.' is studing'.$name;

	}
	abstract public static function hobby($name);

	public function study($course ='html')
	{
			 return $this->name.'is studing'.$course;
	}
}

class Student extends Person
{
	use Course;
	protected $name='test1';
	public static function hobby($name)
	{
		return $name;
	}
	public function study($course='java')
	{
		return $this->name.' is studing '.$course;
	}
}

$student = new Student();
echo $student->study();

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post