public function settreeage($age){ $this->treeage = $age; return trie; }}
/**
* 定义具体产品类 葡萄 * 首先,我们要实现所继承的接口所定义的方法 * 然后定义葡萄所特有的属性,以及方法 */class grape implements fruit{ //葡萄是否有籽
private $seedless; public function grow(){
echo apple grow; } public function plant(){
echo apple plant; } public function harvest(){
echo apple harvest; } public function eat(){
echo apple eat; } //有无籽取值
public function getseedless(){ return $this->seedless; } //设置有籽无籽
public function setseedless($seed){ $this->seedless = $seed; return true; }}/**
*农场主类 用来获取实例化的水果 * */class farmer{ //定义个静态工厂方法
public static function factory($fruitname){ switch ($fruitname) { case 'apple': return new apple(); break; case 'grape': return new grape(); break; default: throw new badfruitexception(error no the fruit, 1); break; } }}class badfruitexception extends exception{
public $msg; public $errtype; public function __construct($msg = '' , $errtype = 1){ $this->msg = $msg; $this->errtype = $errtype; } }/**
* 获取水果实例化的方法 */try{ $appleinstance = farmer::factory('apple'); var_dump($appleinstance);}catch(badfruitexception $err){ echo $err->msg . _______ . $err->errtype;}
复制代码
