TypeScript Class Inheritance and Polymorphism
TypeScript supports the inheritance and polymorphism features of object-oriented programming.
Class Inheritance
Use the extends keyword to implement inheritance.
Example
class
Animal
{
name
:
string
;
constructor
(
name
:
string
)
{
this
.
name
=
name
;
}
speak
(
)
:
void
{
console.
log
(
this
.
name
" makes a sound"
)
;
}
}
class
Dog
extends
Animal
{
breed
:
string
;
constructor
(
name
:
string
,
breed
:
string
)
{
super
(
name
)
;
// Call the parent class constructor
this
.
breed
=
breed
;
}
speak
(
)
:
void
{
console.
log
(
this
.
name
" Woof woof woof!"
)
;
}
}
var
dog
=
new
Dog
(
"Wangcai"
,
"Golden Retriever"
)
;
dog.
speak
(
)
;
Running result:
旺财 汪汪汪!
The super Keyword
super is used to call methods and the constructor of the parent class.
Example
class
Shape
{
color
:
string
;
constructor
(
color
:
string
)
{
this
.
color
=
color
;
}
describe
(
)
:
string
{
return
"This is a "
this
.
color
" shape"
;
}
}
class
Circle
extends
Shape
{
radius
:
number
;
constructor
(
color
:
string
,
radius
:
number
)
{
super
(
color
)
;
this
.
radius
=
radius
;
}
// Override the parent class method
describe
(
)
:
string
{
// Call the parent class method and extend it
return
super
.
describe
(
)
", with radius "
this
.
radius
;
}
area
(
)
:
number
{
return
Math
.
PI
this
.
radius
this
.
radius
;
}
}
var
circle
=
new
Circle
(
"Red"
,
5
)
;
console.
log
(
circle.
describe
(
)
)
;
console.
log
(
"Area: "
circle.
area
(
)
.
toFixed
(
2
)
)
;
Running result:
这是一个 红色的图形,半径是 5
面积: 78.54
Polymorphism
An instance of a child class can be assigned to a parent class type.
Example
class
Animal
{
name
:
string
;
constructor
(
name
:
string
)
{
this
.
name
=
name
;
}
speak
(
)
:
void
{
console.
log
(
this
.
name
" makes a sound"
)
;
}
}
class
Cat
extends
Animal
{
speak
(
)
:
void
{
console.
log
(
this
.
name
" Meow meow meow!"
)
;
}
}
class
Dog
extends
Animal
{
speak
(
)
:
void
{
console.
log
(
this
.
name
" Woof woof woof!"
)
;
}
}
// Polymorphism: an array stores instances of different child classes
var
animals
:
Animal
[
]
=
[
new
Cat
(
"Xiaobai"
)
,
new
Dog
(
"Wangcai"
)
,
new
Animal
(
"Animal"
)
]
;
// Calling the same method produces different implementations in different child classes
for
(
var
_i
=
0
,
animals_1
=
animals
;
_i
<
animals_1.
length
;
_i
++
)
{
var
animal
=
animals_1
[
_i
]
;
animal.
speak
(
)
;
}
Running result:
小白 喵喵喵!
旺财 汪汪汪!
动物 发出声音
instanceof check
Use instanceof to check the instance type.
Example
class
Rectangle
{
width
:
number
;
height
:
number
;
constructor
(
width
:
number
,
height
:
number
)
{
this
.
width
=
width
;
this
.
height
=
height
;
}
area
(
)
:
number
{
return
this
.
width
this
.
height
;
}
}
class
Circle
{
radius
:
number
;
constructor
(
radius
:
number
)
{
this
.
radius
=
radius
;
}
area
(
)
:
number
{
return
Math
.
PI
this
.
radius
**
2
;
}
}
var
shapes
=
[
new
Rectangle
(
4
,
5
)
,
new
Circle
(
3
)
]
;
for
(
var
_i
=
0
,
shapes_1
=
shapes
;
_i
<
shapes_1.
length
;
_i
++
)
{
var
shape
=
shapes_1
[
_i
]
;
if
(
shape
instanceof
Rectangle
)
{
console.
log
(
"Rectangle area: "
shape.
area
(
)
)
;
}
else
if
(
shape
instanceof
Circle
)
{
console.
log
(
"Circle area: "
shape.
area
(
)
.
toFixed
(
2
)
)
;
}
}
Running result:
矩形面积: 20
圆形面积: 28.27
protected members
protected members are visible in child classes.
Example
class
Person
{
protected name
:
string
;
constructor
(
name
:
string
)
{
this
.
name
=
name
;
}
}
class
Employee
extends
Person
{
private department
:
string
;
constructor
(
name
:
string
,
department
:
string
)
{
super
(
name
)
;
this
.
department
=
department
;
}
public introduce
(
)
:
string
{
// protected members can be accessed
return
"I am "
this
.
name
", working in "
this
.
department
" department"
;
}
}
var
emp
=
new
Employee
(
"Alice"
,
"Engineering Department"
)
;
console.
log
(
emp.
introduce
(
)
)
;
// console.log(emp.name); // Error: protected cannot be accessed from outside
Running result:
我是 Alice,在 技术部 工作
Summary
- **Inheritance:** the extends keyword
- **super:** calls the parent class
- **Polymorphism:** different implementations of the same interface
- **protected:** visible in child classes