// class person{
// name: string = "默认";
// age: number = 0;
// static des: string
// constructor(name: string, age: number){
// this.name = name;
// this.age = age;
// }
// static test(){
// }
// }
// let a = new person("令狐冲", 20);
// 继承
// class person{
// name: string = "";
// say(){
// console.log("我是人类,叫做" + this.name);
// }
// }
// class student extends person {
// num: number = 0;
// score: number = 0;
// say(){
// super.say();
// console.log("我是学生,叫做" + this.name);
// }
// }
// let a = new student();
// a.name = "aaaaa";
// a.say();
//抽象类,用于被继承
// abstract class person{
// name: string = "";
// run() {
// }
// abstract say();
// }
// class student extends person{
// say(){
// }
// }
// let a: person = new student();
// a.say();
//接口
//人 狼 狼人:人,狼
// class Person{
// name: string;
// }
// interface IWolf{
// attack();
// }
// interface Idog{
// eat();
// }
// class Wolf_Man extends Person implements IWolf,Idog{
// attack(){
// }
// eat(){
// }
// }
// class person{
// _hp: number = 300;
// //取值
// get hp(){
// return this._hp;
// }
// //赋值
// set hp(vaule){
// if (vaule < 0) {
// this._hp = 0;
// } else {
// this._hp = vaule
// }
// }
// }
// let a = new person();
// a._hp = 9999
// a.hp -= 180;
// console.log(a.hp + "");
// class SoundManager{
// // static Instance = new SoundManager();
// private static instance: SoundManager;
// private constructor() {
// }
// static Instance() {
// //当前单例是否产生
// //懒加载
// if (!SoundManager.instance) {
// SoundManager.instance = new SoundManager();
// }
// return SoundManager.instance;
// }
// }
// //用声音管理类
// // SoundManager.Instance
// // SoundManager.Instance
// interface ICalc{
// calc(num1, num2): number;
// }
// class Npc1 implements ICalc{
// calc(num1, num2){
// return num1 + num2;
// }
// }
// class Npc2 implements ICalc{
// calc(num1, num2): number{
// return num1 - num2;
// }
// }
// class Person {
// //代理
// delegate: ICalc;
// //计算数字
// GetNum(num1,num2){
// //拿到num1和2计算后的结果
// let num = this.delegate.calc(num1, num2);
// console.log(num + "")
// }
// }
// let person = new Person();
// person.delegate = new Npc1();
// person.GetNum(3,4);
interface IObserver{
nameChanged(newName);
}
class Person{
private _name: string;
//所有观察者(新建一个新数组)
observers: Array<IObserver> = new Array<IObserver>();
set name(value) {
this._name = value;
//发生变化
//遍历观察者数组,给所有的观察者发消息
for (let observer of this.observers){
observer.nameChanged(this._name);
}
}
get name() {
return this._name;
}
}
class Test implements IObserver{
nameChanged(newName){
console.log('监听到名字变化为' + newName);
}
}
let person = new Person();
let test = new Test();
person.observers.push(test);
person.name = "哈哈哈";