繁体   English   中英

此Typescript类与此接口如何兼容

[英]How is this Typescript class compatible with this interface

我是TypeScript(和OOP)的新手。 我在他们的官方文档中找到了以下示例:

class Student {
    fullName: string;
    constructor(public firstName: string, public middleInitial: string, public lastName: string) {
        this.fullName = firstName + " " + middleInitial + " " + lastName;
    }
}

interface Person {
    firstName: string;
    lastName: string;
}

function greeter(person : Person) {
    return "Hello, " + person.firstName + " " + person.lastName;
}

let user = new Student("Jane", "M.", "User");

document.body.innerHTML = greeter(user);

我的看法是, class Student没有属性lastname ,因此在调用function greeter时不应与interface Person兼容。

我在这里想念什么?

该类确实具有字段lastName public lastName: string是速记字段声明。 这既是public字段又是参数声明。 这就是构造函数参数中修饰符的含义。 请参阅文档以获取更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM