C#:具有非抽象计算属性的多重继承
我正在创建一系列包含基本属性的接口/抽象类,并且我希望具有计算属性和多重继承。
public abstract class /interface Modifiable
{
public DateTime ModifiedDate {get; set;}
public boo ModifiedToday
{
get { return DateTime.Now.AddDays(-1).CompareTo(ModifiedDate) >= 0; }
}
public bool ModifiedInLastWeek
{
get { return DateTime.Now.AddDays(-7).CompareTo(ModifiedDate) >= 0; }
}
}
public abstract class /interface Deletable
{
public DateTime DeletionDate {get; set;}
public bool Deleted
{
get { return DeletionDate != default(DateTime) }
}
}
然后我有一个继承这两个接口/抽象类的类。
public class Something : Modifiable, Deletable
{
//
}
但一个类不能继承两个抽象类。所以我需要使用接口,但是使用接口我不能有方法体。然后,我必须在多个类中定义完全相同的函数,以使用接口实现这些简单的 bool 属性。
我也不想从可删除继承可修改,因为我可能希望某些内容可修改但不可删除。这些特定的类不是我的问题,我只是用它们来说明我的问题。
是否有一种设计模式可以通过允许函数体来模仿抽象类,但又允许像接口一样的多个继承者?
I'm creating a series of Interfaces/Abstract classes that contain basic properties and I would like to have computed Properties and multiple inheritance.
public abstract class /interface Modifiable
{
public DateTime ModifiedDate {get; set;}
public boo ModifiedToday
{
get { return DateTime.Now.AddDays(-1).CompareTo(ModifiedDate) >= 0; }
}
public bool ModifiedInLastWeek
{
get { return DateTime.Now.AddDays(-7).CompareTo(ModifiedDate) >= 0; }
}
}
public abstract class /interface Deletable
{
public DateTime DeletionDate {get; set;}
public bool Deleted
{
get { return DeletionDate != default(DateTime) }
}
}
Then I have a class that inherits from these two Interfaces/Abstract classes.
public class Something : Modifiable, Deletable
{
//
}
But a class cannot inherit from two abstract classes. So I then need to use interfaces, but with interfaces I cannot have method bodies. I then have to define the same exact functions across multiple classes to implement these simple bool properties using interfaces.
I also don't want to have Modifiable inherit from Deletable because I might want something to be Modifiable but not Deletable. These specific classes aren't my problem, I'm simply using them to illustrate my problem.
Is there a design pattern that mimics an abstract class by allowing function bodies, but allows multiple inheritors like an interface?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这不是多重继承,但我想到的是扩展方法。
这给人一种被烘焙到类型中的辅助方法的“感觉”,但它们恰好是在其他地方声明的。参加这门课:
你可以这样做:
It's not multiple inheritance, but something that comes to mind is Extension methods.
That gives the "feel" of helper methods that are baked into the type, but they happen to be declared elsewhere. Take this class:
And you can do this:
不可以。C# 没有以这种方式实现多重继承的机制。
对于接口来说,这是可能的,因为当您定义多个接口时,您还需要全部实现它们。
考虑不同的设计,可能使用组合来重用要用于多重继承的类。
No. C# doesn't have a mechanism to implement multiple inheritance this way.
When it comes to interfaces, this is possible because when you define multiple interfaces you also need to implement them all.
Consider a different design, possibly using composition in order to reuse the classes you want to use for multiple inheritance.
我忘记了设计模式名称,但是有一种模式可以通过将方法/属性调用包装在属于同一接口的成员的接口实现周围来实现多个接口:
I forget the design pattern name, but there's a pattern to implement multiple interfaces by wrapping the method/property calls around interface implementations of members who are of that same interface:
是的,实际上有几种方法。一些想法:
显然,以上都不具备多重继承的全部优点。如果您希望在 .NET 中实现多重继承,则可以使用 C++.NET 或 Eiffel.NET。
Yes there are methods, several, actually. A few thoughts:
Deletable
,Modifiable
etc (called marker interfaces), then create extension methods for them. This is not as expandable as multiple inheritance, but it gets a long way.Obviously, none of the above has all the advantages of multiple inheritance. If you want multiple inheritance in .NET, you can use C++.NET or Eiffel.NET.
抱歉,多重继承在 C# 中是不可能的,这对你来说是一个遗憾。您的选择是:
它并不漂亮,但您还可以通过检查实例类型来控制类内的属性可访问性或返回值。
Sorry, multiple inheritance is not possible in C# and that's a bummer for you. Your choices are:
It's not pretty, but you can also control property accessibility or return values inside your classes by checking the instance type.
可修改&可删除的 IMO 应该是接口,而不是基类。基类定义了类型是什么,而接口则描述了类型的用途。
就实现代码而言,您始终可以使用扩展方法:
Modifiable & Deletable IMO should be interfaces, not base classes. A base class defines what a type is, whereas a interface describes what a type does.
As far as implementing the code, you can always use extension methods:
我可能会使用委托来实现这一目标。创建可修改和可删除作为接口,然后创建它们的实现。给出这些实现的
Something
类实例。这是可删除的示例:I would probably use delegation to achieve this. Create Modifiable and Deletable as interfaces, then create implementations of those. Give the
Something
class instances of these implementations. Here's an example for Deletable: