abstract class Parent
{
protected string attrParent;
public AttrParent { get; protected set }
public Parent(string sParent)
{
AttrParent = sParent;
}
}
class Child : Parent
{
private string attrChild;
public AttrChild { get; private set }
public Child(string sParent, string sChild) : base(sParent)
{
AttrChild = sChild;
}
}
class Program
{
static void Main(string[] args)
{
Parent p = new Child();
p.AttrChild = "hello";
}
}
当我运行这个程序时,出现以下错误信息:
“'Example.Parent'不包含定义为 'AttrChild' 的内容,也没有接受类型为 'Example.Parent' 的第一个参数的扩展方法 'AttrChild'。”
有人可以解释一下这是为什么吗?
AttrChild
改为公共的,像这样:public AttrChild { get; set; }
。并将变量p
的类型更改为Child
。 - Yacoub MassadChild
的实例,但是你的变量p
只看到了Parent
中已经存在的部分(因为你将其类型定义为Parent
)......并且在Parent
类中没有AttrChild
。 - marc_sChild
中没有无参构造函数。您能解释一下您想要做什么吗? - Yacoub Massad