gpt4 book ai didi

c# - 尝试访问派生类中基类属性的 protected 访问器时出错

转载 作者:行者123 更新时间:2023-12-01 01:12:34 25 4
gpt4 key购买 nike

 namespace PalleTech
{
public class Parent
{
private int test = 123;
public virtual int TestProperty
{
// Notice the accessor accessibility level.
set {
test = value;
}

// No access modifier is used here.
protected get { return test; }
}
}
public class Kid : Parent
{
private int test1 = 123;
public override int TestProperty
{
// Use the same accessibility level as in the overridden accessor.
set { test1 = value / 123; }

// Cannot use access modifier here.
protected get { return 0; }
}
}
public class Demo:Kid
{
public static void Main()
{
Kid k = new Kid();
Console.Write(k.TestProperty);

}
}
}

Error 1 Cannot access protected member 'PalleTech.Parent.TestProperty' via a qualifier of type 'PalleTech.Kid'; the qualifier must be of type 'PalleTech.Demo' (or derived from it)

最佳答案

From MSDN Article
“只有在通过派生类类型进行访问时,才能在派生类中访问基类的 protected 成员。”

在这里,您正在访问 Kid protected setter 及其实例。您必须创建 Demo 类的实例并通过它进行访问。

关于c# - 尝试访问派生类中基类属性的 protected 访问器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14500694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com