C#:具有非抽象计算属性的多重继承

发布于 2024-10-20 09:08:15 字数 910 浏览 4 评论 0原文

我正在创建一系列包含基本属性的接口/抽象类,并且我希望具有计算属性和多重继承。

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 技术交流群。

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

发布评论

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

评论(7

成熟稳重的好男人 2024-10-27 09:08:15

这不是多重继承,但我想到的是扩展方法。

public interface IModifiable
{
    DateTime ModifiedDate {get; set;}
}

public static class ModifiableExtensions
{
   public bool ModifiedToday(this IModifiable m)
   {
      return DateTime.Now.AddDays(-1).CompareTo(m.ModifiedDate) >= 0;
   } 

   public bool ModifiedInLastWeek(this IModifiable m)
   {
     return DateTime.Now.AddDays(-7).CompareTo(m.ModifiedDate) >= 0; 
   }

}

这给人一种被烘焙到类型中的辅助方法的“感觉”,但它们恰好是在其他地方声明的。参加这门课:

public class MyModifiable :IModifiable
{
     public ModifiedDate {get; set;}
}

你可以这样做:

MyModifiable m = new MyModifiable;

m.ModifiedDate = DateTime.Now;

bool isToday = m.ModifiedToday();

It's not multiple inheritance, but something that comes to mind is Extension methods.

public interface IModifiable
{
    DateTime ModifiedDate {get; set;}
}

public static class ModifiableExtensions
{
   public bool ModifiedToday(this IModifiable m)
   {
      return DateTime.Now.AddDays(-1).CompareTo(m.ModifiedDate) >= 0;
   } 

   public bool ModifiedInLastWeek(this IModifiable m)
   {
     return DateTime.Now.AddDays(-7).CompareTo(m.ModifiedDate) >= 0; 
   }

}

That gives the "feel" of helper methods that are baked into the type, but they happen to be declared elsewhere. Take this class:

public class MyModifiable :IModifiable
{
     public ModifiedDate {get; set;}
}

And you can do this:

MyModifiable m = new MyModifiable;

m.ModifiedDate = DateTime.Now;

bool isToday = m.ModifiedToday();
雨后彩虹 2024-10-27 09:08:15

不可以。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.

私藏温柔 2024-10-27 09:08:15

我忘记了设计模式名称,但是有一种模式可以通过将方法/属性调用包装在属于同一接口的成员的接口实现周围来实现多个接口:

interface IDrivable {
  void Drive();
}

interface IFlyable {
  void Fly();
}

class Car : IDrivable {
  public void Drive() { /* Implementation */ }
}

class Plane : IFlyable {
  public void Fly() { /* Implementation */ }
}

class MyClass : IDrivable, IFlyable {
  private IDrivable _car = new Car();
  private IFlyable _plane = new Plane();

  public void Drive() { _car.Drive(); }
  public void Fly() { _plane.Fly(); }
}

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:

interface IDrivable {
  void Drive();
}

interface IFlyable {
  void Fly();
}

class Car : IDrivable {
  public void Drive() { /* Implementation */ }
}

class Plane : IFlyable {
  public void Fly() { /* Implementation */ }
}

class MyClass : IDrivable, IFlyable {
  private IDrivable _car = new Car();
  private IFlyable _plane = new Plane();

  public void Drive() { _car.Drive(); }
  public void Fly() { _plane.Fly(); }
}
没企图 2024-10-27 09:08:15

是的,实际上有几种方法。一些想法:

  • 为可删除、可修改等(称为标记接口)使用空接口,然后为它们创建扩展方法。这不像多重继承那样可扩展,但它还有很长的路要走。
  • 使用通用性,可能使用相同的标记接口来创建依赖项。这样,您就可以拥有一个包含可修改和可删除的所有方法的基类,包括派生类中的抽象方法和重写实现使用
  • 面向方面的编程来获得相同的结果
  • 几乎相同,但可以使用 Castle 或类似的库自行完成,可能在属性的帮助下。

显然,以上都不具备多重继承的全部优点。如果您希望在 .NET 中实现多重继承,则可以使用 C++.NET 或 Eiffel.NET。

Yes there are methods, several, actually. A few thoughts:

  • Use an empty interface for 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.
  • Use genericity, possibly with the same tagging interfaces to create dependencies. This way you can have a base class with all methods for both Modifiable and Deletable, including abstract methods and override implementation in derived classes
  • Use aspect oriented programming to get to the same results
  • Almost the same, but do it yourself with Castle or similar library, possibly with the help of attributes.

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.

音盲 2024-10-27 09:08:15

抱歉,多重继承在 C# 中是不可能的,这对你来说是一个遗憾。您的选择是:

  1. 要么将基类继承链接为 MyClass : MyBaseClass : EvenBasierClass
  2. 要么从多个接口继承。并实现所有接口的所有方法。

它并不漂亮,但您还可以通过检查实例类型来控制类内的属性可访问性或返回值。

Sorry, multiple inheritance is not possible in C# and that's a bummer for you. Your choices are:

  1. Either to chain your base class inheritance a la MyClass : MyBaseClass : EvenBasierClass
  2. Or inherit from multiple interfaces. And implement all methods of all interfaces.

It's not pretty, but you can also control property accessibility or return values inside your classes by checking the instance type.

长不大的小祸害 2024-10-27 09:08:15

可修改&可删除的 IMO 应该是接口,而不是基类。基类定义了类型是什么,而接口则描述了类型的用途。

就实现代码而言,您始终可以使用扩展方法:

public interface IModifiable
{
   public DateTime ModifiedDate {get; set;}
}

public interface IDeletable
{
   public DateTime DeletionDate {get; set;}    
}


public static class SomeExtentions
{
    public static bool IsModifiedToday(this IModifiable modifiable)
    {
        return DateTime.Now.AddDays(-1).CompareTo(modifiable.ModifiedDate) >= 0;
    } 

    public static bool IsModifiedInLastWeek(this IModifiable modifiable)
    {
        return DateTime.Now.AddDays(-7).CompareTo(modifiable.ModifiedDate) >= 0;
    }   
    public static bool IsDeleted(this IDeletable deletable)
    {
        return deletable.DeletionDate != default(DateTime);
    }
}

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:

public interface IModifiable
{
   public DateTime ModifiedDate {get; set;}
}

public interface IDeletable
{
   public DateTime DeletionDate {get; set;}    
}


public static class SomeExtentions
{
    public static bool IsModifiedToday(this IModifiable modifiable)
    {
        return DateTime.Now.AddDays(-1).CompareTo(modifiable.ModifiedDate) >= 0;
    } 

    public static bool IsModifiedInLastWeek(this IModifiable modifiable)
    {
        return DateTime.Now.AddDays(-7).CompareTo(modifiable.ModifiedDate) >= 0;
    }   
    public static bool IsDeleted(this IDeletable deletable)
    {
        return deletable.DeletionDate != default(DateTime);
    }
}
天气好吗我好吗 2024-10-27 09:08:15

我可能会使用委托来实现这一目标。创建可修改和可删除作为接口,然后创建它们的实现。给出这些实现的 Something 类实例。这是可删除的示例:

public interface Deletable
{
  DateTime DeletionDate{get;set;}
  bool Deleted{get;}
}

public class DeletableImpl : Deletable
{
  public DateTime DeletionDate{get; set;}
  public bool Deleted{get {return DeletionDate != default(DateTime);}}
}
// Do the same thing with Modifiable and ModifiableImpl

public class Something : Deletable, Modifiable
{
  private Deletable _deletable = new DeletableImpl();
  private Modifiable _modifiable = new ModifiableImpl();
  public DateTime DeletionDate
  {
    get{return _deletable.DeletionDate;}
    set{_deletable.DeletionDate = value;}
  }
  public bool Deleted{get{return _deletable.Deleted;}}
  public DateTime ModifiedDate {
    // and so on as above
}

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:

public interface Deletable
{
  DateTime DeletionDate{get;set;}
  bool Deleted{get;}
}

public class DeletableImpl : Deletable
{
  public DateTime DeletionDate{get; set;}
  public bool Deleted{get {return DeletionDate != default(DateTime);}}
}
// Do the same thing with Modifiable and ModifiableImpl

public class Something : Deletable, Modifiable
{
  private Deletable _deletable = new DeletableImpl();
  private Modifiable _modifiable = new ModifiableImpl();
  public DateTime DeletionDate
  {
    get{return _deletable.DeletionDate;}
    set{_deletable.DeletionDate = value;}
  }
  public bool Deleted{get{return _deletable.Deleted;}}
  public DateTime ModifiedDate {
    // and so on as above
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文