-
Book Overview & Buying
-
Table Of Contents
Full-Stack React, TypeScript, and Node - Second Edition
By :
Polymorphism is a bit of an intimidating name as it’s not immediately clear what it means. However, it’s actually quite powerful because it allows us to maintain type safety in our code while still being able to use different code implementations as needed.Let’s look at some code and see how this works. Create a file called polymorphism.ts and add the following code. This is going to be a fair bit of code, so let’s go through it in pieces:
interface Animal {
name: string;
runMaxMiles(hours: number): number;
}
First, we’ve created an interface that shows an Animal type. This type has a name and a method signature of runMaxMiles. Obviously, since this is an interface, our intention is to implement this interface with specific capabilities, so let’s do that now:
class Wolf implements Animal {
name: string = "";
runMaxMiles(hours: number): number {
return hours * 45;
}
}
class Cheetah implements...
Change the font size
Change margin width
Change background colour