-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
completed classes and inheritance topic
- Loading branch information
Showing
5 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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. |
This file contains 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
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. | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
"use strict"; | ||
var Color; | ||
(function (Color) { | ||
|
This file contains 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
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 |
This file contains 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
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 |