C# 中的类层次结构

发布于 2024-08-16 03:28:26 字数 188 浏览 5 评论 0原文

我有这组课程:
节点(超级类)
|------ NodeType1(类)
|----------NodeType2(类)

type1 和 type2 有一些共同的字段(例如:NAME)。 如果我在 SUPER 类(节点)中声明 NAME 字段,我应该如何访问类型类中的这些变量?我怎样才能制作这些财产? 谢谢您的宝贵时间

I have this set of classes:
Node (SUPER Class)
|------ NodeType1 (Class)
|----------NodeType2 (Class)

There are fields that the type1 and type2 have in common (For example: NAME).
If i declare the NAME field in the SUPER class (Node) how should I access those variables in the types classes? How can i make those property's?
Thank you for your time

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

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

发布评论

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

评论(3

风铃鹿 2024-08-23 03:28:26
class Node
{
    public string Name { get; set; }
}

class NodeType1 : Node
{
    void SomeMethod()
    {
        string nm = base.Name;
    }
}

class NodeType2 : NodeType1
{
    void AnotherMethod()
    {
        string nm = base.Name;
    }
}
class Node
{
    public string Name { get; set; }
}

class NodeType1 : Node
{
    void SomeMethod()
    {
        string nm = base.Name;
    }
}

class NodeType2 : NodeType1
{
    void AnotherMethod()
    {
        string nm = base.Name;
    }
}
-残月青衣踏尘吟 2024-08-23 03:28:26

如果 name访问修饰符字段为 public受保护您将能够在您的派生类。修饰符public将使其对所有其他类可见,而protected将限制对派生类的可见性。

在任何一种情况下,您都可以像在当前类中声明的字段一样访问它:

this._name = "New Name";

如果您希望将其设为属性,则相应地设置它的访问修饰符:

public class Node
{
     protected string Name { get; set; }
}

If the access modifier of the name field is public or protected you will be able to access it in your derived classes. The modifier public will make it visible to all other classes, while protected will restrict visibility to the derived classes.

In either case you can just access it as you would a field declared in the current class:

this._name = "New Name";

If you wish to make it a property then set it's access modifier accordingly:

public class Node
{
     protected string Name { get; set; }
}
故乡的云 2024-08-23 03:28:26

您可以像通常访问该字段的方式一样访问该字段,例如输入 this.fieldName。不要忘记将此字段标记为 protected 以便在继承者中可见,或标记为 public 以便在继承者中和类外部都可见。

class Node
{
    protected string protectedName;
}

class NodeType1 : Node
{
    public string Name
    {
        get
        {
            return protectedName;
        }
    }
}

class NodeType2 : NodeType1
{
    protected void Foo()
    {
        string bar = Name;
    }
}

You can access this field just the way you usually get access to the field, typing this.fieldName, for instance. Don't forget to mark this field as protected to be visible in inheritors or public to be visible both in inheritors and from outside the class.

class Node
{
    protected string protectedName;
}

class NodeType1 : Node
{
    public string Name
    {
        get
        {
            return protectedName;
        }
    }
}

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