从子类访问母类的属性 [英] Accessing properties of a mother class from a child class
问题描述
我的A类方法依赖于其中一个属性
I have class A with a method dependent on one of its properties
公共类A
{
              public string strg {get;组; } =" notstringvalue";
{
public string strg{ get; set; } = "notstringvalue";
              public void mthd()
public void mthd()
              {
{
                            if(strg ==" stringvalue")
if ( strg == "stringvalue")
                            {                              
     
{
Console.WriteLine("B");
                            }
}
              }
}
}
B类来自A,如果我这样做
Class B derives from A, If I do this
公共等级B:A
{
              strg =" stringvalue";
{
strg = "stringvalue";
}
来自B实例(b.mthd())的mthd调用是否真的写" B" ;?如果没有,是否有解决方案?
Will a call of mthd from an instance of B ( b.mthd() ) really write "B"? If not, is there a solution?
推荐答案
是的,但你不能在B(或构造函数)的方法之外做到这一点
Yea but you cannot do that outside a method in B (or the constructor)
这篇关于从子类访问母类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!