TypeScript 类型和构造器的签名new

定义构造器签名分为三种方法:

        1、字面量定义

const myCalss3: new (name: string, age: number) => Teacher = Teacher;

         2、接口字面量

const myCalss4: { new(name: string, age: number): Teacher } = Teacher;

        3、接口定义构造器签名

// 接口中定义构造器(构造函数签名) :人 和 老师
interface myInterface1 {
    new(name: string, age: number)
}
//
const myClass1: myInterface1 = Person;
//
const tom = new myClass1('TOM', 20);

具体例子:

// 定义一个人的类
class Person {
    name: string;
    age: number;
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    };
    run(): void {
        console.log('Person name => : ', this.name, this.age);
    }
}

// 定义一个老师的类
class Teacher extends Person {
    run(): void {
        console.log('Teacher name => ', this.name);
        super.run();
    }
}

// 定义一个学生的类
class Student extends Person {
    identity: string;
    constructor(name: string, age: number, identity: string) {
        super(name, age);
        this.identity = identity;
    };
    run(): void {
        console.log('Student name => :', this.name);
        super.run();
    };
    sayBoook(): void {
        console.log(`Student say => ${this.identity}: 床前明月光,疑是地上霜`);

    }
}


// 接口中定义构造器(构造函数签名) : 学生
interface myInterface2 {
    new(name: string, age: number, identity: string);
}

// 接口中定义构造器(构造函数签名) :人 和 老师
interface myInterface1 {
    new(name: string, age: number)
}


// // 工厂函数加上泛型的使用。
// 接口字面量
function createPerson<T>(clazz: { new(name: string, age: number): T }, name: string, age: number): T {
    return new clazz(name, age);
}
//字面量 
function createPerson2<T>(clazz: new (name: string, age: number) => T, name: string, age: number): T {
    return new clazz(name, age);
}
// 接口
function createPerson3<T>(clazz: myInterface1, name: string = "defaultName", age: number = 18): T {
    return new clazz(name, age);
}

(
    function test() {
        const person1: Person = createPerson(Person, '张三', 25)
        const person2: Person = createPerson2(Person, '李四', 25)
        const person3: Person = createPerson3(Person, '王二')
        person1.run();
        person2.run();
        person3.run(); // 为啥会报错啊

        // 用接口interface定义构造器签名
        const myClass1: myInterface1 = Person;
        const myClass2: myInterface2 = Student;
        const tom = new myClass1('TOM', 20);
        const joker = new myClass2('Joker', 20, '学生');
        const joker2 = new myClass2('Joker2', 20, '学生');
        tom.run()
        joker.run();
        joker.sayBoook();
        joker2.run();
        joker2.sayBoook();


        //字面量 构造器签名
        const myCalss3: new (name: string, age: number) => Teacher = Teacher;
        // 接口字面量 构造器签名
        const myCalss4: { new(name: string, age: number): Teacher } = Teacher;
        const make = new myCalss4('Make', 30);
        const make2 = new myCalss3('make2', 30);
        make.run();
        make2.run();

    }
)()

 运行结果:

 end.tip:

 > 定义了签名的接口不可实现

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值