关于Duck Typing在维基上的说明
使用ts的实现
class Duck{
quack(){
console.log('呱呱呱');
}
feathers(){
console.log('是一个有灰色的羽毛的鸭子');
}
}
class Person{
quack(){
console.log('这个人在模仿一只鸭子,呱呱叫');
}
feathers(){
console.log('穿着一个鸭绒背心的人');
}
}
function inTheForest(duck:any){
duck.quack();
duck.feathers();
}
function game(){
var duck = new Duck();
var person = new Person();
inTheForest(duck);
inTheForest(person);
}
game();
本文通过 TypeScript 的示例介绍了 Duck Typing 的概念及其应用。展示了如何不依赖继承而是通过行为和属性来判断对象类型的方法。
1102

被折叠的 条评论
为什么被折叠?



