创建一个people类,包含id、name、age;。 创建一个 student 类,继承people类,包含 school、gradetclass、sno,一个function(输出所有信息);PHP
时间: 2023-12-10 13:40:37 浏览: 77
以下是 PHP 代码实现:
```php
<?php
class People {
public $id;
public $name;
public $age;
function __construct($id, $name, $age) {
$this->id = $id;
$this->name = $name;
$this->age = $age;
}
}
class Student extends People {
public $school;
public $grade_class;
public $sno;
function __construct($id, $name, $age, $school, $grade_class, $sno) {
parent::__construct($id, $name, $age);
$this->school = $school;
$this->grade_class = $grade_class;
$this->sno = $sno;
}
function display_info() {
echo "ID: " . $this->id . "<br>";
echo "Name: " . $this->name . "<br>";
echo "Age: " . $this->age . "<br>";
echo "School: " . $this->school . "<br>";
echo "Grade and Class: " . $this->grade_class . "<br>";
echo "Student Number: " . $this->sno . "<br>";
}
}
$student = new Student(1, "Alice", 18, "ABC High School", "Grade 12, Class 1", "001");
$student->display_info();
?>
```
运行以上代码,输出结果为:
```
ID: 1
Name: Alice
Age: 18
School: ABC High School
Grade and Class: Grade 12, Class 1
Student Number: 001
```
阅读全文
相关推荐


















