相比像c# java高级语言,js多态就不需要写一个基类,不用在基类中写虚方法在子类中对父类中的虚方法进行覆盖
js多态简单很多
<script>
class student {
constructor(ming) {
this.name = ming;
}
getName() {
return this.name
}
}
class teacher {
constructor(ming, sui) {
this.name = ming;
this.age = sui;
}
getName() {
return this.name
}
getAge() {
return this.age;
}
}
let arr = [];
arr.push(new student("xu18"));
arr.push(new teacher("xu81", 68));
console.log(arr[0].name, arr[1].name); //打印结果: xu18 xu81
</script>
FR:徐海涛(hunk xu)