Skip to content

Commit 0121511

Browse files
committed
Instance of example added to 6 lecture. Implementation of checkType is just for educational reasons. Real implementation of something like this would be other.
1 parent df688f2 commit 0121511

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@ elephant => name = elephant 2, age = 10, vaccination = true
7070
=> name = BMW => speed = 220 => price = 4500 => sport = undefined
7171
```
7272

73-
- **6. Inheritance, abstract class, toString example**.
73+
- **6. Inheritance, abstract class, toString, instanceof example**.
7474

7575
**output** :
7676
```shell
77+
It is Ferrari
7778
{ name: 'My Ferrari', sport: true, toString: [Function] }
7879
Vehicle = [ name : My Ferrari , sport : true , speed : 250 , price : 5600]
80+
It is Ford
7981
{ name: 'My Ford', sport: false, toString: [Function] }
8082
Vehicle = [ name : My Ford , sport : false , speed : 190 , price : 3000]
8183
```

build/6-inheritance/main.js

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/6-inheritance/main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dev/6-inheritance/main.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
// instance of example
2+
function checkType(vehicle : Vehicle) {
3+
if (vehicle instanceof Ford) {
4+
return "It is Ford"
5+
} else if (vehicle instanceof Ferrari) {
6+
return "It is Ferrari"
7+
} else {
8+
return "It is unknown"
9+
}
10+
}
11+
112
// example of abstract class
213
abstract class Vehicle {
314

@@ -55,8 +66,10 @@ class Ford extends Vehicle {
5566

5667
// executable
5768
let vehicle : Vehicle = new Ferrari()
69+
console.log(checkType(vehicle))
5870
console.log(vehicle)
5971
console.log(vehicle.toString())
6072
vehicle = new Ford()
73+
console.log(checkType(vehicle))
6174
console.log(vehicle)
6275
console.log(vehicle.toString())

0 commit comments

Comments
 (0)