鉴于此代码,可在此处尝试:
export abstract class AbstractSanitizedString {
constructor(private s: string) {
}
}
export class PersonNickname extends AbstractSanitizedString {
constructor(label: string) {
super(label + " Smith");
}
}
export class AlphaNumericString extends AbstractSanitizedString {
constructor(s: string) {
super(s);
}
}
export class Person {
constructor(private name: PersonNickname) {
}
}
new Person(new AlphaNumericString("John"));
我预计会出现编译错误,因为我发送了AlphaNumericString
,而我定义它只应该接收PersonNickname
。
这是对的吗?