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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.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_Oop - Fatal编程技术网

Typescript 将一个大抽象类拆分为多个抽象类+;继承

Typescript 将一个大抽象类拆分为多个抽象类+;继承,typescript,oop,Typescript,Oop,基本上,我有一个主要的基本抽象类,它当前实现的方法需要划分为不同的类(getesta1,getesta2需要在一个类上,getTestB1,getTestB2需要在另一个类上,等等) ChildClass将需要在内部提供所有上述方法 我目前的结构如下: 导出抽象类AbstractBase{ //我是抽象的,我需要我的孩子来实现 受保护的抽象httpRequest:TestType; //我将在内部调用我的抽象方法 公共请求=(…某些参数)=>{ //我在调用我的抽象方法 返回此.httpRequ

基本上,我有一个主要的基本抽象类,它当前实现的方法需要划分为不同的类(
getesta1
getesta2
需要在一个类上,
getTestB1
getTestB2
需要在另一个类上,等等)

ChildClass
将需要在内部提供所有上述方法

我目前的结构如下:

导出抽象类AbstractBase{
//我是抽象的,我需要我的孩子来实现
受保护的抽象httpRequest:TestType;
//我将在内部调用我的抽象方法
公共请求=(…某些参数)=>{
//我在调用我的抽象方法
返回此.httpRequest({
方法,,
网址,
body:requestContext.body作为字符串,
})。然后((响应)=>{
responseContext.resolve(响应);
});
}
//以下方法都在内部调用this.request
公共getesta1=函数(){
...
此.request(…args);
}
公共getesta2=函数(){
...
此.request(…args);
}
public getTestB1=函数(){
...
此.request(…args);
}
public getTestB2=函数(){
...
此.request(…args);
}
}
导出类ChildClass扩展了AbstractBase{
受保护的静态只读默认配置={
...
};
//我在这里,实现我的抽象方法
受保护的httpRequest:ClientHTTPRequest=(输入)=>{
...
返回fetch(input.url{
方法:输入法,
headers:input.headers,
body:input.body,
})。然后((res)=>{
...
});
};
}
这里有人指示我使用mixin(他们甚至关闭了我的原始问题作为可能的副本),但这是一个抽象类,这让我更加困惑:/

我的要求是:

  • AbstractBase
    需要划分为不同的类
  • ChildClass
    将需要从这些不同的类继承(或使用多个mixin)
  • 这是如何实现的? 我对这件事有点迷茫

    我到目前为止所做的尝试

    函数applyMixins(DerivedActor:any,BaseCotors:any[]){ baseCtors.forEach((baseCtor)=>{ Object.getOwnPropertyNames(baseCtor.prototype).forEach((名称)=>{ Object.defineProperty( derivedCtor.prototype, 名称 //@ts忽略 Object.getOwnPropertyDescriptor(baseCtor.prototype,名称), ); }); }); } 导出类子类{ 受保护的httpRequest=(输入)=>{ ... }; } 导出接口子类扩展了AbstractBase{} applyMixins(ChildClass,[AbstractBase]); 结果:

    const test=new ChildClass();
    //test.getesta1不是一个函数
    
    我还尝试了以下方法:

    
    多类{
    //继承方法以创建基类。
    静态继承(…_基){
    班级{
    //基类
    get base(){
    返回基数;
    }
    构造函数(…_参数){
    var指数=0;
    为了(让b离开这个基地){
    设obj=newb(_args[index++]);
    多副本(本,obj);
    }
    }
    }
    //复制属性和方法
    for(let base of _base){
    多副本(类、基);
    Multi.copy(Classes.prototype,base.prototype);
    }
    返回类;
    }
    //将属性从一个类复制到另一个类
    静态拷贝(_目标,_源){
    for(让Reflect.ownKeys(_source))的键{
    if(key!=='constructor'&&key!=='prototype'&&key!=='name'){
    让desc=Object.getOwnPropertyDescriptor(_source,key);
    Object.defineProperty(_target,key,desc);
    }
    }
    }
    }
    导出类ChildClass扩展了Multi.inherit(AbstractBase){
    ...
    }
    
    结果:

    const test=new ChildClass();
    //test.httpRequest不是一个函数
    
    您能解释一下您试图解决的问题,而不是您试图实现的设计吗?您似乎只是希望能够跨多个类重用特定的
    httpRequest
    实现?谢谢您的评论@plalx。好吧,我没有添加这个细节(因为我不想让我的问题更加混乱),但我还有实现自己的
    ChildClass2
    (不同于
    ChildCass
    httpRequest
    。但我认为你是对的:如果我设想的设计是错误的,我不想强制执行。你有什么建议吗?不知道课程的性质很难说。您是否为HTTP API实现了各种facades(客户端)?例如,
    ChildClass
    服务/外观是否允许抽象远程调用?将立面拆分为多个班级的动机是什么?如果我们知道这些类属于哪个真实领域以及它们解决了哪个问题,这会有所帮助。
    ChildClass
    (&
    ChildClass2
    )是负责实现实际的
    httpRequest
    函数的类(从(
    AbstractClass
    )重写抽象函数)这很容易做到:
    class-ChildClass扩展了AbstractClass
    ,而
    class-ChildClass2扩展了AbstractClass
    。现在的困难在于需要将
    getesta1
    /
    getesta2
    拆分为自己的类(想想
    Booking
    package)&
    getTestB1
    /
    getTestB2
    到它自己的类中(想想
    通知
    包)。诸如此类。你看到了吗?你能解释一下你试图解决的问题,而不是你试图实现的设计吗?你似乎只是想能够跨多个类重用特定的
    httpRequest
    实现吗