英文:
multiple class extensions with built-in classes
问题
我一直在研究TypeScript中的混合(mixins),并想知道如何实现我的问题。
我有四个类:
AudioNode(原生)
CustomAudioNode(自定义)
GainNode(原生)
CustomGainNode(自定义)
我想实现以下继承关系:
- 1
CustomAudioNode
继承自AudioNode
。 - 2
GainNode
继承自CustomAudioNode
。 - 3
CustomGainNode
继承自GainNode
或[GainNode, CustomAudioNode]
。
我该如何实现这个继承关系?
英文:
I've been looking at mixins in typescript and wonder how I could implement my problem.
I have four classes:
AudioNode (native)
CustomAudioNode ( custom )
GainNode ( native )
CustomGainNode ( custom )
I would like to achieve this inheritance:
- 1
CustomAudioNode
extendsAudioNode
. - 2
GainNode
extendsCustomAudioNode
. - 3
CustomGainNode
extendsGainNode
or[GainNode,CustomAudioNode]
.
How could I achieve this?
答案1
得分: 1
不可能直接构建像问题中所寻找的那样的关系,其中涉及到本地AudioNode
和本地GainNode
,每个都有另一个自定义实现(CustomAudioNode
和CustomGainNode
),其中...
CustomAudioNode
扩展AudioNode
。GainNode
扩展CustomAudioNode
。CustomGainNode
扩展GainNode
或[GainNode,CustomAudioNode]
。
这是因为...
但可以做的是...
- 提供与
AudioNode
相关的任何附加行为作为混入,最好是作为基于函数的混入实现。
这意味着,现在只提供一个非常干净的代码库,其中只涉及一个CustomGainNode
,它扩展了本地的GainNode
并提供自定义增益节点行为,而自定义音频节点行为被单独实现,但在任何自定义增益节点的构建/实例化时应用。
// 纯自定义音频节点特定功能。
function customAudioNodeBahavior01() {
// `this` 指的是当前的 `CustomGainNode` 实例,
// 也是 `GainNode` 和 `AudioNode` 的实例。
}
function customAudioNodeBahavior02() {
// `this` 指的是当前的 `CustomGainNode` 实例,
// 也是 `GainNode` 和 `AudioNode` 的实例。
}
// 基于函数的混入。
function withCustomAudioNodeBahavior() {
Object.assign(this, {
customAudioNodeBahavior01,
customAudioNodeBahavior02,
});
}
// 自定义增益节点子类/子类型。
class CustomGainNode extends GainNode {
constructor(...args) {
// - `args` 总是 ...
// `context`和`options`
// ... 就像 ...
// ```
// constructor(context, options) {
// super(context, options);
// // ...
// }
// ```
// 继承部分。
super(...args);
// 基于混入的组合部分。
withCustomAudioNodeBahavior.call(this);
// - 实施进一步的自定义增益节点
// 特定行为。
}
}
注意
为了符合Web音频API,问题中的人可能考虑实现BaseAudioContext.createGain
工厂方法的相对应版本...类似于BaseAudioContext.createCustomGain
。
英文:
It is not possible to straightforwardly build a relationship like the one the OP is looking for, which involves both native AudioNode
and native GainNode
, each with another custom implementation (CustomAudioNode
and CustomGainNode
) where ...
> 1. CustomAudioNode
extends AudioNode
.
> 2. GainNode
extends CustomAudioNode
.
> 3. CustomGainNode
extends GainNode
or [GainNode,CustomAudioNode]
.
This is due to ...
GainNode
already extendsAudioNode
.AudioNode
neither can be extended nor can it be wrapped in any meaningful way (thus, it is going to remain untouched).
But what can be done instead is ...
- providing any additional behavior which is related to
AudioNode
as mixin, preferably implemented as function based mixin.
This is, one provides a very clean code base which now involves just a CustomGainNode
which extends the native counterpart and provides custom gain node behavior, whereas custom audio node behavior gets implemented separately, but applied at any custom gain node's construction/instantiation time.
// the pure custom audio node specific functionality.
function customAudioNodeBahavior01() {
// `this` refers to the current `CustomGainNode` instance
// which too is an instance of `GainNode` and `AudioNode`.
}
function customAudioNodeBahavior02() {
// `this` refers to the current `CustomGainNode` instance
// which too is an instance of `GainNode` and `AudioNode`.
}
// the function based mixin.
function withCustomAudioNodeBahavior() {
Object.assign(this, {
customAudioNodeBahavior01,
customAudioNodeBahavior02,
});
}
// the custom gain node subclass/subtype.
class CustomGainNode extends GainNode {
constructor(...args) {
// - `args` are always ...
// `context`and `options`
// ... like with ...
// ```
// constructor(context, options) {
// super(context, options);
// // ...
// }
// ```
// the inheritance part.
super(...args);
// the mixin based composition part.
withCustomAudioNodeBahavior.call(this);
// - implementing further custom gain node
// specific behavior.
}
}
Note
In order to comply with the Web Audio Api, the OP might think about implementing the pendant of the BaseAudioContext.createGain
factory method ... something like BaseAudioContext.createCustomGain
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论