打字稿:类中的类

时间:2016-09-06 16:13:04

标签: typescript

我有一个类Moviecharacter,它代表关于电影角色的json数据,以及一个保存有关actor的信息的actor类。 我想将Actor类添加到我的Moviecharacter类中,而不使用重复的属性:

我对Actor和MovieCharacter的版本是:

// 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;
}

但我想要这样的事情:

 // without duplicated Attributes:
export class MovieCharacter {
    Actor(); // not working
    character: string;
}

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:2)

  

//没有重复的属性:

怀疑您正在寻找继承

<ImageView
    android:layout_height="200dp"
    android:layout_width="wrap_content"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:src="@mipmap/ic_launcher"
    android:layout_below="@+id/title"
    android:layout_margin="5dip"
    android:id="@+id/imageView1">

更多

一些有用的文档:https://basarat.gitbooks.io/typescript/content/docs/classes.html

答案 1 :(得分:1)

首先需要将成员指定为“name:type”

如果您想在构建时创建类的新实例,可以执行以下操作。

 class MovieCharacter {
    actor: Actor;

    constructor() {
        this.actor = new Actor();
    }
}
相关问题