简体   繁体   中英

Inheritance - Can't access base class data member in derived class

One doubt in inheritance, I have two class named A and B .
A is Base Class and B is Derived Class . B Class inheriting two data members and two member functions of A Class.

In derived class, accessing the static data member is Working but accessing the non static data member gives error. This same case is also for Member Functions. I can't access non static member function.

If i access either static or non static variable | function inside any of derived class function it working fine.

Why i can't access directly in a class. Why its not showing error when i access inside of any derived class function. Any one please clarify my doubts.

参考图片

 class A
{
    protected string msg1;
    protected static string msg2;

    protected  string alert1() {
        return "Welcome";
    }
    protected static string alert2()
    {
        return "Welcome All";
    }
}
class B : A {

    string copyMsg1 = msg1;
    string copyMsg2 = msg2;

    string getMsg1 = alert1();
    string getMsg2 = alert2();

    void display() {
        msg1 = "";
        msg2 = "";
        alert2();           
    }
}

This line is illegal:

string getMsg1 = alert1();

Because it is equivalent to

string getMsg1 = this.alert1();

and accessing this in a field initializer is illegal. Why? Because field initializers run before either the derived class constructor or the base class constructor, and therefore you could be calling a method that depends on the constructor having already run .

The correct solution is to put your initializations into the constructor:

class B : A {
  string copyMsg1;
  string copyMsg2; 
  string getMsg1;
  string getMsg2;

  public B() 
  {
    this.copyMsg1 = this.msg1;
    this.copyMsg2 = A.msg2; 
    this.getMsg1 = this.alert1();
    this.getMsg2 = A.alert2();
  }

The body of the constructor runs after the field initializers of the derived class, the field initializers of the base class, and the constructor body of the base class. The derived constructor body runs last , and therefore you know that all the stuff it accesses has already been created.

While we're at it: note that methods in C# traditionally begin with a capital letter.

Also, there is not really a good reason shown in this code to do the copying at all. You already have access to the base class members from the derived class, so why are you copying them into the derived class?

If i access either static or non static variable | function inside any of derived class function it working fine.

Why i can't access directly in a class. Why its not showing error when i access inside of any derived class function. Any one please clarify my doubts.

In other words you question is: Why can I access the static fields at the class level (outside of any methods or properties) but not instance fields.

Static fields are per class. You do not need an instance of the class but you need the class to be available. Therefore, if the class is available, then you can access it.

Now let's go to non-static fields. Here is your class, please note the numbers in comments:

class B : A {

    string copyMsg1 = msg1; <-- 1. assign non-static to non static 
    string copyMsg2 = msg2; <-- 2. assign static to non static

    string getMsg1 = alert1(); <-- 3. non static calling non-static
    string getMsg2 = alert2(); <-- 4. non static calling static

    void display() {
        msg1 = "";
        msg2 = "";
        alert2();           
    }
}
  1. This is NOT allowed because they are both instance fields (non-static), and there is no guarantee that there will be an instance of A available at this point.
  2. This is allowed because instance fields can access static fields. But not the other way around because an instance may not be avaialbe. Instance fields, methods, and properties can access both static and non static.
  3. This is NOT allowed because of item 1 above.
  4. This is allowed because of 2.

Call the non-static method inside a setter method :

class A
   {
       protected string alert()
       {
           return "me";
       }
   }

    class B :A
    {
        private string s;

        private void setS()
        {
            s = alert();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM