constructor(name: string, age: number, grade: number) { super(name, age); // 调用父类的 constructor this.grade = grade; } study(): void { console.log(`${this.name} is studying in grade ${this.grade}.`); } } const student1 = new Student('Bob', 18, 12); student1.sayHello()...
6 interface Person extends Animal { // 继承自Animal接口 7 use(): void; 8 } 9 10 class People implements Person { 11 name: string; 12 constructor(theName: string) { 13 this.name = theName; 14 } 15 16 eat() { 17 console.log(`${this.name} 拒绝吃狗粮。`) 18 } 19 20 use() ...
type Point { // type aliases x: number; y: number; } interface Point { // interface...
二、接口继承接口 interface Animal { name: stringdo(value: string):void} interface Cat extends Animal {/*接口继承接口*/name: string eat(value: string):void} class things implements Cat {/*实现Cat接口,也必须含有Animal接口的eat方法*/name: string; constructor(name: string) {this.name =name }...
在这个例子中,interface和type都可以定义一个对象类型,并且在使用上几乎没有区别。 2. 扩展(Extend) 2.1interface的扩展 interface可以通过继承的方式进行扩展: interface User { name: string; age: number; } interface Admin extends User { role: string; ...
interfacePerson {name:string;age:number;greet():void; } 这个示例定义了一个名为Person的接口,该接口要求对象必须包含name和age两个属性,分别为字符串类型和数字类型,并且包含一个没有返回值的greet方法。 2. 实现接口 classStudentimplementsPerson {constructor(public name:string,public age:number) {}greet()...
constructor(name: string) { this.name = name; } eat() { console.log(`${this.name} is eating.`); } } interface就是用来实现的,就像信任就是用来辜负的一样。 2. 同名interface 可以被合并,而 type 不行。 在同一作用域内定义了两个相同名称的 interface,TypeScript 会将它们合并为一个。但是如果...
interface 内部可以使用new关键字,表示构造函数。 interfaceErrorConstructor{new(message?:string):Error;} 上面示例中,接口ErrorConstructor内部有new命令,表示它是一个构造函数。 TypeScript 里面,构造函数特指具有constructor属性的类,详见《Class》一章。 interface 的继承 ...
interface SearchFunc { (source: string, subString: string): boolean; } /* 这样定义后,我们可以像使用其它接口一样使用这个函数类型的接口。 下例展示了如何创建一个函数类型的变量,并将一个同类型的函数赋值给这个变量。 */ const mySearch: SearchFunc = function (source: string, sub: string): boolean...
interfacePerson{name:string;sayHello():void;}functionfn(per:Person){per.sayHello();}fn({name:'孙悟空',sayHello(){console.log(`Hello, 我是${this.name}`)}}); 示例(实现) interfacePerson{name:string;sayHello():void;}classStudentimplementsPerson{constructor(publicname:string){}sayHello(){console...