C#中受保护的方法继承
Protected method inheritance in C#
我在阅读有关受保护访问修饰符的文档后编写了一个简单的 C# 代码,但我收到了很多不合逻辑的错误。我找不到任何解决方案。
using System;
public class WeaponController
{
protected void Reload()
{
Console.WriteLine("Reload");
}
protected virtual void Shoot()
{
}
}
public class SMGController : WeaponController
{
private override void Shoot()
{
Console.WriteLine("Shoot");
}
}
public class Test
{
public static void Main()
{
var test = new SMGController();
test.Shoot();
test.Reload();
}
}
我收到以下错误:
prog.cs(19,27): error CS0621: SMGController.Shoot()': virtual or
abstract members cannot be private prog.cs(19,27): error CS0507:
SMGController.Shoot()': cannot change access modifiers when
overriding protected' inherited member
WeaponController.Shoot()'
prog.cs(10,25): (Location of the symbol related to previous error)
Compilation failed: 2 error(s), 0 warnings
如果我将 SMGController 中 Shoot() 方法的访问修饰符更改为受保护,我会收到更多错误:
prog.cs(30,8): error CS1540: Cannot access protected member
WeaponController.Shoot()' via a qualifier of type
SMGController'.
The qualifier must be of type Test' or derived from it
prog.cs(10,25): (Location of the symbol related to previous error)
prog.cs(30,8): error CS0122:
WeaponController.Shoot()' is
inaccessible due to its protection level prog.cs(10,25): (Location of
the symbol related to previous error) prog.cs(31,8): error CS1540:
Cannot access protected member WeaponController.Reload()' via a
qualifier of type
SMGController'. The qualifier must be of type
Test' or derived from it prog.cs(5,17): (Location of the symbol
related to previous error) prog.cs(31,8): error CS0122:
WeaponController.Reload()' is inaccessible due to its protection
level prog.cs(5,17): (Location of the symbol related to previous
error) Compilation failed: 4 error(s), 0 warnings
我的代码有什么问题?
您不能从 class 外部调用 protected
方法,即从 Test
.
调用 WeaponController.Shoot
此外,如果您覆盖 protected
方法,它也必须是 protected
。
显然,您希望 Reload
和 Shoot
为 public
,因此您可以从 Test.Main
.
调用它们
protected
修饰符意味着只有class本身或子class可以访问Shoot
。
您正试图从与 SMGController
和 WeaponController
都没有继承关系的其他 class Test
访问它,并且这种访问被protected
修饰符。
供参考,参见https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/protected,尤其是第一节中的这一行:
A protected member is accessible within its class and by derived class instances.
您可以通过 Shoot
public
来解决这个问题。或者,如果您坚持保留它 protected
,那么您需要添加一个方法,例如public PublicShoot()
然后调用受保护的方法。
我在阅读有关受保护访问修饰符的文档后编写了一个简单的 C# 代码,但我收到了很多不合逻辑的错误。我找不到任何解决方案。
using System;
public class WeaponController
{
protected void Reload()
{
Console.WriteLine("Reload");
}
protected virtual void Shoot()
{
}
}
public class SMGController : WeaponController
{
private override void Shoot()
{
Console.WriteLine("Shoot");
}
}
public class Test
{
public static void Main()
{
var test = new SMGController();
test.Shoot();
test.Reload();
}
}
我收到以下错误:
prog.cs(19,27): error CS0621:
SMGController.Shoot()': virtual or abstract members cannot be private prog.cs(19,27): error CS0507:
SMGController.Shoot()': cannot change access modifiers when overridingprotected' inherited member
WeaponController.Shoot()' prog.cs(10,25): (Location of the symbol related to previous error) Compilation failed: 2 error(s), 0 warnings
如果我将 SMGController 中 Shoot() 方法的访问修饰符更改为受保护,我会收到更多错误:
prog.cs(30,8): error CS1540: Cannot access protected member
WeaponController.Shoot()' via a qualifier of type
SMGController'. The qualifier must be of typeTest' or derived from it prog.cs(10,25): (Location of the symbol related to previous error) prog.cs(30,8): error CS0122:
WeaponController.Shoot()' is inaccessible due to its protection level prog.cs(10,25): (Location of the symbol related to previous error) prog.cs(31,8): error CS1540: Cannot access protected memberWeaponController.Reload()' via a qualifier of type
SMGController'. The qualifier must be of typeTest' or derived from it prog.cs(5,17): (Location of the symbol related to previous error) prog.cs(31,8): error CS0122:
WeaponController.Reload()' is inaccessible due to its protection level prog.cs(5,17): (Location of the symbol related to previous error) Compilation failed: 4 error(s), 0 warnings
我的代码有什么问题?
您不能从 class 外部调用 protected
方法,即从 Test
.
WeaponController.Shoot
此外,如果您覆盖 protected
方法,它也必须是 protected
。
显然,您希望 Reload
和 Shoot
为 public
,因此您可以从 Test.Main
.
protected
修饰符意味着只有class本身或子class可以访问Shoot
。
您正试图从与 SMGController
和 WeaponController
都没有继承关系的其他 class Test
访问它,并且这种访问被protected
修饰符。
供参考,参见https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/protected,尤其是第一节中的这一行:
A protected member is accessible within its class and by derived class instances.
您可以通过 Shoot
public
来解决这个问题。或者,如果您坚持保留它 protected
,那么您需要添加一个方法,例如public PublicShoot()
然后调用受保护的方法。