最常见的企业建站程序有,自己做的网站能在线支付,织梦网站模板如何安装教程,鑫迪一键建站系统系列文章目录
TypeScript 从入门到进阶系列
TypeScript 从入门到进阶之基础篇(一) ts基础类型篇TypeScript 从入门到进阶之基础篇(二) ts进阶类型篇TypeScript 从入门到进阶之基础篇(三) 元组类型篇TypeScript 从入门到进阶之基础篇(四) symbol类型篇TypeScript 从入门到进阶…
系列文章目录
TypeScript 从入门到进阶系列
TypeScript 从入门到进阶之基础篇(一) ts基础类型篇TypeScript 从入门到进阶之基础篇(二) ts进阶类型篇TypeScript 从入门到进阶之基础篇(三) 元组类型篇TypeScript 从入门到进阶之基础篇(四) symbol类型篇TypeScript 从入门到进阶之基础篇(五) 枚举类型篇TypeScript 从入门到进阶之基础篇(六) 类型断言 、推论、别名| 联合类型 | 交叉类型 篇TypeScript 从入门到进阶之基础篇(七)泛型篇TypeScript 从入门到进阶之基础篇(八)函数篇TypeScript 从入门到进阶之基础篇(九) Class类篇 持续更新中… 文章目录 系列文章目录TypeScript 从入门到进阶系列前言一、抽象类abstract的使用二、抽象类abstract的使用场景 前言 TypeScript 抽象类是一种用于定义其他类的基础类它不能直接被实例化只能被继承。抽象类可以包含属性、方法、以及其他抽象方法的定义。抽象方法只有定义而没有具体实现需要在具体的子类中实现。 一、抽象类abstract的使用 抽象类通过使用关键字 abstract 来定义而抽象方法通过在方法前加上关键字 abstract 来定义。抽象类和抽象方法用于定义类的行为和结构并且可以提供一些通用的实现或规范以供子类继承和实现。 抽象类在面向对象编程中有以下特点
不能直接实例化抽象类不能被直接实例化只能被继承后使用。可以包含属性和方法抽象类中可以定义属性和方法可以有具体实现的方法和抽象方法。可以包含抽象方法抽象类中可以定义抽象方法这些方法只有方法的声明而没有具体实现需要在具体的子类中实现。子类必须实现抽象方法当一个类继承了抽象类时它必须实现抽象类中的所有抽象方法。
下面是一个使用抽象类的例子
//抽象类
abstract class Animal {name: string;constructor(name: string) {this.name name;}//抽象方法 只能有方法的声明而没有具体实现//要实现只能在继承此类之后去实现abstract makeSound(): void;move(distance: number 0) {console.log(${this.name} moved ${distance}m);}
}//Dog 继承 Animal 抽象类
class Dog extends Animal {constructor(name: string) {super(name);}//将Animal 抽象类中的 抽象方法 进行实例化makeSound() {console.log(${this.name} barks);}
}let dog new Dog(Buddy);
dog.makeSound(); // 输出: Buddy barks
dog.move(10); // 输出: Buddy moved 10m在这个例子中Animal 类是一个抽象类它有一个抽象方法 makeSound 和一个实现方法 move。Dog 类继承了 Animal 类并实现了 makeSound 方法。
你不能直接实例化抽象类但可以通过继承抽象类的方式创建实例。在这个例子中我们创建了一个名为 dog 的 Dog 类的实例并调用了它的 makeSound 和 move 方法。
二、抽象类abstract的使用场景 TypeScript抽象类的使用场景: 抽象类可以被用作基类它提供了一个通用的模板或蓝图用于派生出其他类。抽象类可以定义抽象方法这些方法在派生类中必须被实现。这样可以确保派生类具有某些特定的功能或行为。抽象类可以实现一些通用的功能而不用在每个派生类中重新实现这些功能。这样可以使代码更加可维护和可扩展。抽象类可以用于限制实例化。它们不能直接被实例化只能被用作继承。抽象类可以定义属性和方法这些属性和方法可以被派生类继承和重写。
总之抽象类在需要定义一个通用模板或蓝图并确保派生类具有某些特定功能或行为的情况下非常有用。它们可以提供代码重用、可维护性和可扩展性。 一个常见的应用场景案例是在Web应用开发中使用抽象类来建模和组织组件。 假设我们正在开发一个在线购物应用其中有多种类型的商品例如电子产品、服装和食品。我们可以使用抽象类来定义一个通用的商品类并为每种具体类型的商品创建一个子类。 首先我们定义一个抽象商品类
abstract class Product {protected name: string;protected price: number;constructor(name: string, price: number) {this.name name;this.price price;}abstract getDescription(): string;
}然后我们创建几个具体类型的商品类
class Electronics extends Product {protected brand: string;constructor(name: string, price: number, brand: string) {super(name, price);this.brand brand;}getDescription(): string {return This ${this.brand} ${this.name} costs $${this.price};}
}class Clothing extends Product {protected size: string;constructor(name: string, price: number, size: string) {super(name, price);this.size size;}getDescription(): string {return This ${this.size} ${this.name} costs $${this.price};}
}class Food extends Product {protected expirationDate: string;constructor(name: string, price: number, expirationDate: string) {super(name, price);this.expirationDate expirationDate;}getDescription(): string {return This ${this.name} expires on ${this.expirationDate};}
}现在我们可以使用这些具体类型的商品类创建实例并调用它们的getDescription方法来获取商品的描述
const iphone new Electronics(iPhone, 999, Apple);
console.log(iphone.getDescription()); // 输出: This Apple iPhone costs $999const shirt new Clothing(T-Shirt, 29, M);
console.log(shirt.getDescription()); // 输出: This M T-Shirt costs $29const milk new Food(Milk, 2.99, 2022-01-31);
console.log(milk.getDescription()); // 输出: This Milk expires on 2022-01-31这个例子中抽象类Product定义了一个共享的属性和方法而具体的商品类继承了这些属性和方法并根据自己的特性实现了getDescription方法。一个常见的应用场景案例是在Web应用开发中使用抽象类来建模和组织组件。
假设我们正在开发一个在线购物应用其中有多种类型的商品例如电子产品、服装和食品。我们可以使用抽象类来定义一个通用的商品类并为每种具体类型的商品创建一个子类。
首先我们定义一个抽象商品类
abstract class Product {protected name: string;protected price: number;constructor(name: string, price: number) {this.name name;this.price price;}abstract getDescription(): string;
}然后我们创建几个具体类型的商品类
class Electronics extends Product {protected brand: string;constructor(name: string, price: number, brand: string) {super(name, price);this.brand brand;}getDescription(): string {return This ${this.brand} ${this.name} costs $${this.price};}
}class Clothing extends Product {protected size: string;constructor(name: string, price: number, size: string) {super(name, price);this.size size;}getDescription(): string {return This ${this.size} ${this.name} costs $${this.price};}
}class Food extends Product {protected expirationDate: string;constructor(name: string, price: number, expirationDate: string) {super(name, price);this.expirationDate expirationDate;}getDescription(): string {return This ${this.name} expires on ${this.expirationDate};}
}现在我们可以使用这些具体类型的商品类创建实例并调用它们的getDescription方法来获取商品的描述
const iphone new Electronics(iPhone, 999, Apple);
console.log(iphone.getDescription()); // 输出: This Apple iPhone costs $999const shirt new Clothing(T-Shirt, 29, M);
console.log(shirt.getDescription()); // 输出: This M T-Shirt costs $29const milk new Food(Milk, 2.99, 2022-01-31);
console.log(milk.getDescription()); // 输出: This Milk expires on 2022-01-31这个例子中抽象类Product定义了一个共享的属性和方法而具体的商品类继承了这些属性和方法并根据自己的特性实现了getDescription方法。