问题
Just seen one tutorial saying that:
Class Dog
{
private string Name;
}
Class SuperDog:Dog
{
private string Mood;
}
Then there was an UML displaying that SuperDog will inherit Name as well. I have tried but to me it seems that only public members are inherited. At least I could not access Name unless it was declared as public.
回答1:
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
So, technically, yes, but practically, no.
回答2:
Everything from the base class is inherited to derived class. members marked private are not accessible to derived classes for integrity purpose, should you need to make them accessible in derived class, mark the members as protected.
There are various levels of members' accessibility in context of inheritance.
public
: all public members of the base-class are accessible within the derived-class and to the instances of derived-class.
protected
: all protected members of the base-class are accessible within the derived-class and not to the instances of derived-class.
protected internal
: all protected internal members of the base-class are accessible within the derived-class and to the instances of derived-class created within the same assembly.
internal
: all internal members of the base-class are accessible within the derived-class and to the instances of derived-class within the same assembly.
private
: no private members of the base-class are accessible within the derived-class and to the instances of derived-class.
回答3:
SuperDog will inherit the Name field, yes.
SuperDog will NOT have access to the field though, so there is no practical use (as far as SuperDog is concerned).
回答4:
Private members can be visible inside a derived class: (If the subclass is nested within the base class)
public class Person
{
private string message;
public override string ToString()
{
return message;
}
public static Person CreateEmployee()
{
return new Employee();
}
class Employee : Person
{
public Employee()
{
this.message = "I inherit private members!";
}
}
}
Credit for the example goes to KodefuGuru in this thread at MSDN.
回答5:
No, they aren't.
The protected
modifier can make fields available to derived classes, but this is generally considered a bad idea from a maintenance perspective. You'd want to use protected properties instead.
回答6:
Yes, although heirs cannot access that member.
If you with that they will be able to access it, declare it as protected.
回答7:
try the keyword protected, instead of public/private:
http://msdn.microsoft.com/en-us/library/bcd5672a(VS.71).aspx
回答8:
Make Name protected
or public
instead, that will be accessible. Private members are not accessible from derived classes
回答9:
Private members are not accessible to descendants of a class.
I'm not sure of all the access modifiers, but at the most basic only public and protected members are accessible.
回答10:
Yes, The are inherited. But you cannot access them as they are private :).
回答11:
As others have said private members are inherited. Member access is a different subject but not totally disjoint from an inheritance perspective. It is important to understand that all members are inherited regardless of their access modifier because it effects the sizes of the subclasses. Consider the following code.
public class Foo
{
private int a;
public int b;
}
public class Bar : Foo
{
private int c;
public int d;
}
Foo
will consume 16 bytes on the heap. 4 for the syncblock, 4 for the type information (method table), and 4 each for the int
variables for a total of 12. Bar
, on the other hand, will consume 24 bytes. 4 for the syncblock, 4 for the type information (method table), 4 each for the int
fields inherited from Foo
, and 4 each for the int
fields in Bar
for a total of 24.
回答12:
People have said it, but here's an example of why you need the private fields in the derived class:
class Program
{
static void Main(string[] args)
{
var r = new Random();
foreach(var i in Enumerable.Range(0,100))
new Derived(r).Printer();
Console.Read();
}
}
public class Base
{
private Random r;
public Base(Random r) { this.r = r; }
protected void Print()
{
Console.WriteLine(r.Next(1, 10000));
}
}
public class Derived : Base
{
public Derived(Random r) : base(r) { }
public void Printer()
{
base.Print();
}
}
回答13:
Yes, but they are not accessible, so looking at it you can honestly say that they are not inherited. But yes they really are
来源:https://stackoverflow.com/questions/2950820/are-private-members-inherited-in-c