C# 中的类层次结构
我有这组课程:
节点(超级类)
|------ 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果
name
的访问修饰符字段为public
或受保护
您将能够在您的派生类。修饰符public
将使其对所有其他类可见,而protected
将限制对派生类的可见性。在任何一种情况下,您都可以像在当前类中声明的字段一样访问它:
如果您希望将其设为属性,则相应地设置它的访问修饰符:
If the access modifier of the
name
field ispublic
orprotected
you will be able to access it in your derived classes. The modifierpublic
will make it visible to all other classes, whileprotected
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:
If you wish to make it a property then set it's access modifier accordingly:
您可以像通常访问该字段的方式一样访问该字段,例如输入
this.fieldName
。不要忘记将此字段标记为protected
以便在继承者中可见,或标记为public
以便在继承者中和类外部都可见。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 asprotected
to be visible in inheritors orpublic
to be visible both in inheritors and from outside the class.