Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Typescript中的非静态方法调用静态方法_Typescript_Static Methods_Template Method Pattern - Fatal编程技术网

从Typescript中的非静态方法调用静态方法

从Typescript中的非静态方法调用静态方法,typescript,static-methods,template-method-pattern,Typescript,Static Methods,Template Method Pattern,我的所有层次结构中都有一些静态内容(在本例中,是\u图像)。我希望能够访问相应的\u图像,而无需重复代码: 这太好了: class Actor { static _image; // I need it to be static method show(){ // I need it to be non-static this.setImage(this.class._image); //doesn't work.... } } class Good

我的所有层次结构中都有一些静态内容(在本例中,是
\u图像
)。我希望能够访问相应的
\u图像
,而无需重复代码:

这太好了:

class Actor {
    static _image; // I need it to be static

    method show(){  // I need it to be non-static
        this.setImage(this.class._image); //doesn't work....
    }
}

class GoodActor extends Actor {
    static _image = 'good.png'
}

class BadActor extends Actor {
    static _image = 'bad.png'
}

class MediumActor extends Actor {
    static _image = 'medium.png'
}
但它不起作用。现在我只需要:

class Actor {
}

class GoodActor extends Actor {
    static _image = 'good.png'  // I need it to be static

    method show(){   // I need it to be non-static
        this.setImage(GoodActor._image);
    }
}

class BadActor extends Actor {
    static _image = 'bad.png'  // I need it to be static

    method show(){  // I need it to be non-static
        this.setImage(BadActor._image);
    }
}

class MediumActor extends Actor {
    static _image = 'medium.png'  // I need it to be static

    method show(){  // I need it to be non-static
        this.setImage(MediumActor._image);
    }
}
假设这四个类有更多的方法。我不想在每个子类中重复
show()
方法。。。但是我需要
show()
方法是非静态的,并且
\u图像
是静态访问的


我读过这个问题,但不幸的是,我不能在那里问,因为他们已经关闭了它,而没有修复它。他们中没有人谈到需要动态解析要调用的静态方法的问题。

更新:为什么不在缓存图像之前构建单个对象?如果使构造变得便宜,那么使用静态字段没有任何好处

例如:

class Actor {
    image: string;

    showImage() {
        console.log(this.image);
    }
}


class GoodActor extends Actor {
    image = 'good.png';
}

class BadActor extends Actor {
    image = 'bad.png';
}

const myActorTypes: (typeof Actor)[] = [GoodActor, BadActor];

function preloadImages() {
    for (let myActorType of myActorTypes) {
        preloadImage(new myActorType().image);
    }
}

function preloadImage(image: string) {
    console.log(`Loading ${image}`);
}

preloadImages();

// "Loading good.png"
// "Loading bad.png"

更新:为什么不在缓存图像之前构建单个对象?如果使构造变得便宜,那么使用静态字段没有任何好处

例如:

class Actor {
    image: string;

    showImage() {
        console.log(this.image);
    }
}


class GoodActor extends Actor {
    image = 'good.png';
}

class BadActor extends Actor {
    image = 'bad.png';
}

const myActorTypes: (typeof Actor)[] = [GoodActor, BadActor];

function preloadImages() {
    for (let myActorType of myActorTypes) {
        preloadImage(new myActorType().image);
    }
}

function preloadImage(image: string) {
    console.log(`Loading ${image}`);
}

preloadImages();

// "Loading good.png"
// "Loading bad.png"
FTR,您可以访问当前对象的类。它被称为
构造函数
,而不是
,您需要声明它,以便它具有比
函数
更有用的类型

class Actor {
    static _image: string; // I need it to be static

    // Keep static members, remove construct signature because
    // subclasses may define constructors with different parameters. 
    "constructor": Pick<typeof Actor, keyof typeof Actor>;

    show(){  // I need it to be non-static
        this.setImage(this.constructor._image);
    }
}

class GoodActor extends Actor {
    static _image = 'good.png'
}

class BadActor extends Actor {
    static _image = 'bad.png'
}

class MediumActor extends Actor {
    static _image = 'medium.png'
}
类参与者{
static _image:string;//我需要它是静态的
//保留静态成员,删除构造签名,因为
//子类可以使用不同的参数定义构造函数。
“构造函数”:选择;
show(){//我需要它是非静态的
this.setImage(this.constructor.\u image);
}
}
类GoodActor扩展了Actor{
静态图像='good.png'
}
类BadActor扩展了BadActor{
静态图像='bad.png'
}
类MediumActor扩展了Actor{
静态图像='medium.png'
}
FTR,您可以访问当前对象的类。它被称为
构造函数
,而不是
,您需要声明它,以便它具有比
函数
更有用的类型

class Actor {
    static _image: string; // I need it to be static

    // Keep static members, remove construct signature because
    // subclasses may define constructors with different parameters. 
    "constructor": Pick<typeof Actor, keyof typeof Actor>;

    show(){  // I need it to be non-static
        this.setImage(this.constructor._image);
    }
}

class GoodActor extends Actor {
    static _image = 'good.png'
}

class BadActor extends Actor {
    static _image = 'bad.png'
}

class MediumActor extends Actor {
    static _image = 'medium.png'
}
类参与者{
static _image:string;//我需要它是静态的
//保留静态成员,删除构造签名,因为
//子类可以使用不同的参数定义构造函数。
“构造函数”:选择;
show(){//我需要它是非静态的
this.setImage(this.constructor.\u image);
}
}
类GoodActor扩展了Actor{
静态图像='good.png'
}
类BadActor扩展了BadActor{
静态图像='bad.png'
}
类MediumActor扩展了Actor{
静态图像='medium.png'
}

为什么需要将其设置为静态?使用静态变量处理变量通常会尖叫“有问题”。嗨!!是的,我知道这有点奇怪。这是一个网络游戏。我需要有演员的形象之前,演员的建设,以便能够预加载他们。接受建议:)将图像放置在与这些图像无关的静态对象中,然后从那里加载它们。为什么您需要将其设置为静态?使用静态变量处理变量通常会尖叫“有问题”。嗨!!是的,我知道这有点奇怪。这是一个网络游戏。我需要有演员的形象之前,演员的建设,以便能够预加载他们。接受建议:)将图像放置在与这些图像无关的静态对象中,然后从那里加载它们。在您所做的新更新中,您正好达到了我要处理的要点。这正是我想要做的:将类放在一个数组中,并对它们进行forEach。问题是,正如你所指出的,这里的建筑成本很高。在您执行新的myActorType()时,参与者就会显示自己。构造函数执行此操作。show()并计算大量内容。在游戏中,你需要演员在不同的时刻。。。你不想一下子就把它们都买走。改变这种设计现在是一项相当大的工作。我正试图找到一些解决办法。现在我已经学会了“不要有昂贵的结构”,为了将来。我会继续,折射代码。这将为你在做任何其他事情时省下很多痛苦(想想:列出演员姓名,任何类型的列表)。有了一个可以进行重构的像样的IDE,我希望这不会太难。我现在选择实例化每个参与者,我将为所需的重构创建一个问题。谢谢!!!你说的编译时间是什么意思?@AluanHaddad编译器编译的时间框架。在你所做的新更新中,你达到了我要处理的确切点。这正是我想要做的:将类放在一个数组中,并对它们进行forEach。问题是,正如你所指出的,这里的建筑成本很高。在您执行新的myActorType()时,参与者就会显示自己。构造函数执行此操作。show()并计算大量内容。在游戏中,你需要演员在不同的时刻。。。你不想一下子就把它们都买走。改变这种设计现在是一项相当大的工作。我正试图找到一些解决办法。现在我已经学会了“不要有昂贵的结构”,为了将来。我会继续,折射代码。这将为你在做任何其他事情时省下很多痛苦(想想:列出演员姓名,任何类型的列表)。有了一个可以进行重构的像样的IDE,我希望这不会太难。我现在选择实例化每个参与者,我将为所需的重构创建一个问题。谢谢!!!编译时间是什么意思?@AluanHaddad编译器编译的时间框架。谢谢!再次学到了一些东西。谢谢你!再次学到了一些东西。