权限 2018-11-24 12:32

意义:

<?php
header('Content-type:text/html;charset=utf-8');

class Ghost{

	private $name;
	private $age;
	private $addr;
	private $height;

	public function __construct($name,$age,$addr,$height){
		$this->name=$name;
		$this->age=$age;
		$this->addr=$addr;
		$this->height=$height;
	}

	public function set_name($name){
		$this->name=$name;
	}

	// 接口
	public function get_name(){
		return $this->name;
	}

	// 接口
	public function get_age(){
		if ($this->age>30) {
			return 22;
		}
	}

	// 接口
	public function get_height(){
		if ($this->height<150) {
			return 163;
		}
	}

	// 接口
	public function get_addr(){
		return '关你屁事';
	}

	public function show(){
		echo "姓名:{$this->name}<br />";
		echo "年龄:{$this->age}岁<br />";
		echo "身高:{$this->height}cm<br />";
		echo "地址:{$this->addr}<br />";
	}

}

$g=new Ghost('聂小倩',68,'兰若寺',142);
// $g=new Ghost();
// $g->show();

$addr=$g->get_addr();
echo "大家看 ,那姑娘地址是:{$addr}<br />";
// echo $g->name;



为什么要两种?

protected $name;

private $age;

public $age;