我目前正在努力开发一个todo列表(主要是不重要的),我在其中创建一个基类“Task”,它有“Note”,“extendedTask”和“Reminder”类。我还有一个名为“约会”的课程,它继承了提醒。
所有这些类都使用名为“detailsShow”的方法实现一个接口。我希望从子类调用该方法,然后从父类调用相同的方法。
基类中的方法定义为
public virtual string detailsShow()
{
return "Description: " + _taskDescription;
}
虽然子类以这种方式定义它(注意在本例中使用,因为它具有最少数量的变量)
public override string detailsShow()
{
return "Details: " + _noteDescription;
}
我怀疑这是我如何定义基类中的方法和子类中的方法。
另一方面,我如何在RichText框中开始新行
(我希望它看起来如何)
Stuffhere
Andstuffhere
(我用我的方法得到的)
Stuffhere \nAndstuffhere
非常感谢提前
答案 0 :(得分:3)
鉴于代码的当前结构,您的继承者可以调用它的父代,如:
public override string detailsShow()
{
return base.detailsShow() + " Details: " + _noteDescription;
}
可能会产生类似的结果:
Description: Stuffhere Details: Andstuffhere
您不能引用基类Task
并强制它执行virtual detailsShow
,因为继承者可以做出决定。
答案 1 :(得分:1)
您可以调用基类的detailsShow方法并包含Environment.NewLine以在RichTextBox中开始一个新行:
public override string detailsShow()
{
return base.detailsShow() + Environment.NewLine +
"Details: " + _noteDescription;
}