简体   繁体   English

c#类同时实现和继承

[英]c# class implements and inherits at the same time

I want to declare a class that inherits a generic class and implements an interface, such as the following: 我想声明一个继承泛型类并实现接口的类,如下所示:

public class SortableObject
{
    int compare(SortableObejct obj);
}

public class List<T> where T is class
{
    public void add(T obj);
    public T peekCurrent();
}

public class SortedList<T> : List<T> where T : SortableObject, SortableObject
{
    public override int compare(SortableObejct obj);
}

I want SortedList<T> inherits from List<T> and implements from SortableObject , where T is a subclass from SortableObject . 我希望SortedList<T>继承自List<T>并从SortableObject实现,其中TSortableObject的子类。 The c# compiler fails to compile such class; c#编译器无法编译这样的类; it seems to me that the grammar does not support this case. 在我看来,语法不支持这种情况。

Would anyone have met such difficulty and have a solution for it ? 有人会遇到这样的困难并有解决方案吗?

Just make SortableObject implement an interface: 只需让SortableObject实现一个接口:

public interface ISortableObject
{
    int compare(SortableObejct obj);
}

public class SortableObject : ISortableObject
{
    int compare(SortableObejct obj);
}

public class SortedList<T> : List<T> where T : SortableObject

This will ensure that if it is in fact a SortableObject it has implemented the ISortableObject interface. 这将确保如果它实际上是一个SortableObject它已经实现了ISortableObject接口。

You need to make your interface an interface, rather than a class, to start with: 你需要让你的界面成为一个接口,而不是一个类,开始于:

public interface ISortableObject
{
    int compare(ISortableObject obj);
}

Next, your syntax for declaring List<T> wasn't quite right; 接下来,声明List<T>语法不太正确; you weren't declaring the generic constraint properly. 你没有正确地宣布通用约束。 It should be: 它应该是:

public class List<T> 
    where T : class
{
    public void add(T obj);
    public T peekCurrent();
}

Finally, to have a class inherit from a class, implement an interface, and also add generic constraints, you need to do them in that order. 最后,要让类继承自类,实现接口,并添加通用约束,您需要按顺序执行它们。 You can't add the interface implementation after the generic constraints are defined. 在定义了通用约束之后,您无法添加接口实现。

public class SortedList<T> : List<T>, ISortableObject
    where T : ISortableObject
{
    public override int compare(ISortableObject obj);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取一个类继承自并在 C# 中实现的所有类型和接口 - Get all types and interfaces a class inherits from and implements in C# C#中的inherits和implements之间有什么区别 - What is the difference between inherits and implements in C# VB.NET 类继承一个基类并实现一个接口问题(在 C# 中工作) - VB.NET class inherits a base class and implements an interface issue (works in C#) 我怎样才能拥有一个实现接口并继承自抽象 class 的 class? (C# .NET 3.5) - How can I have a class that implements an interface and inherits from an abstract class? (C# .NET 3.5) 如何在 map 继承抽象 class 并在 nHibernate 中同时实现接口的类? - How to map a calss that inherits an asbtract class and implements an interface at the same time in nHibernate? C#:有没有一种方法可以将类强制转换为在运行时实现的接口,而不是在编译时实现的接口? - C#: Is there a way to cast a class to an interface it implements at runtime, but not compile time? 在C#中,如果A实现IX并且B从A继承,那么是否一定要遵循B实现IX? - In C#, if A implements IX and B inherits from A , does it necessarily follow that B implements IX? 检查“T”是否继承或实现了一个类/接口 - Check if 'T' inherits or implements a class/interface 在C#中,将实现接口X的对象传递给从接口X继承的接口Y时出错。 - Error when passing an object that implements interface X to an interface Y that inherits from interface X. in C# 返回类实现C#的接口类型 - Return the types of interfaces that a class implements C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM