Can somehow a method from base class return child class?

前端 未结 2 1761
遥遥无期
遥遥无期 2021-01-02 13:19

I\'ll give an example, so you can better understand what i mean:

public class Base
{
      public Base Common()
      {
          return this;
      }
}

pub         


        
2条回答
  •  猫巷女王i
    2021-01-02 14:04

    Have Base be abstract and not implementing XMethod and have the classes overriding XMethod should do it I guess?

    public abstract class Base
    {
          public Base Common()
          {
              return this;
          }
    
         public abstract T XMethod();
    }
    
    public class XBase : Base
    {
    
         public override XBase XMethod()
         {
              return this;
         }
    }
    

提交回复
热议问题