【问题标题】:in C#, How to access base class variable value from child class在 C# 中,如何从子类访问基类变量值
【发布时间】:2018-05-04 06:57:27
【问题描述】:

基类

class TestBase
{
    protected int a;
    protected int b;

    public TestBase(int i)
    {
        a = i;
    }

    protected TestBase()
    {

    }

    public void Update(int i)
    {
        a = i;

        TestChild child = new TestChild();
        child.Update("Hello World ");
    }

儿童班

class TestChild:TestBase
{
    private string msg;
    public void Update (string s)
    {
        msg = s+ a.ToString();
        Console.WriteLine("msg=" + msg);

    }

}

通话

    private void btnTest_Click(object sender, EventArgs e)
    {
        TestBase t = new TestBase(1);
        t.Update(100);

    }

结果 你好世界 0

问题 我希望得到 Hello World 100。显然子类没有访问基类变量 int a。我该怎么做?

【问题讨论】:

  • 为什么要在TestBase.Update 中创建TestChild 实例?这没有意义。
  • 你应该在你的主类中创建 TestChild 而不是在你的基类中
  • 为什么投反对票?他清楚地陈述了他的问题:到目前为止他做了什么,错误的结果是什么以及他的期望是什么。如果您不喜欢他的编码方式,那么您的答案就有一个很好的主题。停止从人们那里窃取积分。

标签: c# inheritance


【解决方案1】:

首先,不清楚您要完成什么。

这段代码有很多问题。首先,您不应该在 TestBase 类方法中创建 TestChild 的实例。如果您在 btnTest_Click 中创建 TestBase 的实例,那么您将无法访问任何 TestChild 的方法或数据(除非您在 TestBase 中创建 TestChild 的实例,这是不好的做法)。

继承的目的通常是扩展基类的数据/方法,而不是在基类中使用继承类。此外,通过一组继承的类,您可以实现多态性,这是面向对象编程的另一个核心原则。你应该更好地理解这些原则,然后代码就会开始变得更有意义。

【讨论】:

    【解决方案2】:

    您在TestBtn_Click 中创建的对象与您在TestBase.Update 中创建的对象无关。它们是两个不同的对象,因此每个对象都有自己的具有不同值的 a 字段。

    要产生您期望的输出,您需要在Update 中将child.a 设置为this.a:

    public void Update(int i)
    {
        a = i;
    
        TestChild child = new TestChild();
        child.a = this.a;
        child.Update("Hello World ");
    }
    

    这里要理解的重要一点是继承不会影响对象。继承只影响类。基类成员也将出现在子类中。就是这样。 尽管objA 的类型继承自objB,但除非它们是同一个对象,否则它们是不相关的。

    【讨论】:

      【解决方案3】:
           public void Update(int i)
           {
           a = i;
           TestChild child = new TestChild(); 
           child.Update("Hello World "); 
           }
      

      这是您为变量 a 赋值的地方,后来您实例化了另一个不同的 TestChild 类实例。 在此实例化之后,您必须分配值,如

          TestChild child = new TestChild();
          child.a = i;
          child.Update("Hello World ");
      

      你会得到你想要的结果

      【讨论】:

        【解决方案4】:

        TestBase 中的 child 对象不知道他放在哪里。它不知道它在一个类中。

        当您创建 child 时,您还创建了一个完全不同的 TestBase 对象,它与包含 child 的对象不同。当您创建该对象时,a 将被初始化为默认值,在这种情况下为 0。

        如果您想将 a 的值传递给其他对象,则应将其传递给它。例如通过构造函数。


        这样调用:

        TestChild child = new TestChild(a);
        

        制作这样的构造函数:

        public TestChild(int x) : base(x){}
        

        不幸的是,您的概念无处不在,因此它无法以这种形式工作。建议多学习inheritance。

        【讨论】:

          【解决方案5】:

          这是如何使用继承:

          class Program
          {
              static void Main(string[] args)
              {
                  Dog dog = new Dog("Dalmata", "Fuffy", 7);
              }
          }
          
          class Animal
          {
              public string Name { get; set; }
              public int Age { get; set; }
          
              public Animal(string name, int age)
              {
                  Name = name;
                  Age = age;
              }
          }
          
          class Dog : Animal
          {
              public string Race { get; set; }
          
              public Dog(string race, string name, int age) : base(name, age)
              {
                  Race = race;
              }
          }
          

          这样,您也可以访问 Name 和 Age,即使它们是在 Animal 中声明的。

          【讨论】:

            【解决方案6】:

            你应该这样调用你的基类

            class BaseClass
            {
                int a;
                int b;
                protected BaseClass()
                { }
            
                protected BaseClass(int i)
                {
                    a = i;
                }
            
                protected void Update(int i)
                {
                    a = i;
            
                    Console.Write("Hello World ");
                }
            }
            
            class TestChild : BaseClass
            {
                public TestChild(int i) : base(i) //send your constuctor to your base class
                { }
            
                public TestChild()
                { }
            
                public void Update(int i)
                {
                    base.Update(i);
                    Console.Write(i.ToString());
                }
            }
            
            private void btnTest_Click(object sender, EventArgs e)
            {
                TestChild t = new TestChild(); // create instance as your child
                t.Update(100);
            }
            

            将您的对象创建为您的子类,然后调用您的方法并让它调用您的基类

            如果想从您的孩子访问基类中的 a 或 b

            public int a;
            

            public 它,以便您可以从您创建的实例中访问它

            protected 将只允许内部子类访问它

            【讨论】:

              【解决方案7】:

              如果您想获得预期的结果,请在 a 和/或 b 变量前面加上 static 修饰符,如下所示:

                static protected int a;
                static protected int b;
              

              之后,如果你按下那个按钮,它会写出:

              msg=Hello World 100
              

              【讨论】:

                猜你喜欢
                • 2013-01-16
                • 2021-05-05
                • 2011-09-05
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多