【发布时间】:2018-06-06 16:11:04
【问题描述】:
我想避免在我的所有派生类中编写同一行代码。我有以下结构:
using Newtonsoft.Json;
public class BaseClass
{
public string BaseProperty { get; set; }
public string Serialize() { return JsonConvert.SerializeObject(this); }
}
public class DerivedClass : BaseClass
{
public string DerivedProperty { get; set; }
}
当从DerivedClass 调用Serialize() 时会发生什么? this 是否足够聪明,可以知道子对象包含一个额外的属性?或者,我是否仅限于在 BaseClass 的每个孩子中编写同一行代码?
【问题讨论】:
-
是的。
this知道这一点非常聪明。 -
同时检查this SO question
-
谢谢@CodeNotFound。我想我误解了这里描述的一些用法。
标签: c# inheritance polymorphism this