C# Define base class by

后端 未结 4 1051
耶瑟儿~
耶瑟儿~ 2021-01-14 03:11

I am trying to find a way to derive a class from a generic base class. Say:

sealed public class Final : Base
{

}

public class Base

        
4条回答
  •  星月不相逢
    2021-01-14 03:28

    I don't know the context of this question, but I ran into same question with a project where I had to make it possible to extend the base class which is already derived by many others. Like:

    abstract class Base {}
    class FinalA : Base {}
    class FinalB : Base {}
    
    // Now create extended base class and expect final classes to be extended as well:
    class BetterBase : Base {}
    

    The solution was to create common ancestor and connect through properties:

    abstract class Foundation {}
    abstract class Base : Foundation 
    {
        Foundation Final { get; }
    }
    class FinalA : Foundation {}
    class FinalB : Foundation {}
    class FinalC : Foundation
    {
        Foundation Base { get; }
    }
    
    // Here's the desired extension:
    class BetterBase : Base {}
    

    Now BetterBase has connection to final class and if needed, the final classes could have connection with (Better)Base also, as shown in FinalC class.

提交回复
热议问题