TypeScript--类
ts中定义类
class Person{ name:string; //属性 前面省略了public关键词 constructor(n:string){ //构造函数 实例化类的时候触发的方法 this.name = n; } run():void{ alert(this.name); } } var p = new Person('lisi'); p.run()
class Person{ name:string; constructor(name:string){ this.name = name; } getName():string{ return this.name; } setName(name:string):void{ this.name = name; } } var p = new Person('lisi'); alert(p.getName()); p.setName('wanwu'); alert(p.getName());
继承
class Person{ name:string; constructor(name:string){ this.name = name; } run():string{ return `${this.name}在运动`; } } var p = new Person('lisi'); alert(p.run()); class Web extends Person{ constructor(name:string){ super(name); } run():string{ return `${this.name}在运动--子类` } work(){ alert(`${this.name}在工作`) } } var w = new Web('wanwu'); alert(w.run()); w.work();
修饰符
- public :公有 在当前类里面、 子类 、类外面都可以访问
- protected:保护类型 在当前类里面、子类里面可以访问 ,在类外部没法访问
- private :私有 在当前类里面可以访问,子类、类外部都没法访问
public
class Person{ public name:string; constructor(name:string){ this.name = name; } run():string{ return `${this.name}在运动`; } } var p = new Person('lisi'); alert(p.run()); class Web extends Person{ constructor(name:string){ super(name); } run():string{ return `${this.name}在运动--子类` } work(){ alert(`${this.name}在工作`) } } var w = new Web('wanwu'); alert(w.run());
protected
class Person{ protected name:string; constructor(name:string){ this.name = name; } run():string{ return `${this.name}在运动`; } } var p = new Person('lisi'); alert(p.run()); class Web extends Person{ constructor(name:string){ super(name); } run():string{ return `${this.name}在运动--子类` } work(){ alert(`${this.name}在工作`) } } var w = new Web('wanwu'); alert(w.run());
静态属性 静态方法
class Person{ public name:string; /*公有属性*/ static sex = '男' //静态属性 constructor(name:string){ this.name=name; } run():string{ return `${this.name}在运动` } static print(){ // alert('print方法'+this.name); alert('print方法'+Person.sex); } } var p = new Person('lisi'); Person.print();
多态
多态:父类定义一个方法不去实现,让继承它的子类去实现,每一个子类有不同的表现
多态属性继承
父类有方法,子类也有同样的方法
class Animal{ name:string; constructor(name:string){ this.name = name; } //具体吃什么,不知道,具体吃什么,让继承它的子类去实现,每一个子类的表现不一样 eat(){ console.log('吃的方法') } } class Dog extends Animal{ constructor(name:string){ super(name); } eat(){ return this.name +'吃骨头' } } var d = new Dog('d'); alert(d.eat()); class Cat extends Animal{ constructor(name:string){ super(name); } eat(){ return this.name +'吃鱼' } } var c = new Cat('c'); alert(c.eat());
父类有方法,子类没有同样的方法
class Animal{ name:string; constructor(name:string){ this.name = name; } //具体吃什么,不知道,具体吃什么,让继承它的子类去实现,每一个子类的表现不一样 eat(){ console.log('吃的方法') } } class Dog extends Animal{ constructor(name:string){ super(name); } eat(){ return this.name +'吃骨头' } } var d = new Dog('d'); alert(d.eat()); class Cat extends Animal{ constructor(name:string){ super(name); } // eat(){ // return this.name +'吃鱼' // } } var c = new Cat('c'); alert(c.eat());
抽象类
抽象类:它是提供其它继承的基类,不能直接被实例化
- abstract关键字定义抽象方法,抽象类中的抽象方法不包含具体实现并且必须在派生类中实现
- abstract抽象方法只能放在抽象类里面
- 抽象类和抽象方法用来定义标准,标准:Animal这个类要求它的子类必须包含相同的方法,如eat()
直接被实例化,报错
abstract class Animal{ public name:string; constructor(name:string){ this.name = name; } abstract eat():any; run(){ console.log('运动') } } var a = new Animal();
父类定义的抽象方法,子类没有定义,报错
abstract class Animal{ public name:string; constructor(name:string){ this.name = name; } abstract eat():any; run(){ console.log('运动') } } class Dog extends Animal{ constructor(name:string){ super(name); } // eat(){ // console.log(this.name+'吃骨头') // } }
正常
类非抽象方法,子类可以不写
abstract class Animal{ public name:string; constructor(name:string){ this.name = name; } abstract eat():any; run(){ console.log('运动') } } class Dog extends Animal{ constructor(name:string){ super(name); } eat(){ console.log(this.name+'吃骨头') } } var d = new Dog('d'); d.eat() class Cat extends Animal{ constructor(name:string){ super(name); } eat(){ console.log(this.name+'吃鱼') } } var c = new Cat('c'); c.eat()
相关推荐
ChaITSimpleLove 2020-10-06
ChaITSimpleLove 2020-07-26
iconhot 2020-07-05
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06