Skip to content

Polymorphism: dynamic dispatch and toString() · ⁨多态:动态分派与 toString()⁩

English

Polymorphism: one type, many behaviours

  • Polymorphism means "many forms": one superclass type, many subclass behaviours.
  • You can hold a Dog or a Cat in an Animal variable, because each is-an Animal.
  • When you call a method, the object's real type decides which version runs.

中文

多态:一种类型,多种行为

  • 多态(polymorphism)的意思是"多种形态":一个父类类型,多种子类行为。
  • 你可以把一个 Dog 或一个 Cat 放进一个 Animal 变量里,因为它们各自是一种 Animal。
  • 当你调用一个方法时,由对象的真实类型决定运行哪个版本。

同一句 a.speak() 会根据对象的真实类型给出 Woof、Meow 或 Moo

Handout

Log in or create account · ⁨登录或创建账户⁩

IGCSE, A-Level · ⁨IGCSE、A-Level⁩ & AP · ⁨与 AP⁩