Define the person class and its subclasses, and design, define, and instantiate classes (instances) as required

Keywords: PHP Attribute

problem

Design a person class with the following conditions:

1) Define the protected attribute: name (name), age (age), sex (gender)
2) Define static attribute: num (used to calculate the number of instantiated persons)
3) Define a constructor that outputs "I am a person" when an object is created and adds 1 to num;
4) Define a destructor that outputs "bye" when an object is destroyed;
5) Define the common method setInfo($info) to set attribute information such as name, age, sex, etc. through parameters
(Tip: Parameters can be arrays that contain information about each parameter, such as $info[name])
6) Define the common method getInfo(), which is used to output the attribute information of the object
7) Define the common method getNum(), which calculates and returns the number of people instantiated

B. Design a student class under the following conditions:

1) Inherit person class
2) Define the constructor, call the parent constructor, and output "I am a student"
3) Define private attributes: number, class, major
4) Override the parent's setInfo ($info) method, set it to final, call the parent's setInfo function to set common property information, and continue setting property information for this class
5) Override the getInfo() method of the parent class, set it to the final method, call the setInfo function of the parent class to output the common attribute information, and continue to output the attribute information of this class

C. Design a teacher class with the following conditions:

1) inherits from the person class;
2) Define the constructor, call the parent constructor, and output "I am a teacher"
3) Define private attributes: id (number), t_class (teaching class), Department (department)
4) Override the parent's setInfo ($info) method, set it to final, call the parent's setInfo function to set common property information, and continue setting property information for this class
5) Override the getInfo() method of the parent class, set it to the final method, call the setInfo function of the parent class to output the common attribute information, and continue to output the attribute information of this class
D. Instantiate the student class, call the setInfo() function to set the student information (referring to your own real information), and then call the getInfo() function to output the information
E, instantiate the teacher class, call the setInfo() function to set the student information (referring to your own real information), and then call the getInfo() function to output the information
F, call the static method getNum() of the person class, and output the number of people instantiated

The code is as follows:

<?php
class Person
{
    protected $name, $age, $sex;   //Name, Age, Gender
    public static $num;
    public function __construct($name = "", $age = "", $sex = "", $num = "")
    {
        echo "I am a person!<br>";
        $this->name = $name;
        $this->age = $age;
        $this->sex = $sex;
        self::$num++;
    }
    public function __destruct()
    {
        echo "bye";
    }
    public function setInfo($name, $age, $sex, $num)
    {
        $this->name = $name;
        $this->age = $age;
        $this->sex = $sex;
        $this->num = $num;
    }
    public function getInfo()
    {
        echo "Full name:" . $this->name . "<br>Age:" . $this->age . "<br>Gender:" . $this->sex . "<br>";
        return self::$num;
    }
    public function getNum()
    {
        echo "<br>The number of people instantiated is:" . self::$num . "<br>";
    }
}
Person::$num = 0;

class student extends Person
{
    private $number, $clas, $major;  //Number Class Specialty
    public function __construct($number = "", $clas = "", $major = "")
    {
        $this->number = $number;
        $this->clas = $clas;
        $this->major = $major;
        echo "I am a student<br>";
    }
    public final function setInfo($name, $age, $sex, $number, $clas, $major)
    {
        $this->name = $name;
        $this->age = $age;
        $this->sex = $sex;
        $this->number = $number;
        $this->clas = $clas;
        $this->major = $major;
    }
    public final function getInfo()
    {
        parent::getInfo();
        echo  "School Number:" . $this->number . "<br>Class:" . $this->clas . "<br>Professional:" . $this->major . "<br>";
    }
    public function showNum()
    {
        echo parent::getNum();
    }
}
class teacher extends Person
{
    private $id, $t_class, $department;  //Work Number Classes and Departments
    public function __construct($id = "", $t_class = "", $department = "")
    {
        $this->id = $id;
        $this->t_class = $t_class;
        $this->department = $department;
        echo "I am a teacher<br>";
    }
    public final function setInfo($name, $age, $sex, $id, $t_class, $department)
    {
        // parent::setInfo();
        $this->name = $name;
        $this->age = $age;
        $this->sex = $sex;
        $this->id = $id;
        $this->t_class = $t_class;
        $this->department = $department;
    }
    public final function getInfo()
    {
        parent::getInfo();
        echo  "Number:" . $this->id . "<br>Class:" . $this->t_class . "<br>Faculty:" . $this->department . "<br>";
        // echo parent::getNum();
    }
    public function showNum()
    {
        echo parent::getNum();
    }
}


$s1 = new student();
$s1->setInfo("Chen Zhen", 22, "male", 20170521, "Accounting Department 1701", "Computer Science and Technology");
echo $s1->getInfo();
$t1 = new teacher();
$t1->setInfo("Wang Duxiu", 26, "female", 1007, "Accounting 1701, 1702, 1703, 1704", "Information Technology College");
echo $t1->getInfo();
$p1 = new Person();
$p2 = new Person();
$p3 = new Person();
Person::getNum();

The results are as follows:

I am a student
 Name: Chen Zhen
 Age: 22
 Gender: Male
 School number: 20170521
 Class: Accounting 1701
 Specialty: Computer Science and Technology
I am a teacher
 Name: Wang Duxiu
 Age: 26
 Gender: Women
 Number: 1007
 Class: Accounting 1701, Accounting 1702, Accounting 1703, Accounting 1704
 Faculty: College of Information Technology
I am a person!
I am a person!
I am a person!

The number of people instantiated is:3
byebyebyebyebye

Be careful
This case may not be completely understood, but is written through the individual's understanding of the case. If there are any errors or incorrect copies of the operation, please kindly ask your forefathers to point out one or two in order to promote common progress. Thank you.

Posted by moise on Fri, 08 May 2020 10:33:34 -0700