Skip to content

Commit

Permalink
completed classes and inheritance topic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahsan2001 committed Aug 12, 2023
1 parent 7e2e936 commit b796960
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Classes/index.js
Original file line number Diff line number Diff line change
@@ -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.
26 changes: 26 additions & 0 deletions Classes/index.ts
Original file line number Diff line number Diff line change
@@ -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.


1 change: 1 addition & 0 deletions Enums/dist/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

"use strict";
var Color;
(function (Color) {
Expand Down
41 changes: 41 additions & 0 deletions Inheritance/index.js
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions Inheritance/index.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b796960

Please sign in to comment.