调用基类中访问器的基类实现,获得子实现
本文关键字:实现 基类 访问 调用 | 更新日期: 2023-09-27 18:10:37
我试图在我们的代码库中设置一系列对象,以便能够使用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);
}
}
}
由于NotficationFormat
是virtual
,任何对Notification
的调用都将调用最派生的实现。当您在B
类中调用base.NotificationText
时,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);
}
}
}