20191119PHP.class类练习

<?php
class person{
public $name;
private $age;
public $sex;
const WOMAN=0;
const MAN=1;
function __construct($name,$age){
$this->name=$name;
$this->age=$age;
}
private function run(){
echo $this->name." ran..";
}

public function eat(){
echo $this->name." eat";
}
protected function look(){
echo $this->name." look";
}

public function wc(){
echo $this->name." go to wc";
}
}

//$tom=new person("tom",30);
//$tom->eat();//公开的外部可以用
//$tom->look();//保护的外部不可用
//$tom->run();//私有的外部不可用


//继承

class man extends person{
// final $sex="男";//final不能修饰变量;
function __construct($name,$age){
parent::__construct($name,$age);
$this->sex=$this::WOMAN;

}
public function wc(){
echo $this->name." go to manWC";
}
function __destruct(){
echo "over";
}

}
class waman extends person{
// final $sex="女";
public function wc(){
echo $this->name." go to wamanWC";
}
}


$tom=new man("tom",19);
$tom->wc();
?>

猜你喜欢

转载自www.cnblogs.com/syqlwyx/p/11890433.html
今日推荐