A friend of mine asked me a few days ago why the parent class cannot be accessed in the subclass.ProtectedMethod?
What she saw in a bookCodeProbably as follows:
Public Class A
{
Public StringName ="";
Protected IntAge = 0;
}
Public Class AB:A
{
Public VoidInit ()
{
AA =New A();
A. Name ="Lifengguo";
A. Age = 24;
}
}
But she is always unable to pass during compilation. She asked me why? My understanding is:
ProtectedIs a protected member, which can be accessed in its class and can be accessed by a derived class.
The above sentence contains three meanings:
1,Class itself can be accessed;
2,Can be accessed in the derived class;
3,Class instances are inaccessible.ProtectedMember.
The following code is used in the class itselfProtectedMember
Public Class A
{
Public StringName ="";
Protected IntAge = 0;
PublicA ()
{
Name ="Lifengguo";
Age = 24;
}
}
The following code is used in the derived classProtectedMember
Public Class AB:A
{
PublicAB ()
{
Base. Name ="Li fengguo";
Base. Age = 24;
}
}
The following code is a member of the Instance category class:
Public Class C
{
Public VoidInit ()
{
AA =New A();
ABAB =New AB();
A. Name ="Lifengguo";// OK
AB. Name ="Li fengguo";// OK
A. Age = 24;// Error
AB. Age = 24;// Error
}
}