多重继承需要的解决方法限制

时间:2013-01-23 16:39:01

标签: vb.net .net-4.0

我有3个类,每个类都可以单独继承某些继承的能力。

但是其中2个类可以继承到第3个类来创建一个“超级”类,其中包括其他2的能力及其默认逻辑实现。

保持编码逻辑集中的最佳方法是什么?

正如您将在下面看到的那样,Class2是我在Class3中构建的一种提炼形式。我想在一个位置实现属性的验证逻辑,而不是开发一个接口,并且必须为每个工具反复实现逻辑。

有一种情况:Class2可以单独使用,也可以与Class1结合使用。每个类都有自己独特的用途,但Class3结合了2& 1用于结合两种类功能的最终实现。

类别:
public mustinherit class Class1(of T as {New, Component})
  private _prop as T

  public sub New()
    ...
  end sub

  protected friend readonly property Prop1 as T
    Get
      ..validation..
      return me._prop
    end get
  end property
end class

public mustinherit class Class2
  private _prop as short

  public sub new(val as short)
    me._prop = val
  end sub

  protected friend readonly property Prop2 as short
    get
      return me._prop
    end get
  end property
end class
当前的Class3实现:
public mustinherit class Class3(of T as {New, Component})
  Inherits Class1(of T)

  'Distilled the logic design below into its own MustInherit class cause this design
  '  by itself is useful without Clas1 implementation.
  private _prop as Short  <--- Same as Class2._prop

  public sub New(val as short)
    me._prop = val
  end sub

  protected friend readonly property Prop2 as short
    get
      return me._prop
    end get
  end property
end class

Per @Servy:

1类
public mustinherit class Data(of T as {New, Component})
...
end class
等级2
public mustinherit class Brand
...
end class
CLASS3
public mustinherit class BrandData(of T as {New, Component})
  inherits Data(Of T)
...
end class

1 个答案:

答案 0 :(得分:1)

  

我想在一个中实现属性的验证逻辑   位置,而不是开发一个接口,必须实现   每个工具的反复逻辑。

无需反复实施界面。您必须立即实现验证逻辑,然后将其注入需要验证的类。

interface IValidationLogic
{}

class Class1
{
    protected readonly IValidationLogic _validationLogic;
    public Class1(IValidationLogicvalidationLogic)
    {
        _validationLogic = validationLogic;
    }
}

class Class2
{
    protected readonly IValidationLogic _validationLogic;
    public Class2(IValidationLogicvalidationLogic)
    {
        _validationLogic = validationLogic;
    }
}

class Class3 : Class2
{
    public Class2(IValidationLogicvalidationLogic)
         : base(validationLogic) 
    {}
}


class MyValidationLogic : IValidationLogic
{}


var obj = new Class3(new MyValidationLogic())