如何在ts的接口中封装方法
时间: 2023-04-10 17:02:49 浏览: 175
C语言写的TS流打包,接口用C++封装了一下
5星 · 资源好评率100%
可以使用interface来封装方法,例如:
interface Person {
name: string;
age: number;
sayHello(): void;
}
class Student implements Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
sayHello() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}
const student = new Student('John', 20);
student.sayHello(); // Output: "Hello, my name is John and I am 20 years old."
阅读全文