TypeScript类的使用

类就是一类事物的抽象特点,包含属性和方法。

创建类

创建一个类通常使用lass关键字进行创建。
一个类通常包含类的成员属性、成员方法、构造函数这几个部分组成。

class Person{
	//成员字段
	name:string
	age:number
	//构造方法
	constructor(name:string,age:number){
		this.name = name;
		this.age = age;
	}
	//普通方法
	run():string{
		return this.name+this.age;
	}
}

TypeScript类的使用_第1张图片

类的实例化

在定义完类之后,我们通常会使用类,使用类的方式有继承、实例化等方式首先介绍类的实例化。

类的实例化通常使用new关键字进行实例化,以上面创建的Person类为例:
类在实例化的时候会执行构造函数的代码,如果构造函数能够接收参数,那么我们可以向构造函数中传递参数,类实例化返回的东西叫做对象。

let p = new Person("Mr.Lee",100)
console.log(p.run())

类的继承

类的继承通常使用extends关键字实现。
一个类继承另一个类,被继承的类称为父类或超类,继承的类称为父类的子类。在typescript中一个子类只能有一个父类。
一个类继承另一个类就会继承另一个类的公有方法、公有属性,不能继承类的私有属性。同时,在子类中可以使用super关键字引用父类的构造函数,这样就能简化子类的构造函数的书写。

class Person{
	//成员字段
	name:string
	age:number
	//构造方法
	constructor(name:string,age:number){
		this.name = name;
		this.age = age;
	}
	//普通方法
	run():string{
		return this.name+this.age;
	}
}
//儿子类,男人
class Man extends Person{
	
}
//儿子类,女人
class Woman extends Person{
	food:string="早餐"
	eat():string{
		return this.name+"吃"+this.food
	}
}
let m = new Man("男人",30)
console.log(m.run())
let w = new Woman("女人",20)
console.log(w.run())
console.log(w.eat())
//孙子类,老女人
class OldWoman extends Woman{
	
}
let o = new OldWoman("老妇女",60)
console.log(o.eat())

TS支持多级继承并且一个子类只能有一个父类

类方法的重写

有时,一个类继承另一个类后虽然都有相同的行为名称,但是行为的具体内容却不一样,因此,需要重写父类的方法。所谓重写就是在子类中声明一个同名的方法或属性,进而屏蔽父类的方法或属性。
重写的方法根据作用的不同分为构造方法的重写、普通方法的重写。

构造方法的重写

class Person {
	//成员字段
	name: string
	age: number
	//构造方法
	constructor(name: string, age: number) {
		this.name = name;
		this.age = age;
	}
	//普通方法
	run(): string {
		return this.name + this.age;
	}
}
//儿子类,男人
class Man extends Person{
	height:number
	constructor(name:string,age:number,height:number){
		super(name,age)
		this.height=height
	}
}

普通方法的重写

//儿子类,男人
class Man extends Person{
	height:number
	//构造方法的重写
	constructor(name:string,age:number,height:number){
		super(name,age)
		this.height=height
	}
	//普通方法的重写
	run():string{
		return super.run()+"身高为"+this.height;
	}
}

方法的重载

方法的重载就是根据参数的个数、参数的类型不同而执行不同的同名函数。方法的重载需要满足三个条件:

  1. 参数的个数不同
  2. 参数的类型不同
  3. 方法的返回值不同

符合上面三个中的一个就可以称之为方法重载。

构造方法的重载

class Person {
	//成员字段
	name: string
	age: number|undefined
	//构造方法
	//?:表示可选
	constructor(name: string, age?: number) {
		this.name = name;
		this.age = age;
	}
	// 普通方法
	run(): string {
		if(this.age===undefined){
			return this.name + "年龄保密";
		}
		return this.name +"年龄为"+ this.age;
	}
}

let p=new Person("Mr.Lee",100)
let p2 =new Person("Mr.Wang")
console.log(p.run())
console.log(p2.run())

普通方法的重载

class Person {
	//成员字段
	name: string
	age: number|undefined
	//构造方法
	constructor(name: string, age?: number) {
		this.name = name;
		this.age = age;
	}
	//普通方法
	//?:表示可选
	run(flag?:boolean): string {
		if(flag===true){
			return "无法显示"
		}
		if(this.age===undefined){
			return this.name + "年龄保密";
		}
		return this.name +"年龄为"+ this.age;
	}
}

let p=new Person("Mr.Lee",100)
let p2 =new Person("Mr.Wang")
console.log(p.run(true))
console.log(p2.run())

类的多态

不同的子类同一个方法,有不同的实现。

abstract Animal{
	abstract speak():void;
}
class Dog extends Animal{
	speak(){
		console.log('汪汪汪');
	}
}

class Cat extends Animal{
	speak(){
		console.log('喵喵喵');
	}
}

你可能感兴趣的:(typescript,typescript)