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.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Can you explain how inheritance works with classes in TypeScript?
What are access modifiers in TypeScript classes and how do they work?
Can you show how to add more methods or properties to the Person class?
Awesome!
Completion rate improved to 5
Defining and Instantiating Classes
Sveip for å vise menyen
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.
Takk for tilbakemeldingene dine!