简体   繁体   中英

Protected attributes in c#

class A
{ 
    protected int i = 13; 
}

class B : A
{
    int i = 9;

    public void fun() {
        console.write(i);
    }
}

Upon calling method fun() from class B object , it prints the value 9.
What if i wanted to access the protected i=13 value in fun() .

When protected attribute have the same name as the derived class private attribute , how to access the protected attribute of the base class within derived class ?

This would be a bad idea anyway. But you may try base.i .

Use the base keyword to access the inherited class:

Console.Write(base.i);

(Code is tested, and it uses the member in the A class.)

If you want to access it from within your derived class, try:

 class B : A
  {
    public void fun()
    {
      Console.Write(base.i);
    }
  }

Sana Tahseen, having class members with the same name in a base and in a derived class is considered bad practice and the compiler will generate a warning message. I suggest you avoid such practices, but if you absolutely need to do this, then you should also use the new keyword in the definition of the member in the derived class:

class A
{    
    protected int i = 13;    
}

class B : A
{
    protected new int i = 9;

    public void fun()
    {
        Console.Write(base.i);
    }
}

The recommended way, however, is not to use members with the same name. You can change the value of the inherited protected member field in the constructor of the derived class:

class A
{    
    protected int i = 13;    
}

class B : A
{
    public B()
    {
        i = 9;
    }

    public void fun()
    {
        Console.Write(i);
    }
}
class A
{ 
    protected int i = 13; 
}

class B : A
{
    int i = 9;

    public void print_own_member() {
        console.write(i);
    }

    public void print_base_class_member() {
        console.write(base.i);
    }
}

The variable i in the B class is hiding the variable i in class A. It smells of a design issue. But, if you really want to access i from the fun method you can do it like this:

class A {    
    protected int i = 13;    
};

class B : A {    
    int i = 9;

    public void fun() {
        console.write( "B's i == " + i );
        console.write( "A's i == " + base.i );    
    }
}
 public class AcessBaseDataMember
{
   public static void Main()
   {
       DerivedClass obj = new DerivedClass();
       obj.Get_Base_DataMember();
       obj.Get_Derived_DataMember();
       Console.Read();
   }


}
public class BaseClass
{
    protected int i = 13;
}

public class DerivedClass:BaseClass { int i = 9; public void Get_Derived_DataMember() { Console.WriteLine(i); } public void Get_Base_DataMember() { Console.WriteLine(base.i); } }

You should use base keyword for this.

Console.Write(base.i); // It prints A class attribute i

Check theses links

http://msdn.microsoft.com/en-us/library/ms173149.aspx

http://msdn.microsoft.com/en-us/library/ms173149(v=vs.80).aspx

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