Last active
April 3, 2023 12:10
-
-
Save EteimZ/f30bfd364e103e591e10b71793a332d6 to your computer and use it in GitHub Desktop.
A simple example demonstrating shared behaivor using abstract base class and interface in Typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| abstract class Summarize { | |
| abstract summarize(): string; | |
| } | |
| class NewsArticle extends Summarize { | |
| headline: string; | |
| place: string; | |
| author: string; | |
| content: string; | |
| constructor( headline: string, place: string, author: string, content: string){ | |
| super(); | |
| this.headline = headline; | |
| this.place = place; | |
| this.author = author; | |
| this.content = content; | |
| } | |
| summarize(): string{ | |
| return `${this.headline}, by ${this.author} (${this.place})` | |
| } | |
| } | |
| class Tweet extends Summarize { | |
| username: string; | |
| content: string; | |
| reply: boolean; | |
| retweet: boolean; | |
| constructor( username: string, content: string, reply: boolean, retweet: boolean){ | |
| super(); | |
| this.username = username; | |
| this.content = content; | |
| this.reply = reply; | |
| this.retweet = retweet; | |
| } | |
| summarize() :string { | |
| return `${this.username}: ${this.content}` | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function main(): void { | |
| const news = new NewsArticle("The AI invasion has begun.", | |
| "Abuja", | |
| "John Doe", | |
| "The AI invasion began with the release of a transformer.") | |
| const tweet = new Tweet("John Doe", "Read my latest news article.", false, false); | |
| console.log(`Today's news: ${news.summarize()}`); | |
| console.log(`1 new tweet: ${tweet.summarize()}`); | |
| } | |
| main() | |
| /* Output: | |
| Today's news: The AI invasion has begun., by John Doe (Abuja) | |
| 1 new tweet: John Doe: Read my latest news article. | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface Summarize { | |
| summarize(): string; | |
| } | |
| class NewsArticle implements Summarize { | |
| headline: string; | |
| place: string; | |
| author: string; | |
| content: string; | |
| constructor( headline: string, place: string, author: string, content: string){ | |
| this.headline = headline; | |
| this.place = place; | |
| this.author = author; | |
| this.content = content; | |
| } | |
| summarize(): string{ | |
| return `${this.headline}, by ${this.author} (${this.place})` | |
| } | |
| } | |
| class Tweet implements Summarize { | |
| username: string; | |
| content: string; | |
| reply: boolean; | |
| retweet: boolean; | |
| constructor( username: string, content: string, reply: boolean, retweet: boolean){ | |
| this.username = username; | |
| this.content = content; | |
| this.reply = reply; | |
| this.retweet = retweet; | |
| } | |
| summarize() :string { | |
| return `${this.username}: ${this.content}` | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment