06-classes.ts
Typescript/src/01-beginner/06-classes.ts
// ============================================
// 06. Classes
// ============================================
// Basic Class
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
greet(): string {
return `Hello, I'm ${this.name} and I'm ${this.age} years old.`;
}
}
const person = new Person("Alice", 25);
console.log(person.greet());
// Access Modifiers
class BankAccount {
private balance: number = 0;
deposit(amount: number): void {
this.balance += amount;
}
getBalance(): number {
return this.balance;
}
}
const account = new BankAccount();
account.deposit(100);
// account.balance; // Error: Property 'balance' is private
console.log(account.getBalance()); // OK
// Protected
class Animal {
protected name: string;
constructor(name: string) {
this.name = name;
}
}
class Dog extends Animal {
bark(): void {
console.log(`${this.name} barks!`); // OK, name is protected
}
}
const dog = new Dog("Buddy");
// dog.name; // Error: Property 'name' is protected
dog.bark();
// Readonly
class PersonReadonly {
readonly id: number;
name: string;
constructor(id: number, name: string) {
this.id = id; // OK, setting during initialization
this.name = name;
}
}
const person2 = new PersonReadonly(1, "Alice");
// person2.id = 2; // Error: Cannot assign to 'id' because it is a read-only property
// Parameter Properties
class PersonParam {
constructor(public name: string, public age: number) {
// Automatically creates and assigns this.name and this.age
}
constructor(public name: string, public age: number, public email: string ) {
// Automatically creates and assigns this.name and this.age and this.email
}
}
const person3 = new PersonParam("Alice", 25);
console.log(person3.name, person3.age, person3.email);
// Getters and Setters
class Temperature {
private _celsius: number = 0;
get celsius(): number {
return this._celsius;
}
set celsius(value: number) {
if (value < -273.15) {
throw new Error("Temperature cannot be below absolute zero");
}
this._celsius = value;
}
get fahrenheit(): number {
return this._celsius * 9/5 + 32;
}
set fahrenheit(value: number) {
this.celsius = (value - 32) * 5/9;
}
}
const temp = new Temperature();
temp.celsius = 25;
console.log(temp.fahrenheit); // 77
// Inheritance
class AnimalBase {
name: string;
constructor(name: string) {
this.name = name;
}
move(distance: number = 0): void {
console.log(`${this.name} moved ${distance}m.`);
}
}
class DogInherit extends AnimalBase {
breed: string;
constructor(name: string, breed: string) {
super(name); // Call parent constructor
this.breed = breed;
}
bark(): void {
console.log("Woof! Woof!");
}
}
const dog2 = new DogInherit("Buddy", "Golden Retriever");
dog2.move(10); // Inherited method
dog2.bark(); // Dog-specific method
// Abstract Classes
abstract class AnimalAbstract {
name: string;
constructor(name: string) {
this.name = name;
}
abstract makeSound(): void; // Must be implemented by subclasses
move(): void {
console.log(`${this.name} is moving.`);
}
}
class DogAbstract extends AnimalAbstract {
makeSound(): void {
console.log("Woof!");
}
}
// const animal = new AnimalAbstract("Generic"); // Error: Cannot create instance of abstract class
const dog3 = new DogAbstract("Buddy");
dog3.makeSound();
// Static Members
class MathHelper {
static PI: number = 3.14159;
static add(a: number, b: number): number {
return a + b;
}
}
console.log(MathHelper.PI); // 3.14159
console.log(MathHelper.add(5, 3)); // 8
// Implementing Interfaces
interface Flyable {
fly(): void;
}
interface Swimmable {
swim(): void;
}
class DuckClass implements Flyable, Swimmable {
fly(): void {
console.log("Flying...");
}
swim(): void {
console.log("Swimming...");
}
}
export { Person, BankAccount, Temperature, DogInherit, MathHelper };
Articles liés
01-introduction.ts
01-introduction.ts — typescript source code from the Typescript learning materials (Typescript/src/01-beginner/01-introduction.ts).
Lire l'article →02-basic-types.ts
02-basic-types.ts — typescript source code from the Typescript learning materials (Typescript/src/01-beginner/02-basic-types.ts).
Lire l'article →03-variables.ts
03-variables.ts — typescript source code from the Typescript learning materials (Typescript/src/01-beginner/03-variables.ts).
Lire l'article →04-functions.ts
04-functions.ts — typescript source code from the Typescript learning materials (Typescript/src/01-beginner/04-functions.ts).
Lire l'article →05-interfaces.ts
05-interfaces.ts — typescript source code from the Typescript learning materials (Typescript/src/01-beginner/05-interfaces.ts).
Lire l'article →07-arrays-tuples.ts
07-arrays-tuples.ts — typescript source code from the Typescript learning materials (Typescript/src/01-beginner/07-arrays-tuples.ts).
Lire l'article →