为什么我无法访问子类中的受保护变量?

发布于 2024-11-24 12:47:37 字数 245 浏览 0 评论 0 原文

我有一个带有受保护变量的抽象类,

abstract class Beverage
{
        protected string description;

}

我无法从子类访问它。 Intellisense 不显示它可访问。为什么会这样呢?

class Espresso:Beverage
{
    //this.description ??
}

I have an abstract class with a protected variable

abstract class Beverage
{
        protected string description;

}

I can't access it from a subclass. Intellisense doesn't show it accessible. Why is that so?

class Espresso:Beverage
{
    //this.description ??
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲爱恨情仇 2024-12-01 12:47:37

简短回答:description 是一种特殊类型的变量,称为“MSDN 上的字段

长答案:您必须访问子类的构造函数、方法、属性等中的受保护字段。

class Subclass
{
    // These are field declarations. You can't say things like 'this.description = "foobar";' here.
    string foo;

    // Here is a method. You can access the protected field inside this method.
    private void DoSomething()
    {
        string bar = description;
    }
}

class 声明中,您声明该类的成员。这些可能是字段、属性、方法等。这些不是要执行的命令性语句。与方法内的代码不同,它们只是告诉编译器类的成员是什么。

在某些类成员(例如构造函数、方法和属性)内,您可以放置​​命令式代码。这是一个例子:

class Foo
{
    // Declaring fields. These just define the members of the class.
    string foo;
    int bar;

    // Declaring methods. The method declarations just define the members of the class, and the code inside them is only executed when the method is called.
    private void DoSomething()
    {
        // When you call DoSomething(), this code is executed.
    }
}

Short answer: description is a special type of variable called a "field". You may wish to read up on fields on MSDN.

Long answer: You must access the protected field in a constructor, method, property, etc. of the subclass.

class Subclass
{
    // These are field declarations. You can't say things like 'this.description = "foobar";' here.
    string foo;

    // Here is a method. You can access the protected field inside this method.
    private void DoSomething()
    {
        string bar = description;
    }
}

Inside a class declaration, you declare the members of the class. These may be fields, properties, methods, etc. These are not imperative statements to be executed. Unlike code inside a method, they simply tell the compiler what the members of the class are.

Inside certain class members, such as constructors, methods, and properties, is where you put your imperative code. Here is an example:

class Foo
{
    // Declaring fields. These just define the members of the class.
    string foo;
    int bar;

    // Declaring methods. The method declarations just define the members of the class, and the code inside them is only executed when the method is called.
    private void DoSomething()
    {
        // When you call DoSomething(), this code is executed.
    }
}
夢归不見 2024-12-01 12:47:37

您可以从方法内访问它。试试这个:

class Espresso : Beverage
{
    public void Test()
    {
        this.description = "sd";
    }
}

You can access it from within a method. Try this:

class Espresso : Beverage
{
    public void Test()
    {
        this.description = "sd";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文