📜  原型设计模式打字稿代码示例

📅  最后修改于: 2022-03-11 14:48:14.927000             🧑  作者: Mango

代码示例1
interface IPrototype {
    getClone(): InstanceType
}

class Person implements IPrototype {
    public name: string
    public age: number
    public hobby: string

    constructor(name: string, age: number, hobby: string) {
        this.name = name
        this.age = age
        this.hobby = hobby
    }

    public getClone(): InstanceType {
        return this
    }
}

const john: Person = new Person('john doe', 30, 'programming')
console.log(john)

const jane = john.getClone()
jane.name = 'jane doe'
jane.hobby = 'swimming'

console.log(jane)

const ari = john.getClone()
ari.name = 'ari gunawan'
ari.age = 25
ari.hobby = 'music'

console.log(ari)