![](/img/trans.png)
[英]How to define a nonserializable property for TypeScript `class` or `interface`?
[英]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.