Reputation: 199
I have a class Moviecharacter which represents json data about movie characters aswell as an actor class which holds information about actors. I want to add the Actor class into my Moviecharacter class without using duplicated attributes:
My versions for Actor and MovieCharacter are:
// actor class stores general information about an actor in a json file
export class Actor{
name: string;
id: number;
}
// duplicated attributes name and id:
export class MovieCharacter {
name: string;
id: number;
character: string;
}
But i want something like this:
// without duplicated Attributes:
export class MovieCharacter {
Actor(); // not working
character: string;
}
Any help would be appreciated.
Upvotes: 1
Views: 55
Reputation: 276269
// without duplicated Attributes:
Suspect you are looking for inheritance:
// actor class stores general information about an actor in a json file
export class Actor {
name: string;
id: number;
}
// Inherits name and id from Actor
export class MovieCharacter extends Actor {
character: string;
}
Some helpful docs : https://basarat.gitbooks.io/typescript/content/docs/classes.html
Upvotes: 2
Reputation: 316
You firstly need to specify the member as "name : type"
If you wanted to create a new instance of the class on construction you can do the following..
class MovieCharacter {
actor: Actor;
constructor() {
this.actor = new Actor();
}
}
Upvotes: 1