基本使用
class A {public name: string;private age: number;protected sex: string;constructor(name: string, age: number, sex: string) {this.name = name;this.age = age;this.sex = sex;}public getName() {return this.name;}private getAge() {return this.age;}protected getSex() {return this.sex;}}const aa = new A('wangyang', 11, 'nan');// console.log(aa);// console.log(aa.name);// console.log(aa.age); // 属性“age”为私有属性,只能在类“A”中访问// console.log(aa.sex); // 属性“sex”受保护,只能在类“A”及其子类中访问// console.log(aa.getName());// console.log(aa.getAge()); // 属性“getAge”为私有属性,只能在类“A”中访问// console.log(aa.getSex()); // 属性“getSex”受保护,只能在类“A”及其子类中访问class B extends A {city: string;constructor(name: string, age: number, sex: string, city: string) {super(name, age, sex);this.city = city;}getBName() {console.log(super.getName());}getBAge() {// console.log(super.getAge()); // 属性“getAge”为私有属性,只能在类“A”中访问// console.log(this.age); // 属性“age”为私有属性,只能在类“A”中访问s}getBSex() {console.log(super.getSex());}}const bb = new B('zhangsan', 123, '女', '杭州');bb.getBName();bb.getBSex();// bb.getSex(); // 属性“getSex”受保护,只能在类“A”及其子类中访问
protected 修饰 constuctor 构造函数之后。该类只能被子类继承。 不能被实例化。
protected 修饰 constuctor 构造函数之后。该类只能被子类继承。 不能被实例化。类似 用 abstract 修饰一个类 [抽象类]class A {protected constructor () {}}const a = new A() // Error: 类“A”的构造函数是受保护的,仅可在类声明中访问。ts(2674)
js 抽象类的实现
原型继承
// js 抽象类function AbstraFn(name) {if (new.target === AbstraFn) {throw '抽象类不能被实例化,只能被继承';}this.name = name;}AbstraFn.prototype.abc = function () {console.log('AbstraFn 原型上的方法');};// const abstraFN = new AbstraFn('123'); // errorfunction Child(name) {AbstraFn.call(this, name); // 构造函数继承}// 原型链继承, 父类的实例给子类的原型,还需把子类的构造函数改回来Child.prototype = Object.create(AbstraFn.prototype);console.log('123', Child.prototype.constructor); // AbstraFn 构造函数被改变了Child.prototype.constructor = Child;const child = new Child('zhangsan');console.log(child.name);child.abc();
class 继承
class D {name: string;constructor(name) {if (new.target === D) {throw new Error('该类只能被子类继承');}this.name = name;}eat() {return `${this.name}`}}// const d = new D('helo');class E extends D {constructor(props) {super(props);}getProps() {console.log(super.eat());}}const e = new E('world');e.getProps();
readonly
class A {public readonly name: stringconstructor(name: string) {this.name = name}}const a = new A('zhangsan')a.name = 'lisi' // Error
可选属性
class A {public name: stringpublic age?: numberconstructor (name:string, age?: number, public sex?: string) {this.name = namethis.age = age}}const a = new A('张三')const b = new A('李四', 18)const c = new A('王五',20, 'male')console.log(a) // A {sex: undefined, name: "张三", age: undefined}console.log(b) // A {sex: undefined, name: "李四", age: 18}console.log(c) // A {sex: "male", name: "王五", age: 20}
抽象类 & 抽象方法
abstract class A {constructor(public name: string) {}abstract getName(): string;}// const a = new A () // 无法创建抽象类的实例。ts(2511)class B extends A {constructor(name: string) {super(name);this.name = name;}public getName() { // 抽象的方法必须实现return this.name;}}
abstract class A {abstract _name:stringabstract get insideName():stringabstract set insideName(val: string): string // error 不能标记 返回值 类型。abstract func ():void}
implements 类继承接口
interface Food {type:string,size:number}class Tomoto implements Food {type: stringsize: numberconstructor(type: string, size: number) {this.type = typethis.size = size}}# 接口 检测的是该类实例化的 对象// 也就是class Tomoto implements Food { //Error: 类型 "Tomoto" 中缺少属性 "type",但类型 "Food" 中需要该属性。ts(2420)public static type: stringpublic size: number = 10}
接口继承类
接口继承一个类后,会继承该类的成员,但是 不包括其实现 ( 即只继承他的成员 和 成员类型)。接口还会继承private,protected 修饰的成员。当接口继承了 有这2个修饰符修饰的成员后,该接口只能被 该类,或者该类的子类 使用。
class A {private name:string = ''}interface I extends A {}class B implements I {} // Error: 类型 "B" 中缺少属性 "name",但类型 "I" 中需要该属性。class B implements I {private name: string = '' // Error: 类“B”错误实现接口“I”。 类型具有私有属性“name”的单独声明。ts(2420)}class B extends A implements I {}
泛型中使用类
const create = <T>(c: new () => T): T => new c();class A {public name: string = 'HELLO';}const a = create<A>(A);console.log(a.name);------------------------------------------------------------const create = <T> (c: new(param:number)=> T, param: number): T=> new c(param)class A{public age: numberconstructor(age:number) { this.age = age}}create<A>(A, 11)
