// 定义Game命名空间
namespace Game {
// 定义Characters命名空间
namespace Characters {
// 定义抽象类Character
export abstract class Character {
public health: number;
constructor(health: number) {
this.health = health;
}
// 定义抽象方法attack
public abstract attack(): string;
}
// 定义Warrior命名空间
namespace Warrior {
// 定义Knight类继承自Character
export class Knight extends Character {
public attack(): string {
this.health -= 10;
return 'Attacking!';
}
}
}
}
// 定义Maps命名空间
namespace Maps {
// 定义Map接口
export interface Map {
name: string;
size: number;
}
}
}
// 使用方式示例
// 导入Knight类
import { Knight } from './Game/Characters/Warrior';
// 导入Map接口
import { Map } from './Game/Maps';
// 创建Knight实例
const knight = new Knight(100);
console.log(knight.attack());
console.log(knight.health);
// 定义实现Map接口的对象
const myMap: Map = {
name: 'Example Map',
size: 1000
};
console.log(myMap.name);
console.log(myMap.size);