构造函数 2018-11-24 13:05

代码:

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

class Animal{

	public $name;
	public $age;
	public $color;

	public function __construct($name,$age,$color){
		$this->name=$name;
		$this->age=$age;
		$this->color=$color;
		echo 'Animal<br />';
	}

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

	public function set_age($age){
		$this->age=$age;
	}

	public function set_color($color){
		$this->color=$color;
	}

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

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

	// 接口
	public function get_color(){
		return $this->color;
	}

	public function show(){
		echo "温馨提示:名字:{$this->name}<br />";
		echo "年龄:{$this->age}岁<br />";
		echo "颜色:{$this->color}<br />";
	}

}

// 猫的类型
class Cat extends Animal{

}

// 鸟的类型
class Bird extends Animal{

}

// ------------------------------------------
$c=new Cat('一只猫',2,'灰色');
$c->show();

$b=new Bird('猫头鹰',3,'白色');
$b->show();



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

class Animal{

	public $name;
	public $age;
	public $color;

	public function __construct($name,$age,$color){
		$this->name=$name;
		$this->age=$age;
		$this->color=$color;
		echo 'Animal<br />';
	}

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

	public function set_age($age){
		$this->age=$age;
	}

	public function set_color($color){
		$this->color=$color;
	}

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

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

	// 接口
	public function get_color(){
		return $this->color;
	}

	public function show(){
		echo "温馨提示:名字:{$this->name}<br />";
		echo "年龄:{$this->age}岁<br />";
		echo "颜色:{$this->color}<br />";
	}

}

// 猫的类型
class Cat extends Animal{
	public function __construct($name,$age,$color){
		echo 'Cat<br />';
	}
}

// 鸟的类型
class Bird extends Animal{
	public function __construct($name,$age,$color){
		echo 'Bird<br />';
	}
}

// ------------------------------------------
$c=new Cat('一只猫',2,'灰色');
$c->show();

$b=new Bird('猫头鹰',3,'白色');
$b->show();



原因:如果自己又写了同名方法(比如:__construct()),那么就用自己的,就不多用父类继承的方法;

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

class Animal{

	public $name;
	public $age;
	public $color;

	public function __construct($name,$age,$color){
		$this->name=$name;
		$this->age=$age;
		$this->color=$color;
		echo 'Animal<br />';
	}

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

	public function set_age($age){
		$this->age=$age;
	}

	public function set_color($color){
		$this->color=$color;
	}

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

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

	// 接口
	public function get_color(){
		return $this->color;
	}

	public function show(){
		echo "名字:{$this->name}<br />";
		echo "年龄:{$this->age}岁<br />";
		echo "颜色:{$this->color}<br />";
	}

}

// 猫的类型
class Cat extends Animal{
	public function __construct($name,$age,$color){
		parent::__construct($name,$age,$color);
		echo 'Cat<br />';
	}
}

// 鸟的类型
class Bird extends Animal{
	public function __construct($name,$age,$color){
		echo 'Bird<br />';
	}
}

// ------------------------------------------
$c=new Cat('一只猫',2,'灰色');
$c->show();

$b=new Bird('猫头鹰',3,'白色');
$b->show();



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

class Animal{

	public $name;
	public $age;
	public $color;

	public function __construct($name,$age,$color){
		$this->name=$name;
		$this->age=$age;
		$this->color=$color;
		echo 'Animal<br />';
	}

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

	public function set_age($age){
		$this->age=$age;
	}

	public function set_color($color){
		$this->color=$color;
	}

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

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

	// 接口
	public function get_color(){
		return $this->color;
	}

	public function show(){
		echo "名字:{$this->name}<br />";
		echo "年龄:{$this->age}岁<br />";
		echo "颜色:{$this->color}<br />";
	}

}

// 猫的类型
class Cat extends Animal{
	public function __construct($name,$age,$color){
		parent::__construct($name,$age,$color);
		echo 'Cat<br />';
	}
}

// 鸟的类型
class Bird extends Animal{
	public function __construct($name,$age,$color){
		parent::__construct($name,$age,$color);
		echo 'Bird<br />';
	}
}

// ------------------------------------------
$c=new Cat('一只猫',2,'灰色');
$c->show();

$b=new Bird('猫头鹰',3,'白色');
$b->show();



结论:手工多用同名的继承过来的方法,

调用的方式:parent::