Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Type {
- _kind: string;
- _breed: string;
- constructor(typeObj: any) {
- this._kind = typeObj.kind;
- this._breed = typeObj.breed;
- }
- }
- class Pet {
- private _name: string;
- private _age: number;
- private _type: Type;
- private _mealCount: number;
- constructor(petObj: any) {
- this._name = petObj.name;
- this._age = petObj.age;
- this._type = new Type(petObj.extra.type);
- this._mealCount = petObj.extra.meals.length;
- }
- getDescription = (): string =>
- `My ${this._name} is a ${this._type._breed} ${this._type._kind}.\n` +
- `She is ${this._age} year(s) old.\n` +
- `She takes ${this._mealCount} meal(s) everyday.`;
- }
- const pet: Pet = new Pet(JSON.parse(
- '{"name":"Fido","age":5,"extra":{"id":1234,"type":{"kind":"Cat","breed":"Parsian"},"meals":["Cat Food","Tuna Can","Boiled Chicken"]}}'
- ));
- console.log(pet.getDescription());
Advertisement
Add Comment
Please, Sign In to add comment