1

好的,这需要先进行一些设置。

public interface IPolicyObjectAdapter
{
    PolicyImage GetPolicyImage(BridgePolicy policy);
}

public class BridgePolicyAdapter : IPolicyObjectAdapter
{
    protected virtual PolicyImage GetPolicyImage(BridgePolicy policy)
    {
         AddPolicyInformation(policy);
    }

    protected virtual void AddPolicyInformation(BridgePolicy policy)
    {
       //does stuff
    }
}


public class HSPolicyAdapter : BridgePolicyAdapter, IPolicyObjectAdapter
{
     protected override void AddPolicyInformation(BridgePolicy policy)
     {
          base.AddPolicyInformation(policy);
          //does more stuff
     }
}

当我执行以下代码时,我希望代码进入 HSPolicyAdapter 的 AddPolicyInformation 方法。但它永远不会。它直接进入 BridgePolicyAdapters AddPolicyInformation 方法。

IPolicyObjectAdapter Adapter = null;
Adapter = new HSPolicyAdapter();
PolicyImage image = Adapter.GetPolicyImage(policy);

我确信我错过了一些如此明显会伤害的东西,但我的大脑现在无法正常工作。我错过了什么?

4

1 回答 1

1

我将这种解释拼凑在一起,对我来说效果很好:

public class BridgePolicy
{
    public string Name { get; set; }
}

public class PolicyImage
{
    public string Name { get; set; }
}

public interface IPolicyObjectAdapter
{
    PolicyImage GetPolicyImage(BridgePolicy policy);
}

public class BridgePolicyAdapter : IPolicyObjectAdapter
{

    protected virtual void AddPolicyInformation(BridgePolicy policy)
    {
        //does stuff
    }


    public virtual PolicyImage GetPolicyImage(BridgePolicy policy)
    {
        AddPolicyInformation(policy);

        return null;
    }
}


public class HSPolicyAdapter : BridgePolicyAdapter, IPolicyObjectAdapter
{
    protected override void AddPolicyInformation(BridgePolicy policy)
    {
        base.AddPolicyInformation(policy);
        //does more stuff
    }
}


class Program
{
    static void Main(string[] args)
    {
        BridgePolicy p = new BridgePolicy();
        IPolicyObjectAdapter Adapter = null;
        Adapter = new HSPolicyAdapter();
        PolicyImage image = Adapter.GetPolicyImage(p);
    }
}
于 2012-11-15T16:09:37.607 回答