-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
33 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
const person3 = new Person(123, '30', 'man'); // Invalid argument type | ||
} catch (error) { | ||
console.log(error.message); | ||
} |