From b7969608a75671267de2e83aacc46ba20c272b71 Mon Sep 17 00:00:00 2001 From: Ahsan2001 Date: Fri, 11 Aug 2023 17:43:21 -0700 Subject: [PATCH] completed classes and inheritance topic --- Classes/index.js | 19 +++++++++++++++++++ Classes/index.ts | 26 ++++++++++++++++++++++++++ Enums/dist/index.js | 1 + Inheritance/index.js | 41 +++++++++++++++++++++++++++++++++++++++++ Inheritance/index.ts | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+) create mode 100644 Classes/index.js create mode 100644 Classes/index.ts create mode 100644 Inheritance/index.js create mode 100644 Inheritance/index.ts diff --git a/Classes/index.js b/Classes/index.js new file mode 100644 index 0000000..cf84f42 --- /dev/null +++ b/Classes/index.js @@ -0,0 +1,19 @@ +var Animal = /** @class */ (function () { + // Constructor + function Animal(name) { + // this.name is the private property we define in top + // name come from parameter + this.name = name; + } + // Method + Animal.prototype.makeSound = function (sound) { + console.log("".concat(this.name, " makes a ").concat(sound, " sound.")); + }; + return Animal; +}()); +// Create instances of the class +var cat = new Animal("Whiskers"); +var dog = new Animal("Buddy"); +// Use class methods +cat.makeSound("Meow"); // Output: Whiskers makes a Meow sound. +dog.makeSound("Woof"); // Output: Buddy makes a Woof sound. diff --git a/Classes/index.ts b/Classes/index.ts new file mode 100644 index 0000000..bcf39bc --- /dev/null +++ b/Classes/index.ts @@ -0,0 +1,26 @@ +class Animal { + // Properties + private name: string; + + // Constructor + constructor(name: string) { + // this.name is the private property we define in top + // name come from parameter + this.name = name; + } + + // Method + makeSound(sound: string): void { + console.log(`${this.name} makes a ${sound} sound.`); + } +} + +// Create instances of the class +const cat = new Animal("Whiskers"); +const dog = new Animal("Buddy"); + +// Use class methods +cat.makeSound("Meow"); // Output: Whiskers makes a Meow sound. +dog.makeSound("Woof"); // Output: Buddy makes a Woof sound. + + diff --git a/Enums/dist/index.js b/Enums/dist/index.js index 2023df2..25eb52e 100644 --- a/Enums/dist/index.js +++ b/Enums/dist/index.js @@ -1,3 +1,4 @@ + "use strict"; var Color; (function (Color) { diff --git a/Inheritance/index.js b/Inheritance/index.js new file mode 100644 index 0000000..df3273c --- /dev/null +++ b/Inheritance/index.js @@ -0,0 +1,41 @@ +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var Name = /** @class */ (function () { + function Name(name) { + this.name = name; + } + Name.prototype.getName = function () { + console.log("My name is ".concat(this.name)); + }; + return Name; +}()); +var Fullname = /** @class */ (function (_super) { + __extends(Fullname, _super); + function Fullname(name, lastname) { + var _this = _super.call(this, name) || this; + _this.lastname = lastname; + return _this; + } + Fullname.prototype.getFullname = function () { + console.log("My full name is ".concat(this.name, " ").concat(this.lastname)); + }; + return Fullname; +}(Name)); +// Create instances +var person = new Fullname("John", "Doe"); +// Use methods +person.getName(); // Output: My name is John +person.getFullname(); // Output: My full name is John Doe diff --git a/Inheritance/index.ts b/Inheritance/index.ts new file mode 100644 index 0000000..a352fdb --- /dev/null +++ b/Inheritance/index.ts @@ -0,0 +1,34 @@ +class Name { + protected name: string; + + constructor(name: string) { + this.name = name; + } + + getName() { + console.log(`My name is ${this.name}`); + } + +} + +class Fullname extends Name { + private lastname: string; + + constructor(name: string, lastname: string) { + super(name); // Call the constructor of the parent class + this.lastname = lastname; + } + + getFullname() { + console.log(`My full name is ${this.name} ${this.lastname}`); + } +} + + +// Create instances +const person = new Fullname("John", "Doe"); + + +// Use methods +person.getName(); // Output: My name is John +person.getFullname(); // Output: My full name is John Doe