Inheritance of C# private/protected members
Some say here that if members are protected you can access them: Are private members inherited in C#?
Did someone really tried ?
I have tried it doesn't even work with protected:
public partial class Base
{
protected IObject myObject;
}
If I derive (Base is in another namespace but it shouldn't matter I of course import that namespace)
public partial class Derive: Base
{
}
Intellisense doesn't show myObject in Derive Class.
So what I can do if I need a myObjhect member in all my derived classes to call开发者_Go百科 some methods upon ? If I have to duplicate that member then what's the use of inheritance ?
Update: I forgot Derive:Base but that was just mystypo, of course I did that.
You aren't deriving. To derive, you have to do the following:
public class Base
{
protected IObject myObject;
}
public class Derive : Base
{
}
myObject
is available in Derive
now. Partial means you are splitting the class definition over multiple files.
You have to actually derive from the Base
class:
public partial class Derive : Base
{
}
A derived class has access to the public, protected, internal, and protected internal members of a base class. Even though a derived class inherits the private members of a base class, it cannot access those members. However, all those private members are still present in the derived class and can do the same work they would do in the base class itself. For example, suppose that a protected base class method accesses a private field. That field has to be present in the derived class in order for the inherited base class method to work properly.
From: http://msdn.microsoft.com/en-us/library/ms173149.aspx
check this
Aren't you missing the base class when deriving? Should work with myObject declared as protected.
public partial class Derive : Base
{
}
精彩评论