Class Methods
1234567891011121314151617181920212223242526class Calculator { add(a: number, b: number): number { return a + b; } subtract(a: number, b: number): number { return a - b; } multiply(a: number, b: number): number { return a * b; } divide(a: number, b: number): number { if (b === 0) { throw new Error("Cannot divide by zero."); } return a / b; } } const calc = new Calculator(); console.log(calc.add(5, 3)); // 8 console.log(calc.subtract(10, 4)); // 6 console.log(calc.multiply(2, 7)); // 14 console.log(calc.divide(20, 5)); // 4
When you define a function inside a TypeScript class, that function is called a method. Methods are used to perform actions or calculations related to the class's purpose. You can declare methods by writing the method name, followed by parentheses and an optional parameter list, a colon, and the return type. The method body is enclosed in curly braces.
To call a method, create an instance of the class and use dot notation, such as instance.methodName(). Methods can return any type, including number, string, boolean, or even custom types. If a method does not explicitly return a value, its return type is void. Specifying the return type helps TypeScript check that your method returns the correct kind of value.
In the code above, the Calculator class defines four methods: add, subtract, multiply, and divide. Each method takes two numbers as parameters and returns a number. The divide method also checks for division by zero and throws an error if needed. When you create a new Calculator instance, you can call these methods to perform calculations.
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
Class Methods
Swipe um das Menü anzuzeigen
1234567891011121314151617181920212223242526class Calculator { add(a: number, b: number): number { return a + b; } subtract(a: number, b: number): number { return a - b; } multiply(a: number, b: number): number { return a * b; } divide(a: number, b: number): number { if (b === 0) { throw new Error("Cannot divide by zero."); } return a / b; } } const calc = new Calculator(); console.log(calc.add(5, 3)); // 8 console.log(calc.subtract(10, 4)); // 6 console.log(calc.multiply(2, 7)); // 14 console.log(calc.divide(20, 5)); // 4
When you define a function inside a TypeScript class, that function is called a method. Methods are used to perform actions or calculations related to the class's purpose. You can declare methods by writing the method name, followed by parentheses and an optional parameter list, a colon, and the return type. The method body is enclosed in curly braces.
To call a method, create an instance of the class and use dot notation, such as instance.methodName(). Methods can return any type, including number, string, boolean, or even custom types. If a method does not explicitly return a value, its return type is void. Specifying the return type helps TypeScript check that your method returns the correct kind of value.
In the code above, the Calculator class defines four methods: add, subtract, multiply, and divide. Each method takes two numbers as parameters and returns a number. The divide method also checks for division by zero and throws an error if needed. When you create a new Calculator instance, you can call these methods to perform calculations.
Danke für Ihr Feedback!