C#:基 class 中的受保护方法;无法使用来自另一个 class 的派生 class 对象进行访问
C#: Protected method in base class; unable to access using derived class object from another class
继承自基 class 的 class 是否应该可以访问基 class 的受保护成员?
我正在尝试使用来自另一个 class 的派生 class 的对象访问基 class 的受保护方法,但我收到此错误消息
the base class method is inaccessible due to protection level
我做错了什么?
Program.cs
class Program
{
static void Main(string[] args)
{
DerivedClass dc = new DerivedClass();
dc.DisplayValue();
}
}
BaseClass.cs
class BaseClass
{
private int value = 3;
protected void DisplayValue()
{
Console.WriteLine(this.value);
}
}
DerivedClass.cs
class DerivedClass : BaseClass{}
DerivedClass 中的代码可以访问 BaseClass 的受保护成员,但只能通过 DerivedClass 类型或子类型的表达式。
您的 Main 函数在 Derived Class 之外,这就是您出现异常的原因。
来自 C# 5 规范的第 3.5.3 节(强调我的):
When a protected instance member is accessed outside the program text
of the class in which it is declared, and when a protected internal
instance member is accessed outside the program text of the program in
which it is declared, the access must take place within a class
declaration that derives from the class in which it is declared.
Furthermore, the access is required to take place through an instance
of that derived class type or a class type constructed from it.
您可以将代码更改为此代码,例如使用基的受保护函数 class
public class Program
{
public static void Main(string[] args)
{
DerivedClass dc = new DerivedClass();
dc.Display();
}
}
public class BaseClass
{
private int value = 3;
protected void DisplayValue()
{
Console.WriteLine(this.value);
}
}
public class DerivedClass : BaseClass
{
public void Display()
{
DisplayValue();
}
}
受保护的成员确实在派生的 classes 中可见。但是在您的示例中,您不会从派生的 class 访问 DisplayValue
。您从 Program
访问它, 而不是 从 BaseClass
派生。您需要让该成员 public
.
继承自基 class 的 class 是否应该可以访问基 class 的受保护成员?
我正在尝试使用来自另一个 class 的派生 class 的对象访问基 class 的受保护方法,但我收到此错误消息
the base class method is inaccessible due to protection level
我做错了什么?
Program.cs
class Program
{
static void Main(string[] args)
{
DerivedClass dc = new DerivedClass();
dc.DisplayValue();
}
}
BaseClass.cs
class BaseClass
{
private int value = 3;
protected void DisplayValue()
{
Console.WriteLine(this.value);
}
}
DerivedClass.cs
class DerivedClass : BaseClass{}
DerivedClass 中的代码可以访问 BaseClass 的受保护成员,但只能通过 DerivedClass 类型或子类型的表达式。
您的 Main 函数在 Derived Class 之外,这就是您出现异常的原因。
来自 C# 5 规范的第 3.5.3 节(强调我的):
When a protected instance member is accessed outside the program text of the class in which it is declared, and when a protected internal instance member is accessed outside the program text of the program in which it is declared, the access must take place within a class declaration that derives from the class in which it is declared. Furthermore, the access is required to take place through an instance of that derived class type or a class type constructed from it.
您可以将代码更改为此代码,例如使用基的受保护函数 class
public class Program
{
public static void Main(string[] args)
{
DerivedClass dc = new DerivedClass();
dc.Display();
}
}
public class BaseClass
{
private int value = 3;
protected void DisplayValue()
{
Console.WriteLine(this.value);
}
}
public class DerivedClass : BaseClass
{
public void Display()
{
DisplayValue();
}
}
受保护的成员确实在派生的 classes 中可见。但是在您的示例中,您不会从派生的 class 访问 DisplayValue
。您从 Program
访问它, 而不是 从 BaseClass
派生。您需要让该成员 public
.