在基类中调用访问器的基本实现,获得子实现

时间:2015-08-31 14:06:43

标签: c#

我正在尝试在代码库中设置一系列对象,以便能够使用HTML提供格式良好的字符串表示。但是,当我尝试检索子类的通知文本时,当它获取父访问器时,它将检索子类的NotificationFormat实现,而不是基类NotificationFormat

尝试使用this澄清没有区别,Resharper建议删除冗余限定符。如果我改为将子类改为在我试图保持的方法上使用new,那么当我访问被视为基类的子类的NotificationText时,我只得到基类NotificationText。我以为没有办法从父母那里访问任何东西的子实现。

我的目标是能够在一个可能是基类或子类的对象上调用NotificationText。如果我在一个实际上是子类的对象上调用它,我想使用子实现并让它回调基类实现以获取基类格式化信息。

using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;

public class Program
{   
    public static void Main()
    {
        var TestB = new B();
        TestB.Details = "Some Details";
        TestB.Name = "A Name";
        TestB.ID = 1;

        Console.WriteLine(TestB.NotificationText); // works

        var TestA = (A)TestB;
        Console.WriteLine(TestA.NotificationText); // only returns A notification Text
    }
}

public class A
{
    public long? ID {get;set;}

    public virtual string NotificationFormat
    {
        get
        {
            return "<h3>General Details</h3>" + 
                   "<table> " +
                   "<tr>" + 
                   "<td>Id:</td>" + 
                   "<td>{0}</td>" +
                   "</tr>" +
                   "</table>";
        }
    }

    public virtual string NotificationText
    {
        get
        {
            return string.Format(NotificationFormat, ID);
        }
    }
}

public class B : A
{
    public string Name {get;set;}
    public string Details {get;set;}

    // chnage override to new to get it to compile
    public override string NotificationFormat
    {
        get
        {
            return "<h3>Specific Details</h3>" + 
                   "<table> " +
                   "<tr>" + 
                   "<td>Name:</td>" + 
                   "<td>{0}</td>" +
                   "</tr>" +
                   "<tr>" + 
                   "<td>Details:</td>" + 
                   "<td>{1}</td>" +
                   "</tr>" +
                   "</table>";
        }
    }   

    // chnage override to new to get it to compile
    public override string NotificationText
    {
        get
        {
            var baseNotification = base.NotificationText;
            return baseNotification + string.Format(NotificationFormat, Name, Details);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

由于NotficationFormatvirtual,对Notification的任何调用都会调用派生最多的实现。当您在课程base.NotificationText中致电B时,A.NotificationText中的代码会对NotificaitonFormat进行虚拟通话,因此会拨打B.NotificationFormat。听起来你不希望NotificationFormat是虚拟的:

public class A
{
    public long? ID {get;set;}

    private string NotificationFormat
    {
        get
        {
            return "<h3>General Details</h3>" + 
                   "<table> " +
                   "<tr>" + 
                   "<td>Id:</td>" + 
                   "<td>{0}</td>" +
                   "</tr>" +
                   "</table>";
        }
    }

    public virtual string NotificationText
    {
        get
        {
            return string.Format(NotificationFormat, ID);
        }
    }
}

public class B : A
{
    public string Name {get;set;}
    public string Details {get;set;}

    private string NotificationFormat
    {
        get
        {
            return "<h3>Specific Details</h3>" + 
                   "<table> " +
                   "<tr>" + 
                   "<td>Name:</td>" + 
                   "<td>{0}</td>" +
                   "</tr>" +
                   "<tr>" + 
                   "<td>Details:</td>" + 
                   "<td>{1}</td>" +
                   "</tr>" +
                   "</table>";
        }
    }   

    // change override to new to get it to compile
    public override string NotificationText
    {
        get
        {
            var baseNotification = base.NotificationText;
            return baseNotification + string.Format(NotificationFormat, Name, Details);
        }
    }
}