如果我有一个基类和两个派生类,我想手工实现两个派生类之间的转换,有什么办法吗? (在 C# 中)
abstract class AbsBase
{
private int A;
private int B;
private int C;
private int D;
}
class Imp_A : AbsBase
{
private List<int> E;
}
class Imp_B : AbsBase
{
private int lastE;
}
通常我将从 Imp_A -> Imp_B 进行转换,我希望 E 列表中的最后一个值是“LastE”。另外,如果有三个或更多实现类(例如 Salary、Hourly、Consultant 和 Former Employees)怎么办。
无论这在架构上是否合理(我无法描述整个应用程序并且简明扼要)是否可能?
我本来打算写一个转换器,但据我所知,转换器会创建一个 Imp_B 类的新对象,我不需要它,因为“员工”在任何时候都只是一个选项.
-德文
最佳答案
class Imp_A : AbsBase
{
public static explicit operator Imp_B(Imp_A a)
{
Imp_B b = new Imp_B();
// Do things with b
return b;
}
}
现在您可以执行以下操作。
Imp_A a = new Imp_A();
Imp_B b = (Imp_B) a;
关于c# - 超越(转换),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/725435/
如果我有一个基类和两个派生类,我想手工实现两个派生类之间的转换,有什么办法吗? (在 C# 中) abstract class AbsBase { private int A; priva
非常基本的场景: 我的 Nib 上有一个 NSTableView,有一个指向它的 socket 。我的应用程序委托(delegate)中有以下内容: - (void)applicationDidFin
我正在尝试使用 R 来估计具有手动规范的多项 logit 模型。我找到了一些可以让您估计 MNL 模型的软件包 here或 here . 我发现了一些关于“滚动”你自己的 MLE 函数的其他著作 he
我正在监视某些 FreeIPA 服务器,这些服务器通常 fork 300 覆盖我专门为同样继承“Template OS Linux”和“Template IPA Servers”的此类服务器创建的另一
我正在尝试分析文本,但我的 Mac 的 RAM 只有 8 GB,并且 RidgeRegressor 在一段时间后停止,并显示 Killed: 9。我认为这是因为它需要更多内存。 有没有办法禁用堆栈大小
我有一个名为 sourceTable 的数据表,其中包含 source_Id、title 和 programme_Id 列。第二个数据表是 credits,包含 credit_Id、programme
这或多或少是一个以框架为中心的版本 past Stack Overflow question ,这是关于 MVC 应用程序的大多数介绍性 Material 如何倾向于呈现模型、 View 和 Cont
从 Java 转向 Python,有人告诉我工厂不是 Pythonic。因此,我正在寻找 a Python 方法来执行如下操作。 (我过度简化了我的目标,这样我就不必描述我的整个程序,这非常复杂)。
当 UIButton 的框架位于其父框架之外时,UIButton(或任何其他控件)是否有可能接收触摸事件?因为当我尝试这个时,我的 UIButton 似乎无法接收任何事件。我该如何解决这个问题? 最佳
我以 VBto 为起点,并大量学习了 Delphi 6 User's Guide。我可以编译我的新组件,但我想不出办法让它显示,所以我可以完成调试。 50 年的编程经验也无济于事。这是我的组件的内容:
对于以下代码,我得到的平均计算时间为 50 毫秒。我该如何优化 filter(u -> myStrings.contains(u.getName()) 获得更快的计算时间? list size 300