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. 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
. 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:
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).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.