【发布时间】:2015-12-21 11:44:22
【问题描述】:
我在远程服务器中有一个名为HasChar 的属性,类型为Bool。我需要在我的 Derived 类中重新定义它并进行一些修改。但是基本属性没有标记为Partial、abstract 或extern。但我需要重新定义,我该如何实现?在派生类中,我需要访问派生类中基类的HasChar属性,如果值为False,则必须将BlogText设为String.Empty
注意:基类在远程服务器中,我无法更改它或 启动更改。基类属性未标记为
Partial、abstract或extern。不要创建任何额外的属性来实现这一点。请提供与 Override 或类似的解决方案。
我的基类
public class BlogBase
{
private string _blogText = string.Empty;
public string BlogText
{
get { return _blogText; };
set
{
_blogText = value;
HasChar = _blogText.Length >0 ? true : false;
}
}
public bool HasChar { get; set; }
}
我的派生类:粗略代码
public class BlogChild : BlogBase
{
private bool _hasChar = false;
public bool HasChar
{
get { return _hasChar; };
set
{
_hasChar = value;
if(!_hasChar)
BlogText = string.Empty;
}
}
}
【问题讨论】:
-
尝试用“new”关键字标记属性,或者制作一些包装器并使用它
-
@Alex 我无法更改基类,请查看注释。
-
@CodeCaster - 我向您展示了示例代码。我无法解释整个项目。我需要如上所述的要求。如果你理解要求,那就给你答案,否则不要。
标签: c# class inheritance properties overriding