PHP:类的三大特性、继承、parent关键字、

类的三大特性:

1.封装
2.继承
3.多态

1、类的继承特性:

#子承父业
class It extends Person{

}

驼峰式写法

<?php 
// 人类(父类)
class userPerson{
	public $name;

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

	public function say(){
		echo "my name is {$this->name}";
	}	
}

// linuxIsVeryGood 驼峰式写法

// LinuxIsVeryGood 严格的驼峰式写法
?>



多参数类–重用性差

<?php 
// 人类(父类)
class Person{
    public $name;

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

    public function say(){
        echo "<p>my name is {$this->name}</p>";
    }   
}

// it人员
class It{
    public $name;
    public $soft;

    public function __construct($n,$s){
        $this->name=$n;
        $this->soft=$s;
    }

    public function say(){
        echo "<p>my name is {$this->name}</p>";
    }

    public function soft(){
        echo "<p>{$this->name}正在开发{$this->soft}软件</p>";
    }
}

$obj=new It('小马','PHP');

$obj->say();
$obj->soft();

 ?>


类继承特性

<?php 
// 人类(父类)
class Person{
    public $name;

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

    public function say(){
        echo "<p>my name is {$this->name}</p>";
    }   

    public function eat(){
        echo "<p>{$this->name}正在吃饭</p>";
    }

    public function sleep(){
        echo "<p>{$this->name}正在睡觉</p>";
    }
}

// it人员
class It extends Person{
    public $soft;

    public function __construct($n,$s){
        $this->name=$n;
        $this->soft=$s;
    }

    public function soft(){
        echo "<p>{$this->name}正在开发{$this->soft}软件</p>";
    }
}

$obj=new It('小马','PHP');

$obj->say();
$obj->soft();
$obj->eat();
$obj->sleep();

 ?>

**

my name is 小马

小马正在开发PHP软件

小马正在吃饭

小马正在睡觉

2、parent关键字:

parent::__construct($n,$a);

继承作用:

1.提高重用性
2.提高扩展性
3.提高灵活性



parent关键字

<?php 
// 人类(父类)
class Person{
    public $name;

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

    public function say(){
        echo "<p>my name is {$this->name},the age is {$this->age}</p>";
    }   

    public function eat(){
        echo "<p>{$this->name}正在吃饭</p>";
    }

    public function sleep(){
        echo "<p>{$this->name}正在睡觉</p>";
    }
}

// it人员
class It extends Person{
    public $soft;

    public function __construct($n,$a,$s){
        // parent关键字
        parent::__construct($n,$a);
        $this->soft=$s;
    }

    public function soft(){
        echo "<p>{$this->name}正在开发{$this->soft}软件</p>";
    }
}

$obj=new It('小马',20,'PHP');

$obj->say();
$obj->soft();
$obj->eat();
$obj->sleep();

 ?>

**

my name is 小马,the age is 20

小马正在开发PHP软件

小马正在吃饭

小马正在睡觉
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值