From 8da8aada302403cfc86a469837202a6b9d839652 Mon Sep 17 00:00:00 2001 From: Mary2811 Date: Thu, 2 Nov 2023 13:04:10 +0300 Subject: [PATCH] changed class person --- Classes/person.js | 50 +++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/Classes/person.js b/Classes/person.js index a8fa35a..bacc735 100644 --- a/Classes/person.js +++ b/Classes/person.js @@ -1,31 +1,47 @@ class Person { - constructor(name, age) { + constructor(name, age, gender) { + if (typeof name !== 'string' || typeof gender !== 'string' || typeof age !== 'number') { + throw new Error('Invalid argument type'); + } this.name = name; this.age = age; + this.gender = gender; } - sayHello() { - console.log(`Hello, my name is ${this.name}`); + static createMan(name, age) { + return new Person(name, age, 'man'); } - introduce() { - console.log(`I am ${this.age} years old`); + static createWoman(name, age) { + return new Person(name, age, 'woman'); } - celebrateBirthday() { - this.age++; - console.log(`Happy birthday! Now I am ${this.age} years old`); + greet() { + return `Hello, my name is ${this.name}. I am ${this.age} years old and I am a ${this.gender}.`; + } + + isAdult() { + return this.age >= 18; + } + + toString() { + return `Person: ${this.name}, ${this.age}, ${this.gender}`; } } - // Пример использования: - const person1 = new Person('John', 25); - person1.sayHello(); // Hello, my name is John - person1.introduce(); // I am 25 years old - person1.celebrateBirthday(); // Happy birthday! Now I am 26 years old + try { + const person1 = new Person('John', 25, 'man'); + console.log(person1.greet()); // Output: Hello, my name is John. I am 25 years old and I am a man. + console.log(person1.isAdult()); // Output: true + console.log(person1.toString()); // Output: Person: John, 25, man + + const person2 = Person.createWoman('Jane', 17); + console.log(person2.greet()); // Output: Hello, my name is Jane. I am 20 years old and I am a woman. + console.log(person2.isAdult()); // Output: true + console.log(person2.toString()); // Output: Person: Jane, 20, woman - const person2 = new Person('Alice', 30); - person2.sayHello(); // Hello, my name is Alice - person2.introduce(); // I am 30 years old - person2.celebrateBirthday(); // Happy birthday! Now I am 31 years old \ No newline at end of file + const person3 = new Person(123, '30', 'man'); // Invalid argument type + } catch (error) { + console.log(error.message); + }