From 9aa009f7b888c1f3669f1fbb5fc0e0d3fd6cb51f Mon Sep 17 00:00:00 2001 From: __HoLeN__ Date: Sat, 7 Dec 2024 12:09:36 +0300 Subject: [PATCH] severe polymorphism --- rpgsaga/saga/src/Customer.ts | 10 +++++++ rpgsaga/saga/src/Employee.ts | 46 ++++++++++++++++------------- rpgsaga/saga/src/Person.ts | 37 +++++++++++++++++++++++ rpgsaga/saga/src/index.ts | 35 ++++++++++++++++------ rpgsaga/saga/tests/customer.test.ts | 17 +++++++++++ 5 files changed, 115 insertions(+), 30 deletions(-) create mode 100644 rpgsaga/saga/src/Customer.ts create mode 100644 rpgsaga/saga/src/Person.ts create mode 100644 rpgsaga/saga/tests/customer.test.ts diff --git a/rpgsaga/saga/src/Customer.ts b/rpgsaga/saga/src/Customer.ts new file mode 100644 index 00000000..53f3066d --- /dev/null +++ b/rpgsaga/saga/src/Customer.ts @@ -0,0 +1,10 @@ +import { Person } from "./Person"; + +export class Customer extends Person { + toString() { + return 'У Customer toString() переопределён'; + } + come(): void { + console.log(`${this.name} пришёл домой`); + } +} \ No newline at end of file diff --git a/rpgsaga/saga/src/Employee.ts b/rpgsaga/saga/src/Employee.ts index 965240dc..a6184033 100644 --- a/rpgsaga/saga/src/Employee.ts +++ b/rpgsaga/saga/src/Employee.ts @@ -1,29 +1,15 @@ -export class Employee { - private _name: string; - private _age: number; +import { Person } from "./Person"; + +export class Employee extends Person { private _salary: number; + private _yearsOfWorkExperience: number = 0; - constructor(name: string, age: number, salary: number = 15000) { - this.name = name; - this.age = age; + constructor(name: string, age: number, salary: number = 15000, isSick: boolean = false) { + super(name, age, isSick); this.salary = salary; } - public get name(): string { - return this._name; - } - public set name(name: string) { - this._name = name; - } - public get age(): number { - return this._age; - } - public set age(age: number) { - if (age < 0) { - throw new Error('Age cannot be negative.'); - } - this._age = age; - } + public get salary(): number { return this._salary; } @@ -33,4 +19,22 @@ export class Employee { } this._salary = salary; } + + come() { + console.log(`${this.name} пришёл на работу`); + } + toString(): void { + console.log('У Employee toString() переопределён') + } + + + addYearsOfWorkExperience(years: number): void { + if (years < 0) { + throw new Error('You cannot add negative numbers'); + } + this._yearsOfWorkExperience+=years; + } + getYearsOfWorkExperience(): number { + return this._yearsOfWorkExperience; + } } diff --git a/rpgsaga/saga/src/Person.ts b/rpgsaga/saga/src/Person.ts new file mode 100644 index 00000000..92e7206f --- /dev/null +++ b/rpgsaga/saga/src/Person.ts @@ -0,0 +1,37 @@ +export abstract class Person { + private _name: string; + private _age: number; + private _sick: boolean; + + constructor(name: string, age: number, isSick: boolean = false) { + this._name = name; + this.age = age; + this._sick = isSick; + } + + abstract come(): void; + abstract toString(): void; + + public get sick(): boolean { + return this._sick; + } + public set sick(isSick: boolean) { + this._sick = isSick; + } + + public get name(): string { + return this._name; + } + public set name(name: string) { + this._name = name; + } + public get age(): number { + return this._age; + } + public set age(age: number) { + if (age < 0) { + throw new Error('Age cannot be negative.'); + } + this._age = age; + } +} \ No newline at end of file diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index 05e44df5..5e8106e5 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -1,4 +1,6 @@ +import { Customer } from './Customer'; import { Employee } from './Employee'; +import { Person } from './Person'; const func = (x: number, a: number = 4.1, b: number = 2.7): number => (b * Math.sqrt(x) - a * log(x, 5)) / log(Math.abs(x - 1), 10); @@ -26,18 +28,33 @@ const taskB = (xs: number[]): number[] => { export { func, log, taskA, taskB }; -const a: Employee = new Employee('Ivan', 17); -const b: Employee = new Employee('Vlad', 18); -const c: Employee = new Employee('Mikhail', 17, 20000); +const employee1: Employee = new Employee('Ivan', 17); +const employee2: Employee = new Employee('Vlad', 18, 16000, true); +const employee3: Employee = new Employee('Mikhail', 17, 20000); -console.log(a); -console.log(b); -console.log(c); -b.age = 10; -console.log(b); +const customer1: Customer = new Customer('test', 123); + +let array: Person[] = []; + +array.push(employee1); +array.push(employee2); +array.push(customer1); +for (const a of array) { + a.come(); +} + + +console.log(employee1); +console.log(employee2); +console.log(employee3); +employee2.age = 10; +console.log(employee2); + +customer1.toString(); +employee3.toString(); try { - c.age = -10; + employee3.age = -10; } catch (e) { console.log(e.message); } diff --git a/rpgsaga/saga/tests/customer.test.ts b/rpgsaga/saga/tests/customer.test.ts new file mode 100644 index 00000000..96ed5353 --- /dev/null +++ b/rpgsaga/saga/tests/customer.test.ts @@ -0,0 +1,17 @@ +import { Customer } from "../src/Customer"; + +describe('test log', () => { + it('should return true', () => { + const customer1 = new Customer('Roman', 32); + expect(customer1.toString()).toEqual('У Customer toString() переопределён'); + }) + it('should return true', () => { + const customer1 = new Customer('Artem', 50); + expect(customer1.name).toEqual('Artem'); + }) + it('should return true', () => { + const customer1 = new Customer('Andrey', 32); + customer1.age = 40; + expect(customer1.age).toEqual(40); + }) +}); \ No newline at end of file