我有这样一个类:为什么派生类无法从基类访问受保护的getter?
public class Base
{
public Base(string name)
{
this.Name = name;
}
public string Name { get; set; }
public string PrettyName
{
get { return Prettify(Name); }
}
}
,我从中获得:
public class Derived : Base
{
public Derived(Base b) : base(b.Name) { }
}
的Name
属性不应访问;逻辑名称只能由PrettyName
访问。所以我想我会做这样的属性:
public string Name { protected get; set; }
但我发现了这一点:
Cannot access protected member 'Name' via a qualifier of type 'Base'; the qualifier must be of type 'Derived' (or derived from it)
这是为什么? getter应该暴露给基类和它的所有子类。我在这里弄错了什么?
因为示例代码是简化的,我试图用真正的代码实现的是将'b'的所有属性(不止是名称)复制到新对象。 –