Why doesn't my use of "protected" work? [duplicate]
I have read that a protected member can be accessed from derived classes, but the following does not work.
class A
{
protected int Test;
}开发者_如何学Go
class B:A
{
A instanceOfA= new A()
public B()
{
instanceOfA.Test //Not possible
}
}
I have read that a protected member can be accessed from derived classes. Why my does my use of “protected” not work?
This is illegal because you have not provided a guarantee that you are accessing data of an instance of "B". Consider this similar case:
abstract class BankAccount
{
protected int accountNumber;
}
class SwissBankAccount : BankAccount
{
}
--- in another assembly, evil-doers write ---
class EvilBankAccount : BankAccount
{
void DoEvil()
{
BankAccount b = GetASwissBankAccount();
int number = b.accountNumber;
}
}
EvilBankAccount does not inherit from SwissBankAccount, so SwissBankAccount's protected member is not allowed to be used inside EvilBankAccount. You're allowed to access the protected members of your "parents", but not your "siblings"! EvilBankAccount can only access protected members of EvilBankAccount (or a type derived from EvilBankAccount). SwissBankAccount's protected members are off-limits.
The rule is that the type of the "receiver" expression that the protected instance member is accessed through has to be at least as derived as the type declaration containing the member access. For the precise wording of the rule and some illustrative examples see section 3.5.3 of the C# 4.0 specification.
Incidentally, C++ also has this rule.
This rule is frequently misunderstood. For a more in-depth analysis of this rule and some of the other consequences of protected access, see my articles on the subject. The most germane articles I've written on this subject are this one and this one. There are a bunch more articles I've written on related topics here (though some of them wander off the topic of protected access itself and onto the topic of how to use protected access to build a data model with parent references.)
You haven't set up your code correctly. Class B
shouldn't have an instance of class A
. Instead class B
itself inherits the protected variables from class A
.
Your code should look more like:
class A
{
protected int Test;
}
class B:A
{
public B()
{
int someInt = this.Test;
}
}
You can access the Test
int within the B
class itself. However, you cannot access properties of an instance. A
does not know that it is a child of B
so won't give access to its property.
class A
{
protected int Test;
}
class B : A
{
public B()
{
Test = 3; //possible
base.Test = 3; //explicitly calling base member, but not necessary in this case
}
}
As B
already inherits from A
, you don't need a separate instance of A
.
public B()
{
this.Test = 1; //possible
}
You can access to it through the same class not as a public member of the base class.
class A
{
protected int Test;
}
class B:A
{
void TestMethod()
{
this.Test = 3; // Possible
}
}
Check protected access modifier in C#.
Subclasses can access their own inherited members that are marked as protected
.
class A
{
protected int Test;
}
class B : A
{
public B()
{
this.Test = 42; // Possible
}
}
You do not need to create an instance of A
if you inherit A
.
class A
{
protected int Test;
}
class B:A
{
public B()
{
this.Test = 666;
}
}
精彩评论