c#Abstract Abstract Generic Class继承自Abstract Generic Class

时间:2014-10-10 10:29:04

标签: c# generics inheritance

我试图创建一个继承自另一个抽象泛型类的抽象泛型类。

这是我到目前为止所拥有的

public abstract class BaseClass {
    public long Id { get; private set; }

    public BaseClass(long id) {
        this.Id = id;
    }
}

public abstract class BaseClass<T> : BaseClass where T : BaseClass {
    protected BaseClass(long id)
        : base(id) {

    }

    public static T Get(long id) {
        T item;
        return TryGet(id, out item) ? item : default(T);
    }

    public static bool TryGet(long id, out T item) {
        item = null; // This is where I call the cache but for this example I've removed so it will compile
        if (item != null) { return true; }
        else {
            // Call TryGetFallback method
            return false;
        }
    }

    protected abstract T TryGetFallback(long id);
}


public abstract class DerivedClass : BaseClass<DerivedClass> {
    public String Name { get; private set; }

    public DerivedClass(long id, String name)
        : base(id) {
        this.Name = name;
    }
}

public class DerivedDerivedClass : DerivedClass {

    protected override DerivedDerivedClass TryGetFallback(long id) {
        // Handle the try get fallback
    }
}

DerivedDerivedClass上的TryGetFallback方法导致编译器错误。

2 个答案:

答案 0 :(得分:1)

首先,您需要修复BaseClass<T>实现,使其不具有递归类型约束。

public abstract class BaseClass<T> : BaseClass where T : new() {
    //snip
}

然后你可以在派生类中使用它,例如我将使用int作为泛型类型参数:

public abstract class DerivedClass : BaseClass<int> {
    //snip
}

现在如果你编译它会警告你'DerivedDerivedClass' does not implement inherited abstract member 'BaseClass<int>.TryGetFallback(long)'

答案 1 :(得分:0)

感谢@DavidG的提示,它帮助我解决了以下代码的问题

public abstract class BaseClass {
    public long Id { get; private set; }

    public BaseClass(long id) {
        this.Id = id;
    }
}

public abstract class BaseClass<T> : BaseClass where T : BaseClass<T>, new() {
    protected BaseClass(long id) : base(id) { }

    public static T Get(long id) {
        T item;
        return TryGet(id, out item) ? item : default(T);
    }

    public static bool TryGet(long id, out T item) {
        item = null; // Try to get item from cache here
        if (item != null) { return true; }
        else {
            T obj = new T();
            item = obj.TryGetFallback(id);
            return item != null;
        }
    }

    protected abstract T TryGetFallback(long id);
}

public abstract class DerivedClass<T> : BaseClass<T> where T : DerivedClass<T>, new() {
    public String Name { get; private set; }

    public DerivedClass() : base(0) {  }

    public DerivedClass(long id, String name)
        : base(id) {
        this.Name = name;
    }

    protected abstract override T TryGetFallback(long id);
}

public class DerivedDerivedClass : DerivedClass<DerivedDerivedClass> {

    public DerivedDerivedClass() {

    }

    protected override DerivedDerivedClass TryGetFallback(long id) {
        throw new NotImplementedException();
    }
}