Today I encountered an error learning TypeScript polymorphism (i.e., inheritance)
Mistakes show
here indicates an error with super inheritance. String arguments cannot be assigned to string arguments. What the hell is this
To solve the error
See String
, think of it, change to String
try it? So he can
Modified polymorphic code
class Animal {
name:String
constructor(name:String) {
this.name = name
}
eat() {}
}
class Dog extends Animal{
constructor(name:String) {
super(name)
}
eat() {
return this.name + 'Eat Bean'
}
}
class Cat extends Animal{
constructor(name:String) {
super(name)
}
eat() {
return this.name + 'Eat?'
}
}
let dog = new Dog('WW')
console.log(dog.eat())
let cat = new Cat('MM')
console.log(cat.eat())
Questions remain
In fact, the previous use of string
inheritance is shown without error information, I don’t know why there is an error when using polymorphic inheritance, so I can only change it to string
according to the prompt. If you know the reason, please give instruction