TypeScript学习笔记(二)

文章目录

接口

// 对象类型
interface SquareConfig {
    color?: string
    ary?: number[]
    width: number
    height: number
    readonly getArea: Function
}
const s1: SquareConfig = { width: 1, height: 2, ary: [1, 2], getArea: function (): number { return s1.width * s1.height } }
s1.width = 32
console.log(s1)
console.log(s1.getArea())

// 函数类型
interface SearchFunc {
    (source: string, subString: string): boolean
}
let mySearch: SearchFunc
mySearch = function (source, subString) {
    let result = source.search(subString)
    return result > -1
}
console.log(mySearch('aabbccdd', 'c'))

// 类类型
interface ClockInterface {
    currentTime: Date
    setTime(d: Date)
}

class Clock implements ClockInterface {
    currentTime: Date
    setTime(d: Date) {
        this.currentTime = d
    }
    getTime() {
        return this.currentTime
    }
    constructor(currentTime: Date) {
        this.currentTime = currentTime
    }
}

const Clock1 = new Clock(new Date('2020-06-30'))
console.log(Clock1.getTime())

// 接口相互继承
interface Shape {
    color: string
}
interface Square extends Shape {
    sideLength: number
}
const square: Square = { color: 'blue', sideLength: 10 }

// 接口继承类
interface ClockInterfaceExtend extends Clock {
    select(): void
}
const Clock2 = <ClockInterfaceExtend>{ currentTime: new Date('2019-05-28'), setTime: () => { }, getTime: () => { return Clock2.currentTime }, select: () => { } }
console.log(Clock2.getTime())

class Greeter {
    // 公共,私有与受保护的修饰符
    public greeting: string
    private color: string
    protected timer: Date
    // 构造函数
    constructor(message: string) {
        this.greeting = message
    }
    greet() {
        return "Hello, " + this.greeting
    }
    getColor() {
        return this.color
    }
    setColor(color: string) {
        this.color = color
    }
}

// 继承
class Greeter2 extends Greeter {
    private greeting2: string
    // 构造函数
    constructor(message: string) {
        super(message)
        this.greeting2 = message
    }
    setGreeting(message: string) {
        this.greeting = message
    }
    getTimer() {
        return this.timer
    }
    setTimer(timer: Date) {
        this.timer = timer
    }
}
const greeter2 = new Greeter2("world")
greeter2.setGreeting('Amercia')
greeter2.setColor('red')
greeter2.setTimer(new Date('2018-03-12'))
console.log(greeter2.greet())
console.log(greeter2.getColor())
console.log(greeter2.getTimer())

// 抽象类
abstract class Department {
    // readonly只读属性只能在声明和构造函数内赋值
    private readonly DepartmentName: string
    constructor(public name: string) {
        this.DepartmentName = name
    }
    printName(): void {
        console.log('Department name: ' + this.DepartmentName)
    }
    abstract printMeeting(): void // 必须在派生类中实现
}

class AccountingDepartment extends Department {
    constructor() {
        super('Accounting and Auditing') // 在派生类的构造函数中必须调用 super()
    }
    printMeeting(): void {
        console.log('The Accounting Department meets each Monday at 10am.')
    }
    generateReports(): void {
        console.log('Generating accounting reports...')
    }
}
const department = new AccountingDepartment() // 允许对一个抽象子类进行实例化和赋值
department.printName()
department.generateReports()

Mixins

// 混入操作函数
function applyMixins(derivedCtor: any, baseCtors: any[]) {
    baseCtors.forEach(baseCtor => {
        Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
            derivedCtor.prototype[name] = baseCtor.prototype[name]
        })
    })
}

// Disposable Mixin
class Disposable {
    isDisposed: boolean
    dispose() {
        this.isDisposed = true
    }

}
// Activatable Mixin
class Activatable {
    isActive: boolean
    activate() {
        this.isActive = true
    }
    deactivate() {
        this.isActive = false
    }
}
class SmartObject implements Disposable, Activatable {
    // Disposable
    isDisposed: boolean = false
    dispose: () => void
    // Activatable
    isActive: boolean = false
    activate: () => void
    deactivate: () => void
}

applyMixins(SmartObject, [Disposable, Activatable])
const smartObj = new SmartObject()
console.log(smartObj)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值