Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.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 - Fatal编程技术网

Typescript 我可以在实例方法中引用子类的类属性吗?

Typescript 我可以在实例方法中引用子类的类属性吗?,typescript,Typescript,我有一个超类a,它有一个class属性templateUrl 以及利用class属性的实例方法f: class A { public static templateUrl: string = "default.html"; public f() { console.log(A.templateUrl); } } 在fso的主体中,有没有一种方法可以说self而不是a 子类将访问被覆盖的templateUrl吗 更具体地说,当我将这个类扩展到 特定类别 c

我有一个超类
a
,它有一个class属性
templateUrl
以及利用class属性的实例方法
f

class A {
    public static templateUrl: string = "default.html";
    public f() {
        console.log(A.templateUrl);
    }
}
f
so的主体中,有没有一种方法可以说
self
而不是
a
子类将访问被覆盖的templateUrl吗

更具体地说,当我将这个类扩展到 特定类别

class B extends A {
}

class C extends A {
    public static templateUrl: string = "projectX.html";
}
。。。然后去:

new A().f()
new B().f()
new C().f()
。。。我将看到:

default.html
default.html
default.html
。。。但我想看到的是:

default.html
default.html
projectX.html

有办法吗?或者我只是使用了完全错误的模式?

好吧,如果子类的每个实例都想重新定义
templateUrl
的值,那么很明显,您想要的是一个实例字段成员,而不是您当前拥有的静态字段成员

因此,最简单的解决方案可能是:

class A {

    constructor(public templateUrl="default.html"){
    }

    public f() {
        console.log(this.templateUrl);
    }
}

class B extends A {}

class C extends A {
    constructor(){
        super("projectX.html");
    }
}


new A().f();
new B().f();
new C().f();
这会产生你想要的:

default.html
default.html
projectX.html
--编辑--

关于您对共享同一字段值的评论

var url = 'index.html'
var a = new A(url);
var b = new B(url);
var c = new B(url);

它们不是共享相同的值吗?

利用JavaScript中构造函数的性质,您可以执行如下操作,从而避免在对象的每个实例中存储值:

class A {
    public static templateUrl = "default.html";
    public f() {
        /* 
         the static value is stored on the constructor
         for the class, so we can grab it by 
         casting away the specific type and
         accessing the property directly
         */
        return (<any>this).constructor.templateUrl;
    }
}

class B extends A { }

class C extends A {
    public static templateUrl = "projectX.html";    
}

console.log(new A().f()); // <==default.html 
console.log(new B().f()); // <==default.html
console.log(new C().f()); // <==projectX.html
A类{
publicstatictemplateurl=“default.html”;
公共f(){
/* 
静态值存储在构造函数中
为了上课,所以我们可以抓住它
丢弃特定类型和
直接访问属性
*/
返回(this).constructor.templateUrl;
}
}
类B扩展了{}
C类扩展了{
公共静态模板url=“projectX.html”;
}

console.log(新的A().f());//显然,您的
templateUrl
应该是一个实例属性,而不是一个静态属性。见下文。整洁!现在假设templateUrl的类型发生了变化(假设它是一个对象,并且在一个子类中有新的属性)。如果能得到类似于
typeof(this.constructor.tmeplateUrl
(typescript
typeof
,而不是动态的)的东西,那就太好了,但我想这是行不通的,因为this.constructor的具体类型只有在运行时才知道?您可以获取存储在那里的任何内容,然后执行类型断言,或者使用
instanceof
检查存储的内容,然后再对其进行操作。虽然这对本例有效,但这只是有时您想要的。如果要更改同一子类的两个不同实例的属性,则需要将其作为实例属性。但是我希望templateUrl对于同一子类的每个实例都是相同的,所以我更喜欢WiredPairie的答案。@user2645074我想说,您原来的问题是,您问的是您认为是问题的答案(如何在子类中使用静态字段),而不是问真正的问题(如何在子类的实例之间共享状态).从软件设计的角度来看,您的字段必须是实例字段。我不理解您的评论,在我的设计中,您的所有子类仍然可以共享相同的URL实例。您可以在我证明这一点的地方看到我的编辑。我想您只需要更好地理解继承和对象组合的概念。我nstance变量和类变量非常简单,两者都有明显的应用(否则就不会有任何类变量):类变量在所有实例上共享相同的值,而实例变量却不共享。我不明白你为什么坚持谈论实例变量,因为我的问题显然是关于类变量的。而且我也不认为有必要侮辱。