类: 类与对象字面量和接口差不多,比较两个类类型的对象时,只有实例的成员会被比较。 静态成员和构造函数不在比较的范围内。 class Animal { feet: number; constructor(name: string, numFeet: number) {} } class Size { feet: number; constructor(numFeet: number) { } } let a: Animal = new Size(1); //冒号后面仅仅只是检验属性是不是一样的,不是类型 let s: Size = new Animal("",0); function creat(x:Animal){} //冒号只是检验属性,不做类型检查, creat(a); creat(s); console.log(a);//Size {} console.log(s);//Animal {} ----------------------------------------------------------- 交叉类型: class A{ constructor(public _a1:number,public _a2:number){ this._a1 = _a1; this._a2 = _a2; } fa1(){} } interface b{ b1:number; f1(); } class B implements b{ b1:number; f1(){ console.log("f1"); } fb1(){} constructor(b1:number,public _B1:number,public _B2:number){ this.b1 = b1; this._B1 = _B1; this._B2 = _B2; } } var a = new A(0,2); a._a1 = 1; var b = new B(3,4,5); console.log(a); for(let i in a){ console.log(i + "--" + typeof i); } console.log(b); for(let i in b){ console.log(i + "----" + typeof i); } var result; function extend<T,U>(a : T, b : U): T & U { result = <T & U>{}; for(let i in a){ //(<T & U>result)[i] = (<T & U>a)[i]; //result[i] = a[i]; (<any>result)[i] = (<any>a)[i]; } for(let i in b){ if(!result.hasOwnProperty(i)){ // (<T & U>result)[i] = (<T & U>b)[i]; result[i] = b[i]; (<any>result)[i] = (<any>b)[i]; } } return result; } extend(a,b); console.log("-----------------"); console.log(result); for(let i in result){ console.log(i + "----" + typeof i); }
本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/7227304.html,如需转载请自行联系原作者