在 TypeScript 中处理共享属性
Handling shared properties in TypeScript
我有一个 class 系统,如下所示。我想获得共享属性 out/optimized 以实现更具可维护性的数据结构。
我的第一个想法是使用接口,但它们不能包含 TypeScript 中的属性(据我所知)。 - 那么你知道我怎样才能让它变得更好吗?
class Particle {
position: Vector;
}
class Block extends Particle {
height: number;
width: number;
get Left(): number {
return this.position.getX();
}
get Right(): number {
return this.position.getX() + this.width;
}
}
class Sphere extends Particle{
radius: number;
get Left(): number {
return this.position.getX() - this.radius;
}
get Right(): number {
return this.position.getX() + this.radius;
}
}
在 TypeScript 中,您可以使用 classes 作为接口定义。您可以引入具有属性 Left
和 Right
的 class LeftRight
并将其用作 class 定义:
class Block extends Particle implements LeftRight {
/* .... */
}
一个完整的示例如下所示:
class Vector {
getX (): number { return 0; }
}
class Particle {
position: Vector = new Vector();
}
class LeftRight {
get Left(): number {
throw 'Member Left not overriden';
}
get Right(): number {
throw 'Member Right not overriden';
}
}
class Block extends Particle implements LeftRight {
height: number;
width: number;
get Left(): number {
return this.position.getX();
}
get Right(): number {
return this.position.getX() + this.width;
}
}
class Sphere extends Particle implements LeftRight {
radius: number;
get Left(): number {
return this.position.getX() - this.radius;
}
get Right(): number {
return this.position.getX() + this.radius;
}
}
var block = new Block();
block.height = 100;
block.width = 100;
// note that you can safely cast to type LeftRight
var lr: LeftRight = block;
alert('Left: ' + lr.Left + ' Right: ' + lr.Right);
这在 Mixin 示例中的 TypeScript 手册中也有显示:http://www.typescriptlang.org/Handbook#mixins
我有一个 class 系统,如下所示。我想获得共享属性 out/optimized 以实现更具可维护性的数据结构。
我的第一个想法是使用接口,但它们不能包含 TypeScript 中的属性(据我所知)。 - 那么你知道我怎样才能让它变得更好吗?
class Particle {
position: Vector;
}
class Block extends Particle {
height: number;
width: number;
get Left(): number {
return this.position.getX();
}
get Right(): number {
return this.position.getX() + this.width;
}
}
class Sphere extends Particle{
radius: number;
get Left(): number {
return this.position.getX() - this.radius;
}
get Right(): number {
return this.position.getX() + this.radius;
}
}
在 TypeScript 中,您可以使用 classes 作为接口定义。您可以引入具有属性 Left
和 Right
的 class LeftRight
并将其用作 class 定义:
class Block extends Particle implements LeftRight {
/* .... */
}
一个完整的示例如下所示:
class Vector {
getX (): number { return 0; }
}
class Particle {
position: Vector = new Vector();
}
class LeftRight {
get Left(): number {
throw 'Member Left not overriden';
}
get Right(): number {
throw 'Member Right not overriden';
}
}
class Block extends Particle implements LeftRight {
height: number;
width: number;
get Left(): number {
return this.position.getX();
}
get Right(): number {
return this.position.getX() + this.width;
}
}
class Sphere extends Particle implements LeftRight {
radius: number;
get Left(): number {
return this.position.getX() - this.radius;
}
get Right(): number {
return this.position.getX() + this.radius;
}
}
var block = new Block();
block.height = 100;
block.width = 100;
// note that you can safely cast to type LeftRight
var lr: LeftRight = block;
alert('Left: ' + lr.Left + ' Right: ' + lr.Right);
这在 Mixin 示例中的 TypeScript 手册中也有显示:http://www.typescriptlang.org/Handbook#mixins