根据子类指定基类抽象方法的返回类型

发布于 2024-07-16 11:03:33 字数 887 浏览 5 评论 0原文

我有以下结构:

abstract class Base {
        public abstract List<...> Get(); //What should be the generic type?
}

class SubOne : Base {
    public override List<SubOne> Get() {

    }
}

class SubTwo : Base {
    public override List<SubTwo> Get() {

    }
}

我想创建一个抽象方法,该方法返回具体子类是什么类。 因此,正如您从示例中看到的,SubOne 中的方法应返回 List,而 SubTwo 中的方法应返回 列出

我在基类中声明的签名中指定什么类型?


[更新]

感谢您发布的答案。

解决方案是使抽象类变得通用,如下所示:

abstract class Base<T> {
        public abstract List<T> Get();
}

class SubOne : Base<SubOne> {
    public override List<SubOne> Get() {

    }
}

class SubTwo : Base<SubTwo> {
    public override List<SubTwo> Get() {

    }
} 

I have the following structure:

abstract class Base {
        public abstract List<...> Get(); //What should be the generic type?
}

class SubOne : Base {
    public override List<SubOne> Get() {

    }
}

class SubTwo : Base {
    public override List<SubTwo> Get() {

    }
}

I want to create an abstract method that returns whatever class the concrete sub class is. So, as you can see from the example, the method in SubOne should return List<SubOne> whereas the method in SubTwo should return List<SubTwo>.

What type do I specify in the signature declared in the Base class ?


[UPDATE]

Thank you for the posted answers.

The solution is to make the abstract class generic, like such:

abstract class Base<T> {
        public abstract List<T> Get();
}

class SubOne : Base<SubOne> {
    public override List<SubOne> Get() {

    }
}

class SubTwo : Base<SubTwo> {
    public override List<SubTwo> Get() {

    }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

娜些时光,永不杰束 2024-07-23 11:03:33

你的抽象类应该是通用的。

abstract class Base<T> {
        public abstract List<T> Get(); 
}

class SubOne : Base<SubOne> {
    public override List<SubOne> Get() {

    }
}

class SubTwo : Base<SubTwo> {
    public override List<SubTwo> Get() {
    }
}

如果需要引用不带泛型类型参数的抽象类,请使用接口:

interface IBase {
        //common functions
}

abstract class Base<T> : IBase {
        public abstract List<T> Get(); 
}

Your abstract class should be generic.

abstract class Base<T> {
        public abstract List<T> Get(); 
}

class SubOne : Base<SubOne> {
    public override List<SubOne> Get() {

    }
}

class SubTwo : Base<SubTwo> {
    public override List<SubTwo> Get() {
    }
}

If you need to refer to the abstract class without the generic type argument, use an interface:

interface IBase {
        //common functions
}

abstract class Base<T> : IBase {
        public abstract List<T> Get(); 
}
半边脸i 2024-07-23 11:03:33
public abstract class Base<T> 
{       
    public abstract List<T> Get(); 
}

class SubOne : Base<SubOne> 
{
    public override List<SubOne> Get() { return new List<SubOne>(); }
}

class SubTwo : Base<SubTwo> 
{
    public override List<SubTwo> Get() { return new List<SubTwo>(); }
}
public abstract class Base<T> 
{       
    public abstract List<T> Get(); 
}

class SubOne : Base<SubOne> 
{
    public override List<SubOne> Get() { return new List<SubOne>(); }
}

class SubTwo : Base<SubTwo> 
{
    public override List<SubTwo> Get() { return new List<SubTwo>(); }
}
独夜无伴 2024-07-23 11:03:33

尝试这个:

public abstract class Base<T> {
  public abstract List<T> Foo();
}

public class Derived : Base<Derived> {   // Any derived class will now return a List of
  public List<Derived> Foo() { ... }     //   itself.
}

Try this:

public abstract class Base<T> {
  public abstract List<T> Foo();
}

public class Derived : Base<Derived> {   // Any derived class will now return a List of
  public List<Derived> Foo() { ... }     //   itself.
}
云胡 2024-07-23 11:03:33

我不认为你可以让它成为特定的子类。 不过你可以这样做:

abstract class Base<SubClass> {
        public abstract List<SubClass> Get(); 
}
class SubOne : Base<SubOne> {
    public override List<SubOne> Get() {
        throw new NotImplementedException();
    }
}
class SubTwo : Base<SubTwo> {
    public override List<SubTwo> Get() {
        throw new NotImplementedException();
    }
}

I don't think you can get it to be the specific subclass. You can do this though:

abstract class Base<SubClass> {
        public abstract List<SubClass> Get(); 
}
class SubOne : Base<SubOne> {
    public override List<SubOne> Get() {
        throw new NotImplementedException();
    }
}
class SubTwo : Base<SubTwo> {
    public override List<SubTwo> Get() {
        throw new NotImplementedException();
    }
}
坚持沉默 2024-07-23 11:03:33

如果您的基类由于各种原因不能通用,那么此方法可能会很有用:

abstract class Base {
}
interface IHasGetter<T> {
  List<T> Get();
}
static class HasGetterHelper {
    public static List<T> Get<T>(this T v) where T: IHasGetter<T>  => v.Get();
}
class SubOne : Base, IHasGetter<SubOne> {
    public List<SubOne> Get() {
        throw new NotImplementedException();
    }
}
class SubTwo : Base, IHasGetter<SubTwo> {
    public List<SubTwo> Get() {
        throw new NotImplementedException();
    }
}

如果您无法向子类添加接口,并且仍然无法将泛型添加到基本类型此方法可能有用:
(不幸的是,您不能使 GetImpl 受到保护,因为不允许帮助程序类位于基类内部)

abstract class Base {
    public abstract List<T> GetImpl<T>() where T:Base;
}
static class HasGetterHelper {
    public static List<T> Get<T>(this T v) where T: Base => v.GetImpl<T>();
}
class SubOne : Base {
    public override List<T> GetImpl<T>() {
        throw new NotImplementedException();
    }
}
class SubTwo : Base {
    public override List<T> GetImpl<T>() {
        throw new NotImplementedException();
    }
}

在这两种情况下,这都将按预期工作:

class Tester
{
   List<SubOne> TestIt() => new SubOne().Get();
}

If you have an situation where your base class cannot be generic for various reasons, this method might be useful:

abstract class Base {
}
interface IHasGetter<T> {
  List<T> Get();
}
static class HasGetterHelper {
    public static List<T> Get<T>(this T v) where T: IHasGetter<T>  => v.Get();
}
class SubOne : Base, IHasGetter<SubOne> {
    public List<SubOne> Get() {
        throw new NotImplementedException();
    }
}
class SubTwo : Base, IHasGetter<SubTwo> {
    public List<SubTwo> Get() {
        throw new NotImplementedException();
    }
}

If you are unable to add interface to your sub classes, and still unable to add generics to the Base type this method might be useful:
(Unfortunately you cannot make the GetImpl protected, since the helper class is not allowed to be inside the base class)

abstract class Base {
    public abstract List<T> GetImpl<T>() where T:Base;
}
static class HasGetterHelper {
    public static List<T> Get<T>(this T v) where T: Base => v.GetImpl<T>();
}
class SubOne : Base {
    public override List<T> GetImpl<T>() {
        throw new NotImplementedException();
    }
}
class SubTwo : Base {
    public override List<T> GetImpl<T>() {
        throw new NotImplementedException();
    }
}

In both cases, this will work as expected:

class Tester
{
   List<SubOne> TestIt() => new SubOne().Get();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文