Defining and Instantiating Classes
Understanding how to define and use classes is central to mastering object-oriented programming in TypeScript. Classes offer a structured way to model real-world entities and behaviors, acting as blueprints for creating objects with shared properties and methods. By using classes, you can organize your code more effectively, promote reuse, and clearly express relationships between different parts of your application.
12345678910111213141516class Person { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } greet() { console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`); } } const alice = new Person("Alice", 30); alice.greet(); // Output: Hello, my name is Alice and I'm 30 years old.
To define a class in TypeScript, start with the class keyword followed by the class name. Inside the class body, you can declare properties, such as name and age above, which hold data for each object created from the class. The constructor is a special method that runs when you create a new instance of the class. It allows you to initialize the object's properties using parameters. Within the constructor, you assign values to the properties using this.propertyName = value.
To create an object from a class, use the new keyword followed by the class name and any required arguments. In the example, const alice = new Person("Alice", 30); creates a new Person object with the name "Alice" and age 30. You can then call methods on the instance, such as alice.greet(), to perform actions or retrieve information.
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Awesome!
Completion rate improved to 5
Defining and Instantiating Classes
Swipe um das Menü anzuzeigen
Understanding how to define and use classes is central to mastering object-oriented programming in TypeScript. Classes offer a structured way to model real-world entities and behaviors, acting as blueprints for creating objects with shared properties and methods. By using classes, you can organize your code more effectively, promote reuse, and clearly express relationships between different parts of your application.
12345678910111213141516class Person { name: string; age: number; constructor(name: string, age: number) { this.name = name; this.age = age; } greet() { console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`); } } const alice = new Person("Alice", 30); alice.greet(); // Output: Hello, my name is Alice and I'm 30 years old.
To define a class in TypeScript, start with the class keyword followed by the class name. Inside the class body, you can declare properties, such as name and age above, which hold data for each object created from the class. The constructor is a special method that runs when you create a new instance of the class. It allows you to initialize the object's properties using parameters. Within the constructor, you assign values to the properties using this.propertyName = value.
To create an object from a class, use the new keyword followed by the class name and any required arguments. In the example, const alice = new Person("Alice", 30); creates a new Person object with the name "Alice" and age 30. You can then call methods on the instance, such as alice.greet(), to perform actions or retrieve information.
Danke für Ihr Feedback!