类的继承和类中属性和方法的重载技术

Original 2018-12-20 22:04:30 219
abstract:<meta charset="utf-8"><?phpclass Person{    public $name;    private $age;    private $sex;    public function __construct($name,$age,$sex) &

<meta charset="utf-8">
<?php

class Person{
   public $name;
   private $age;
   private $sex;

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

public function getAge(){
       return "我的年龄".$this->age;
  }

 public function mary(){
       return "名字是".$this->name."年龄".$this->age."岁"."性别".$this->sex;
 }

 function __get($name){
     switch ($name) {
         case 'age':
             return "我的年龄".$this ->$name;
         case 'sex':
             return "我的性别".$this ->$name;

     }
 }

   function __set($key,$value){

       echo $key."赋值为".$value;

   }

   function __isset($name){
       return isset($this->$name);
   }

}

class Student extends Person{
 public $grade;

 public function __construct($name, $age, $sex,$grade)
 {
     parent::__construct($name, $age, $sex);
     $this->grade=$grade;
 }

   public function mary()
{
   return parent::mary()."成绩".$this->grade;
}

}

$person=new Person("姚明","29","男");

echo $person->sex;
echo "<hr>";
echo $person->sex="uoip";

echo "<hr>";
echo $person->mary();
echo "<hr>";
$student=new Student('小明','30','男','50');
echo $student->mary();

?>


Correcting teacher:天蓬老师Correction time:2018-12-20 23:11:05
Teacher's summary:下次请把代码放到代码块中提交, 如果不会, 群中问一下,别这样提交纯文本了

Release Notes

Popular Entries