请使用html语言在写一遍
时间: 2024-03-12 18:49:42 浏览: 15
以下是将上述代码转化为 HTML 页面的示例:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Student and Teacher Classes</title>
</head>
<body>
<script>
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
introduce() {
console.log(`Hi, my name is ${this.name} and I am ${this.age} years old.`);
}
}
class Student extends Person {
constructor(name, age, className) {
super(name, age);
this.className = className;
}
introduce() {
console.log(`Hi, my name is ${this.name} and I am ${this.age} years old. I am in class ${this.className}.`);
}
}
class Teacher extends Person {
constructor(name, age, subject) {
super(name, age);
this.subject = subject;
this.classes = [];
}
teach(classes) {
this.classes.push(...classes);
}
hasTaught(student) {
for (let cls of this.classes) {
if (cls.includes(student.className)) {
console.log(`Yes, teacher ${this.name} has taught student ${student.name} in class ${student.className}.`);
return true;
}
}
console.log(`No, teacher ${this.name} has not taught student ${student.name}.`);
return false;
}
}
// Example usage
const alice = new Student("Alice", 15, "9A");
alice.introduce();
const bob = new Teacher("Bob", 35, "Math");
bob.teach([["9A", "9B"], ["10A", "10B"]]);
const charlie = new Student("Charlie", 16, "10B");
bob.hasTaught(charlie);
</script>
</body>
</html>
```
在上述代码中,我们使用了 `<script>` 标签来定义 JavaScript 类。我们首先定义了一个 Person 类,然后定义了一个 Student 类和一个 Teacher 类,它们都继承自 Person 类。代码的其余部分展示了这些类的用法示例。
注意:由于这是一个 JavaScript 示例,需要将代码保存为 `.html` 文件并在浏览器中打开,才能看到控制台输出。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)