Typescript - Human Inheritance
Typescript - Restaurant Class Create a class "Restaurant" that has
⚫ one public member "menu" to store today's menu list,
⚫ a constructor to initialize it, and
⚫ function "list()" that display today's menu list. In list() function, log the menu.
Sample Output: ['dosa', 'idly', 'chat']
Note: Input array is passed in the constructor of the class Restaurant
class Restaurant {
public menu: string[];
constructor(menu: string[]) {
this.menu = menu;
}
list(): void {
console.log(this.menu);
}
}
// Example usage:
const todayMenu = ['dosa', 'idly', 'chat'];
const restaurant = new Restaurant(todayMenu);
// Calling the list method to display today's menu
restaurant.list();
// Example usage:
const todayMenu = ['dosa', 'idly', 'chat'];
const restaurant = new Restaurant(todayMenu);
// Calling the list method to display today's menu
restaurant.list();
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.