Inheritance : does not contain a definition for and no extension method accepting a first argument

后端 未结 2 984
醉梦人生
醉梦人生 2021-01-21 01:23
abstract class Parent
{
        protected string attrParent;

        public AttrParent { get; protected set }

        public Parent(string sParent)
        {
                  


        
2条回答
  •  盖世英雄少女心
    2021-01-21 01:49

    The moment you assign Child instance to variable typed as Parent you can only access members declared on Parent.

    You'd have to downcast it back to Child to access Child-only members:

    Parent p = new Child();
    
    Child c = (Child)p;
    c.AttrChild = "hello";
    

    That cast might fail at runtime, because there might be a different class that inherits Parent.

提交回复
热议问题