Blogger Information
Blog 29
fans 1
comment 1
visits 24856
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
trait--2018年5月9日16:06
谦谦允水的博客
Original
730 people have browsed it
实例
<?php 
//普通类
class Person
{
	protected $name='彭利';//自己本身和子类可用
	public $height=175;    //通用不安全
	const age=20;
	public function __construct($name='')
	{
		if(empty($name)){
			$this->name=$this->name;
		}else{
			$this->name=$name;
		}
	}
	public function __get($g)
	{
		return $this->$g;
	}
	//自定义方法
	public function color($color){
		return '这是'.$color;
	}
	
}
$per=new Person('大哥');
echo $per::age."<hr>";
echo $per->name."<hr>";
echo $per->color('黄色')."<hr>";
echo $per->height."<hr>";
//方法集合
trait active
{
	public $name1='小明';//trait中的属性不能与类中属性重名,方法可以覆盖
	public function sport($do){
		return $this->name1.$do;
	}
	public function color($c){
		return '我是trait类'.$c.'<hr>';
	}
	//抽象方法
	abstract public function wg(); //抽象方法必须在类中实例化
}
//继承类
class jc extends Person
{
	use active;
	protected $name;
	// public function color($msg){
	// 	return "t我是子类".$msg.$this->name.'<hr>';
	// }
	public function wg($w){
		return $w;
	}
}
$jc=new jc('绿色');
echo $jc->color('的');
echo $jc->wg('世界');
//方法优先级   子类>trait>父类
?>
运行实例 »
点击 "运行实例" 按钮查看在线实例


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