web前端全系列 教程
1839个小节阅读:2243.7k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
在 TypeScript 里,我们可以使用常用的面向对象模式。 基于类的程序设计中一种最基本的模式,是允许使用继承来扩展现有的类
xxxxxxxxxx
class Animal {
move(distanceInMeters: number = 0) {
console.log(`Animal moved ${distanceInMeters}m.`);
}
}
class Dog extends Animal {
bark() {
console.log('Woof! Woof!');
}
}
const dog = new Dog();
dog.bark();
dog.move(10);
因为 Dog
继承了 Animal
的功能,因此我们可以创建一个 Dog
的实例,它能够 bark()
和 move()
xxxxxxxxxx
class Animal{
public name:string;
// 温馨提示:子类继承父类,父类存在构造函数,子类必须实现构造函数
constructor(name:string){
this.name = name;
}
move(distanceInMeters:number){
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
class Dog extends Animal{
constructor(name:string){
super(name)
}
drak(){
console.log(this.name + " Woof! Woof!")
}
}
class Snake extends Animal{
constructor(name:string){
super(name)
}
move(distanceInMeters: number): void {
console.log(this.name + ":---" + distanceInMeters)
// 执行父类的move方法
super.move(distanceInMeters)
}
}
const d = new Dog("dog")
d.move(100)
d.drak()
const s = new Snake("蛇")
s.move(1000)
实时效果反馈
1. 在TypeScript中,类实现继承的关键字是:
A function
B extends
C class
D interface
答案
1=>B