抽象类
abstract class Animal {
public name:string;
constructor(name:string){
this.name = name;
}
abstract eat():void;
getName():string{
return this.name;
}
}
class Dg extends Animal {
constructor(name:string){
super(name);
}
eat():void{
console.log('Dog:'+ this.name +' 在吃东西');
}
}
接口
属性约束接口
interface ajaxConfig{
type:string;
url:string;
data?:string;
dataType:string;
}
function ajax(config:ajaxConfig){
var xhr=new XMLHttpRequest();
xhr.open(config.type,config.url,true);
xhr.onreadystatechange=function(){
if(xhr.readyState==4 && xhr.status==200){
console.log('chengong');
if(config.dataType=='json'){
console.log(JSON.parse(xhr.responseText));
}else{
console.log(xhr.responseText)
}
}
}
xhr.send(config.data);
}
ajax({
type:'get',
data:'name=zhangsan',
url:'http://a.itying.com/api/productlist',
dataType:'json'
})
函数约束接口
interface encrypt{
(key:string,value:string):string;
}
var md5:encrypt=function(key:string,value:string):string{
return key+value;
}
console.log(md5('name','zhangsan'));
var sha1:encrypt=function(key:string,value:string):string{
return key+'----'+value;
}
console.log(sha1('name','lisi'));
类约束接口
interface IAnimal {
name:string;
eat():void;
}
interface IDog extends IAnimal{
age:number;
work():void;
}
class Dog implements IDog{
name:string;
age:number;
constructor(name:string,age:number){
this.name = name;
this.age = age;
}
eat(){
console.log("Dog:"+this.name+' 在吃东西');
}
work(){
console.log("Dog:"+this.name+" 是一直警犬");
}
getName(){
return this.name;
}
}