Beispiel #1
0
<?php

/*
1、继承那些东西 public protected子类属性方法 可操作
2、子类可以做什么扩充
3、构造函数如何继承
4、私有的属性和方法如何继承
*/
header('Content-Type:text/html;charset=utf-8');
class Human
{
    private $wife = '小甜甜';
    //如果去掉 Cannot access private property Stu::$wife
    public function hshow()
    {
        echo $this->wife . "<br>";
    }
}
class Stu extends Human
{
    private $wife = '凤姐';
    //如果去掉产生 Undefined property: Stu::$wife 错误
    public function sshow()
    {
        //parent::hshow();//使用parent会更快一点
        $this->hshow();
        echo $this->wife . "<br/>";
    }
}
$stu = new Stu();
$stu->sshow();
Beispiel #2
0
 *
 * self:本类
 * parent:父类
 *
 * 在引入自身的静态方法/静态属性
 * 以及父类的方法,属性时用到
 * 
 */
class Human
{
    public static $head = 1;
    public $age = 28;
    public function say()
    {
        echo $this->age;
    }
    public function show()
    {
        echo self::$head, 'hello<br />';
    }
}
class Stu extends Human
{
    public function show()
    {
        parent::show();
        echo 'world';
    }
}
$m = new Stu();
$m->show();
Beispiel #3
0
}


class Stu extends Human{

}

class FreshMan extends Stu{
	public function Say(){
		echo '我要出国,做美得坚人!<br/>';
	}
}

echo 'final修饰的方法可以继承<br />';

$ming = new Stu();
$ming->Say();
$ming->show();


echo '但不可以修改<br />';

$fr = new FreshMan();
$fr->Say();
$fr->show();
/*
Fatal error: Cannot override final method Human::Say()
*/
?>

Beispiel #4
0
		echo $this->wife,'<br />';	
	}

	public function cry(){
		echo '5555<br />';
	}
}

class Stu extends Human{
	public function subtell(){
		echo $this->wife;
	}

}

$lisi = new Stu();

$lisi -> cry();
$lisi -> tell();
$lisi -> subtell(); //不能调用,因为子类不没有wife

/*
未定义的属性,wifi属性
私有属性,可以理解不能继承
(其实是能继承过来,只不过无法访问)
public protected private中,
public protected 都可以继承,并拥有访问和修改的权限
这就好比说,家产已经了,愿意卖就卖
*/

class Human{
	public function say($name){
		echo $name, '吃了吗?<br />';
	}
}

class Stu extends Human{
	public function say(){
		echo 'Yo Yo 切克闹!<br />'; //上面这个过程叫重写override
	}
}



$ming = new Stu();
$ming->say();
$ming->say('张三');


/*
可以灵活的模拟像Java中的函数重载的效果
*/

class Calc{
	public function area(){
		//判断一个调用area时,得到的参数个数
		$args = func_get_args();
		if(count($args) == 1){
			return 3.14 * $args[0];
		}else if(count($args) == 2){
<?php

/**
 * private_protected_public
 *
 * 
 */
class Human
{
    private $name = 'zhangsan';
    protected $money = 3000;
    public $age = 28;
    public function say()
    {
        echo $this->name, '<br />';
        echo $this->money, '<br />';
        echo $this->age, '<br />';
    }
}
class Stu extends Human
{
    private $friend = 'xiaozhu';
    public function talk()
    {
        echo $this->name, '<br />';
        echo $this->money, '<br />';
        echo $this->age, '<br />';
    }
}
$ming = new Stu();
$ming->talk();
Beispiel #7
0
    private $wife = '小甜甜';
    protected $age = 39;
    public function tell()
    {
        echo $this->wife, '<br />';
    }
    public function cry()
    {
        echo '555555<br />';
    }
    public function hShow()
    {
        echo $this->wife, '<br />';
    }
}
class Stu extends Human
{
    private $wife = '小猪猪';
    public function sShow()
    {
        parent::hShow();
        echo $this->wife, '<br />';
    }
    public function getAge()
    {
        return $this->age = 20;
    }
}
$lisi = new Stu();
echo '<pre>';
echo $lisi->getAge();
Beispiel #8
0
}

class Stu extends Human{
	private $wife = '美女老师';
	public $height = 170;
	public function subtell(){
		echo $this->wife,'<br />';
	}

	//子类自己的工作
	public function SWork(){
		parent::Work();   //继承父的工作
		echo '<br /> 打铁'; //自己又会打铁
	}

}

$lisi = new Stu();

$lisi -> subtell();
$lisi -> SWork();

echo '<pre>';
print_r($lisi);
echo '</pre>';



?>

Beispiel #9
0
后期绑定/延迟绑定
*/
header("Content-Type:text/html; charset=utf-8");

class Human{
	public static function whoami(){
		echo '来自父类的whoami在执行<br />';
	}

	public static function say(){
		self::whoami(); //子类内没有say方法,找到父类这里 self是指父类
	}

	public static function say2(){
		static::whoami(); //子类也没有say2方法,又找到父类这里
							//但是父类用static::whoami
							//指调用你子类自己的whoami方法
	}
}

class Stu{
	public static function whoami(){
		echo '来自子类的whoami在执行<br />';
	}
}


Stu::say();
Stu::say2();
?>
Beispiel #10
0
 *
 * 
 * 重载/ overloaded
 */
header('Content-type:text/html;charset=utf-8');
/**
 * 重写例子
 */
class Human
{
    public function say()
    {
        echo '吃了吗';
    }
}
class Stu extends Human
{
    public function say()
    {
        echo 'hello';
    }
    // public function say($a,$b,$c) {
    //     echo '哥三好';
    // }
    public function oldSay()
    {
        parent::say();
    }
}
$s = new Stu();
$s->say();