typescript定义类
类
- typescript定义类
- typescript继承
- 类里面的修饰符
- typesctipt类的定义
//TS定义类 class Person { name:string; age:number; constructor(n:string,c:number){ this.name = n; this.age = c } r():void{ console.log(this.name,this.age) } getName():string{ return this.name } setName(name:string):void{ this.name = name } } var p = new Person('张三',23); p.r() console.log(p.getName()) p.setName('1234') console.log(p.getName())
- 实现继承(extends和super关键字来实现)
//定义类 class Person { name:string; constructor (name:string) { this.name = name; } run ():string { return this.name } } var p = new Person('你好') console.log(p.run())//你好 //继承(web继承person) class Web extends Person { constructor (name:string) { super(name) } run(){ return this.name } work(){ console.log(this.name) } } var w = new Web('aasdf121') // console.log(w.run())//aasdf121 w.work()//aasdf121
- 类里面的修饰符
- typesctipt中定义属性提供三种修饰符
- public:公有 在类里面、子类、类外面都可以访问
- protected:保护类型 在类里面、子类里面都可以访问,类外面不可以访问
- private:私有 在类里面可以访问,子类、类外面都不可以访问
注:属性不加修饰符默认公有
- public(公有属性)
/定义类 class Person { public name: string; constructor(name: string) { // 自己的类里 this.name = name; } run(): string { return `${this.name}` } } var p = new Person('hello') console.log(p.run()) //继承 class Web extends Person { //子类 constructor(name: string) { super(name) } run(): string { return `run:${this.name}` } word(): void { console.log('word:', this.name) } } var w = new Web('word') console.log(w.run()) w.word() //在类外面访问公有属性 var p = new Person('hahah') console.log('p',p.name)//hahah
- public(公有属性)
- protected(保护类型)
// protected(保护类型) //定义类 class Person { protected name: string; constructor(name: string) { // 自己的类里 this.name = name; } run(): string { return `${this.name}` } } var p = new Person('hello') console.log(p.run()) //继承 class Web extends Person { //子类 constructor(name: string) { super(name) } run(): string { return `run:${this.name}` } word(): void { console.log('word:', this.name) } } var w = new Web('word') console.log(w.run()) w.word() //在类外面访问保护属性 var p = new Person('hahah') // console.log('p',p.name)//能运行(原因是他在js中转行成es5的代码),但编译器报错
- private(私有)
//private(私有) //定义类 class Person { //类里面 private name:string; constructor(name:string){ this.name = name } run ():string{ return this.name } } var p = new Person('你好') console.log(p.run())//你好 (在类里私有属性可以用) class Web extends Person{ //子类 constructor(name:string){ super(name) } run():string{ return this.name//报错 private(私有)只能在类里使用不能在子类使用 } } var w = new Web('lala') console.log(w.run())//子类里 var p = new Person('hahha') console.log(p.name)//报错private(私有)不能再类外面使用
- typesctipt中定义属性提供三种修饰符
- typesctipt类的定义
静态方法
- 静态方法(static关键字)
- 静态方法里面不能调动类里面的属性
- 如果要用类里的属性,就把类中的属性改为静态属性 (用static)
-
class Person { public name:string; static sex:string = '男'//静态属性 constructor(name:string){ this.name =name } run(){ //实例方法 console.log(`${this.name}1`) } word():string{ return `${this.name}2` } static print(){ console.log('静态方法 print'+this.sex) console.log('静态方法 print'+Person.sex) } } //调用实例方法 var p = new Person('nihao') p.run() console.log(p.word()) //调用静态方法 //类名.静态方法名 Person.print() console.log(Person.sex)
-
多态
- 多态是属于继承的一种
- 含义:父类定义一个方法不去实现,让继承它的子类去实现,没一个子类有不同的表现
- 多态实例
class Polymorphic { name: string constructor(name: string) { this.name = name } method() { console.log('aaa') } } class inherit extends Polymorphic { constructor(name: string) { super(name) } method(): string { return this.name + '+1' } } class inherit2 extends Polymorphic { constructor(name: string) { super(name) } method(): string { return this.name + '+2' } } var i1 = new inherit('多态1')//多态1+1 console.log(i1.method()) var i2 = new inherit2('多态2') console.log(i2.method())//多态2+2
抽象方法和抽象类
- 抽象类只能被继承,不能被实例化
- 定义抽象类使用abstract关键字
- 抽象方法只能放在抽象类中
-
abstract class Hello { public name:string constructor(name:string){ this.name =name } abstract eat():any //继承Hello这个类的子类必须要有eat(){}函数 run(){ } } class Word extends Hello{ constructor(name:string){ super(name) } eat(){ } }