forked from ISUCT/TProgramming_2024
-
Notifications
You must be signed in to change notification settings - Fork 0
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
4 changed files
with
74 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Rabbit } from './RabbitClass'; | ||
|
||
const rabbitOne = new Rabbit('Vitalik',3,'Brown','Male') | ||
console.log(rabbitOne) | ||
const rabbitTwo = new Rabbit('LuiVitonovich',4,'Black','Female') | ||
console.log(rabbitTwo) | ||
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
export class Rabbit { | ||
private _name: string; | ||
private _age: number; | ||
private _color: string; | ||
private _gender: string; | ||
constructor(name: string, age: number, gender: string, color: string) { | ||
this._name = name; | ||
this._age = age; | ||
this._color = color; | ||
this._gender = gender; | ||
} | ||
|
||
public set age(age: number) { | ||
if (age < 0) { | ||
throw new Error('Invalid age'); | ||
} | ||
this._age = age; | ||
} | ||
public set name(name: string) { | ||
this._name = name; | ||
} | ||
public set color(color: string) { | ||
this.color = color; | ||
} | ||
public set gender(gender: string) { | ||
if (gender.toLowerCase() == 'male'){ | ||
this._gender = 'Male'; | ||
} else if (gender.toLowerCase() == 'female'){ | ||
this._gender = 'Female' | ||
} else { | ||
throw new Error('Invalid gender') | ||
} | ||
} | ||
public get age() { | ||
return this._age; | ||
} | ||
public get name() { | ||
return this._name; | ||
} | ||
public get gender() { | ||
return this._gender; | ||
} | ||
public get color() { | ||
return this._color; | ||
} | ||
} |
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