ts静态属性、静态方法、抽象类、多态

静态属性、静态方法

function Person(){
	this.run1 = function (){} // 实例方法,实例化后调用
}

Person.run2 = function (){} // 静态方法,类名直接调用
Person.name = 'lucy' // 静态属性
Person.run2() // 静态方法的调用

静态方法、实例方法在jq中的应用

function $(element){
	return new Base(element)
}
$.get = function (){}
function Base(element){
	this.element = '获取dom节点'
	this.css = function (attr, value){
		this.element.style[attr] = value
	}
}
$('#box').css('color', red)

ts中定义静态方法

class Person{
	public name:string
	public age:numer=20
	static sex = '男' // 静态属性
	constructor(name:string){
		this.name = name
	}
	run () {
		console.log(`${this.name}在运动`)
	}
	work(){
		console.log(`${this.name}在工作`)
	}
	static print(){
		console.log('print静态方法'+Person.sex) // 静态方法,没法直接调用类的属性
	}
}
var p = new Person('alex')
p.work()
p.run()
Person.sex // 男
Person.print()

多态

父类定义一个方法不去实现,让继承他的子类去实现,每一个子类有不同的表现
多态属于继承

class Animal{
	name:string
	constructor(name:string){
		this.name
	}
	/* 具体吃什么不知道,具体吃什么继承它的子类去实现,
	每一个子类的表现不一样 */
	eat(){
		console.log('吃的方法')
	}
}

class Dog extends Animal{
	consturctor(name:string){
		super(name)
	}
	eat(){
		console.log(`${this.name}吃骨头`)
	}
}
class Cat extends Animal{
	constructor(name:string){
		super(name)
	}
	eat(){
		console.log(`${this.name}吃鱼`)
	}
}

ts中的抽象类

ts中的抽象类:它是提供其他类继承的基类,不能直接被实例化
abstract关键字定义抽象类和抽象方法,抽象类中的抽象方法不包含具体实现并且必须在派生类中实现
abstract抽象方法只能放在抽象类里面
抽象类和抽象方法定义标准

/* 标准Animal这个类要求它的子类必须包含eat方法 */
abstract class Animal{
	name:string
	constructor(name:string){
		this.name = name
	}
	abstract eat():any; // 抽象类中的抽象方法不包含具体实现并且必须在派生类中实现
	run () {} // 其他方法可以不再子类实现	
}

class Dog extends Animal{
	constructor(name:string){
		super(name)
	}
	/* 抽象类的子类必须实现抽象类里面的抽象方法 */
	eat (){
		console.log(`${this.name}吃骨头`)
	}
}

class Cat extends Animal{
	constructor(name:string){
		super(name)
	}
	eat(){
		console.log(`${this.name}吃鱼`)
	}
}

var d = new Dog('小狗')
d.eat()

var c = new Cat('小猫')
c.eat()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值