`

TypeScript语法基础 - 类相关用法

阅读更多

   1. ts中的类的写法

 

class Person5{
	name:string; //属性,前面省略了public关键字
	constructor(name:string) {//构造函数
		this.name=name;
	}
	
	run():void{
		console.log(this.name+"在跑路");
	}
	
	getName():string{
		return this.name;
	}

	setName(name:string):void{
		this.name=name;
	}
}
let p1=new Person5('张三');
p1.run();//张三在跑路
console.log(p1.getName());// 获取name属性
p1.setName('kay');//设置name属性
console.log(p1.getName());// 重新获取name

 

 

   2. ts中的类的继承

 

class Person5{
	name:string; //属性,前面省略了public关键字
	constructor(name:string) {//构造函数
		this.name=name;
	}

	run():void{
		console.log(this.name+"在跑路");
	}

	getName():string{
		return this.name;
	}

	setName(name:string):void{
		this.name=name;
	}

}

//继承
class web5 extends Person5{
	constructor(name:string) {
		//super表示调用的父类构造函数,内部的this指向子类的的
		//super指向父类的原型对象,所以定义在父类实例上的方法或属性是无法通过super调用的。
		super(name);//初始化父类的构造函数
	}

}

let p1=new Person5('张三');
p1.run();//张三在跑路
console.log(p1.getName());// 获取name属性
p1.setName('kay');//设置name属性
console.log(p1.getName());// 重新获取name

 

 

   3. ts的类的继承方法重写

class Person5{
	name:string; //属性,前面省略了public关键字
	constructor(name:string) {//构造函数
		this.name=name;
	}

	run():void{
		console.log(this.name+"在跑路");
	}

	getName():string{
		return this.name;
	}

	setName(name:string):void{
		this.name=name;
	}
}

//继承 方法的重写
class web5 extends Person5{
	constructor(name:string) {
		//super表示调用的父类构造函数,内部的this指向子类的的
		//super指向父类的原型对象,所以定义在父类实例上的方法或属性是无法通过super调用的。
		super(name);//初始化父类的构造函数
	}

	run():string{
		return `${this.name}在运动`
	}

	work():string{
		return `${this.name}在工作`
	}
}

let w5=new web5('李四');
console.log(w5.work());
console.log(w5.run());
// let p1=new Person5('张三');
// p1.run();//张三在跑路
// console.log(p1.getName());// 获取name属性
// p1.setName('kay');//设置name属性
// console.log(p1.getName());// 重新获取name

 

    4. 类的修饰符

* 修饰符

* public 公有 在类里面。子类 类外面都能访问

* protected 保护类型 在类里面,子类里面可以访问,在类外无法访问

* private 私有 在类里面可以访问 子类 类外部都没法访问.

* 属性不加修饰符,默认是public

class Person5{
	public name:string; //属性,前面省略了public关键字
	constructor(name:string) {//构造函数
		this.name=name;
	}

	run():void{
		console.log(this.name+"在跑路");
	}

	getName():string{
		return this.name;
	}

	setName(name:string):void{
		this.name=name;
	}
}

//继承 方法的重写
class web5 extends Person5{
	constructor(name:string) {
		//super表示调用的父类构造函数,内部的this指向子类的的
		//super指向父类的原型对象,所以定义在父类实例上的方法或属性是无法通过super调用的。
		super(name);//初始化父类的构造函数
	}

	run():string{
		return `${this.name}在运动`
	}

	work():string{
		return `${this.name}在工作`
	}

}

let p5=new Person5('ddd');
//类外部访问
console.log(p5.name);//ddd
let w5=new web5('李四');
console.log(w5.work());
console.log(w5.run());

 

   5. 类的静态属性 ,静态方法

class Person5{
	public name:string; //属性,前面省略了public关键字
	constructor(name:string) {//构造函数
		this.name=name;
	}

	run():void{
		console.log(this.name+"在跑路");
	}

	getName():string{
		return this.name;
	}

	setName(name:string):void{
		this.name=name;
	}

}

//继承 方法的重写
class web5 extends Person5{
	public good:number=1222;
	//定义一个静态属性
	static timer:string='111';
	constructor(name:string) {
		//super表示调用的父类构造函数,内部的this指向子类的的
		//super指向父类的原型对象,所以定义在父类实例上的方法或属性是无法通过super调用的。
		super(name);//初始化父类的构造函数
	}

	run():string{
		return `${this.name}在运动`
	}

	work():string{
		return `${this.name}在工作`
	}

	//定义一个静态方法 静态方法,里面没法之间调用类里面的属性 只能调用静态属性

	static go():void{
		console.log(this.timer);
	}
}
let p5=new Person5('ddd');
//类外部访问
console.log(p5.name);//ddd
let w5=new web5('李四');
console.log(w5.work());
console.log(w5.run());
//调用静态方法
web5.go();//'111'

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics