- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 Project
类,我想从中提取一个接口(interface),因为我们将处理不同类型的项目。 Project
的属性之一称为 Data
:
class Project {
public Data D {get;}
public void SomeSpecificMethodReferencingData()
{
D.SomeSpecificMethod();
}
}
class Data {
public void SomeGenericMethod() { }
public void SomeSpecificMethod() { }
}
现在,Data
也需要作为接口(interface)提取(但只定义了 SomeGenericMethod()
。我目前正在使用这个:
interface IProject {
IData D {get;}
}
interface IData {
void SomeGenericMethod();
}
class Data : IData {
public void SomeGenericMethod() { }
public void SomeSpecificMethod() { }
}
class OtherData : IData {
public void SomeGenericMethod() { }
public int SomeOtherSpecificMethod(float someArgument) { }
}
class Project : IProject {
public IData D { get; }
public void SomeSpecificMethodReferencingData()
{
D.SomeSpecificMethod(); // this does not work!
(D as Data).SomeSpecificMethod(); // this looks stupid!
}
}
class OtherProject : IProject {
public IData D { get; }
public void SomeOSpecificMethodReferencingOtherData()
{
var i = D.SomeOtherSpecificMethod(14.0f); // this does not work!
var i = (D as OtherData).SomeOtherSpecificMethod(14.0f); // this looks stupid!
}
}
我遇到的问题是,在 Project
类中,我引用了 SomeSpecificMethod
。但是,当我为 Data
提取接口(interface)时,我首先必须将其从 IData
转换为能够引用特定方法。这是不可取的,因为 IData 始终是该项目的数据实例。当我构建我的 OtherProject
时,我将为它创建一个 OtherData
,以便获得成对的 xxxProject 和 xxxData 实现。
是否有某种设计模式可以帮助我构建成对的相关类和引用类?类似于抽象工厂,但更好?
最佳答案
你可以为此使用泛型:
interface IProject<T> where T: IData {
T D {get;}
}
现在你的不同项目是这样的:
class Project : IProject<Data>
{
public Data D { get; }
public void SomeSpecificMethodReferencingData()
{
D.SomeSpecificMethod(); // D is of type Data
}
}
class OtherProject : IProject<OtherData> {
public OtherData D { get; }
public void SomeOSpecificMethodReferencingOtherData()
{
D.SomeOtherSpecificMethod(14.0f); // D is of type OtherData
}
}
关于c# - 如果这也是一个接口(interface),则提取接口(interface)会隐藏成员的细节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58131146/
前言 本文主要介绍了关于MySQL主键为0与主键自排约束的关系,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。 开始不设置主键表的设计如下: 如果id的位置有好几个0
我已经阅读了一些关于将消息从一个线程冒泡到所有其他线程以正常退出的正确方法的来源(每个线程都执行它自己的退出例程)。其中,我喜欢全局原子 bool 值的想法,它可以从任何线程进行标记,所有其他线程检查
本文深入探讨Go语言中的流程控制语法,包括基本的 if-else 条件分支、 for 循环、 switch-case 多条件分支,以及与特定数据类型相关的流程控制,如 for-r
我是 MVC 和 XCode 的新手,在将我对 MVC 的概念理解转化为设计和实现具体类时遇到了困难。我希望就如何构建 Controller 和 View 以获得预期的 UI 获得一些建议。这是针对
如果我尝试在 View 中打开 DeatilFragement,我的应用程序崩溃并收到以下错误: Caused by: java.lang.IllegalStateException: Require
我正在尝试构建我的 iOS 应用程序的界面。一遍又一遍地开始新项目我仍然遇到细节 View 控件的问题(见图)。 在这里我得到了截图: 详细 View 显示当用户触摸 UITableView 行时。您
我在与我正在处理的项目的类(class)中遇到问题。该类是一个接受标签和值的 GUI 组件。这里的想法是,用户可以指定一个标签,然后从任何地方链接一个值(更具体地说,该值的 ToString 方法),
嗯.. 我在我的应用程序中设置了表格 View - 详细 View 。 主视图使用常规代码将数据传递给详细 View - (void)tableView:(UITableView *)tableVie
我有 celery 任务,队列中有 100 个输入数据,需要使用 5 个 worker 来执行。 如何获取哪个工作人员正在执行哪个输入? 每个 worker 执行了多少输入及其状态? 如果任何任务失败
我有一个 .net github 项目,它基本上是一个 Web API 的包装器。在测试项目中,我使用 API key 调用 API。我需要将此 key 保密,如何在 Visual Studio 项目
我遇到一个问题,从 Ag-Grid 导出网格只会导出主网格的详细信息,而不会导出子网格。这是一个显示问题的 plunkr: https://next.plnkr.co/edit/jVcvWDJ1NKP
我在详细 View 中有一个不会消失的额外空间。该 View 来自 NavigationLink,但我已经尝试过使用或不使用 NavigationView。我试图包装它用 NavigationView
几天来,我一直在关注猫效应和 IO。我觉得我对这种效果有一些误解,或者只是我错过了它的重点。 首先——如果IO可以替代Scala的Future,我们如何创建异步IO任务?使用 IO.shift ?使用
如何将标高添加到主视图/详细 View 的详细信息 Pane 中,以在其下方提供阴影,同时定位为部分覆盖工具栏(如下面的底部图片)?我尝试使用 android:elevation="4dp" 但这对我
我试图在我的 UISplitViewController 的细节 View 上设置一个阴影,我希望在 iOS 6 中的主 View 上可见。 在我的细节 View Controller 中: sel
我正在阅读 std::basic_string::reserve(size_type res_arg=0) 上的标准.它是这样说的: void reserve(size_type res_arg=0)
Boost 文档说 Starting with Boost release 1.53, shared_ptr can be used to hold a pointer to a dynamicall
我用 OpenGL 编写了一个简单的 24 位位图加载器。我打开一个位图文件并读取它的像素,然后从中创建一个 RGB 像素数据数组,然后将其传递给 glDrawPixels()。 问题:我需要使图像的
for x in ...循环 就是把每个元素代入变量x,然后执行缩进块的语句。 range()函数,可以生成一个整数序列,再通过list()函数可以转换为list。 比如我们想计算1-10的整数
场景 我有一个 DevExpress XtraGrid。 显示的数据采用主/详细信息格式,点击行开头的“+”可展开该主行的详细信息。 我通过将网格数据源绑定(bind)到包含自己的字典属性(以保存详细
我是一名优秀的程序员,十分优秀!