ts学习总结2

67 阅读3分钟

ts类

public 公开属性 主打得就是一个原谅

protected 自己跟后代能访问 外部无法访问(实例无法访问,后代能在super时候访问

private 私有 只有自己class构造函数内能访问

class Circle{
private x:number
public y:number
public fn:()=>void
constructor(x:number,y:number=20){
this.x=x
this.y=y
this.fn=()=>{}
    }
}

写法1

class Animal{
public  name!:string //②在这里使用感叹号告诉构造函数这里得name绝对有值可以省略①处得代码
constructor(name:string){
//this.name=name //①这里可以注释掉

}

}

class Cat extends Aniaml{

constructor(name:string,age:number){
super(name);
}


}

const tom =new Cat('tom',30)

或者
写法2
这里相当于默认给构造函数函数创建了this跟属性指向

class Animal{

constructor(public name:string){
//this.name=name 这里直接干掉
}

}

class Cat extends Aniaml{
constructor(public name:string,public  age:number){
super(name);
//this.age=age 这里直接干掉
}


}

const tom =new Cat('tom',30)


加只读

readonly 标识仅读函数 意味着初始化后补课被修改

class Animal{

constructor(public readonly name:string){
this.name='鸡kun' //这里需要加上this指向用来初始化

}


changeName(){
this.name='cxk'//这里提示只读改不了
}


}

class Cat extends Aniaml{
constructor(public name:string,public  age:number){
super(name);
}


}

const tom =new Cat('tom',30)


子类重构父类

class Animal{

constructor(public  name:string){
this.name='鸡kun' //这里需要加上this指向用来初始化

}

//特殊点:构造函数里的void 意味着不关心函数得返回制 并不是为null或者undefind 
//单独函数类型返回void 是为空得意思
changeName(value:string,age:number):void{ //如果这里void改成number 下面子方法里得changeName只能返回数字
this.name='cxk'//这里提示只读改不了
}


}


//注意点:super在构造函数中指向是父类看(看注释①),在下面原型方法中调用得时候指向是父类得原型 (注释②)
class Cat extends Aniaml{
constructor(public name:string,public readonly  age:number){
super(name);//注释①
}

changeName(value:string){
super.changeName(value,100)//注释②
return 'abc'

}

}

const tom =new Cat('tom',30)

之前是原型方法,现在是原型属性的,原型属性需要构造器来实现

class Animal{

constructor(public  name:string){
this.name='鸡kun' //这里需要加上this指向用来初始化

}

//特殊点:构造函数里的void 意味着不关心函数得返回制 并不是为null或者undefind 
//单独函数类型返回void 是为空得意思
changeName(value:string,age:number):void{ //如果这里void改成number 下面子方法里得changeName只能返回数字
this.name='cxk'//这里提示只读改不了
}


//原型属性 获取
get aliasName(){
return 's'+this.name
}

//原型属性 设置
set alisName(name:string){
this.name=name
}

}



注意点
如果通过public  private 或者protected定义在constructor 函数上的定义属性,ts默认为是实例属性,原型方法不需要定义 

静态方法


class Animal{

constructor(public  name:string){
this.name='鸡kun' //这里需要加上this指向用来初始化

}

//特殊点:构造函数里的void 意味着不关心函数得返回制 并不是为null或者undefind 
//单独函数类型返回void 是为空得意思
changeName(value:string,age:number):void{ //如果这里void改成number 下面子方法里得changeName只能返回数字
this.name='cxk'//这里提示只读改不了

static a=1   //相当于 Cat.__proto__==Animal
static getA(){}
}


//原型属性 获取
get aliasName(){
return 's'+this.name
}

//原型属性 设置
set alisName(name:string){
this.name=name
}

}

以上同es6

单例模式

class Singleton{
static instance =new Singleton();//本身就是一个实例,后续一致用这一个不生产多个
protected constructor(){}//增加protected之后 构造函数不能被new了
static getInstance(){
return instance
}
}
类不需要在外面new
let instance=SingeLeton.getInstance()

抽象类 不能被new 抽象类可以被继承 抽象类中方法 子类必须实现

abstract class  Animal{
public  abstract a:string
drik(){ //非抽象已经实现了
console.log('aikun')
}
abstract eat():void;//抽象方法,父类没有子类必须实现

}

class Cat extends Animal{
publice a!:string;
eat():void{
throw new Error('xxxx')
}

}

目前写代码代码慢慢脱离继承了,组合优先于继承