[英]c# List of derived class
I'm developing a C# WinForm application for work, but I'm stuck and I don't know which is the best way to proceed on. 我正在开发用于工作的C#WinForm应用程序,但是我遇到了麻烦,我不知道哪一种是继续进行的最佳方法。 Here's a snippet of code 这是一段代码
class Block()
{
// generic Block properties
}
class ABlock() : Block
{
// specific ABlock stuff
}
class BBlock() : Block
{
// specific BBlock stuff
}
abstract class Algorithm()
{
// generic Algorithm properties
abstract List<Block> BlockList // ??
}
class AAlgorithm() : Algorithm
{
// specific AAlgorithm stuff
List<ABlock> BlockList // ??
}
class BAlgorithm() : Algorithm
{
// specific BAlgorithm stuff
List<ABlock> BlockList // ??
}
I think the situation is pretty simple: every derived class of Algorithm
has it's own derived class of Block
, but I want to make it generic and accessible from List<Block> BlockList
in asbtract class Algorithm
. 我认为情况非常简单: Algorithm
每个派生类都有它自己的Block
派生类,但是我想使它通用,并且可以从asbtract class Algorithm
List<Block> BlockList
asbtract class Algorithm
中进行asbtract class Algorithm
。 I think that it's a common situation and I'm sure there is a solution. 我认为这是常见的情况,我敢肯定有解决方案。
Thanks in advance 提前致谢
I might be missing something, but couldn't you make it an abstract property? 我可能会丢失一些东西,但是您不能将其设为抽象属性吗?
abstract class Algorithm()
{
abstract List<Block> BlockList { get; }
}
... then when, writing a class that derives from Algorithm, it won't compile unless the BlockList.get is implemented: ...那么,当编写一个从Algorithm派生的类时,除非实现BlockList.get,否则它将不会编译:
public class AlgorithmA : Algorithm
{
public override List<Block> Blocks
{
get
{
return new List<Block> { new ABlock(), new ABlock() };
}
}
}
About the only blemish is that, if code within AlgorithmA wants to use the Blocks property, it might need to cast as (ABlock). 唯一的瑕疵是,如果AlgorithmA中的代码想要使用Blocks属性,则可能需要将其强制转换为(ABlock)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.