如何访问几个层次结构的字段? [英] How to access a field several classes up a hierarchy?

查看:73
本文介绍了如何访问几个层次结构的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用base.fieldname向上访问一个类的层次结构,但是如何访问比一个更高的类呢?例如,假设层次结构中有2个类.

I know how to access a class one hierarchy up using the base.fieldname but how do I access one higher up than just one? For example, let''s say 2 classes up the hierarchy.

推荐答案

层次结构中的距离绝对没有区别.一些间接基类的工作方式与直接基类完全相同.就像直接使用基类一样,直接访问您需要的内容.仅上下文和访问修饰符会影响访问.

—SA
The distance in hierarchy makes absolutely no difference. Some indirect base class works exactly as a direct base class. Just access what you need directly, as with a direct base class. Only the context and access modifiers effect the access.

—SA


如果更高级别的类将您想要的成员公开为私有成员以外的其他成员,则可以在与您直接从仅使用基本标识符继承的类.

例如,您具有从Class2继承的Class3,而Class2则继承自Class1.在Class1中,公开一个称为Test1的属性,在Class2中,公开一个称为Test2的属性.现在在Class3中,基本标识符可以同时使用Test1和Test2.
If the higher level classes expose the member you want as something other than private, it''ll be available to you at the same level as the class you''re directly inheriting from using only the base identifier.

For example, you have Class3 that inherits from Class2 that inherits from Class1. In Class1 you expose a property called Test1 and in Class2 you expose a property called Test2. Now in Class3, both Test1 and Test2 will be available from the base identifier.
public class MyBaseClass1
{

	protected string Test1 {
		get { return "Test1"; }
	}
}

public class MyBaseClass2 : MyBaseClass1
{

	protected string Test2 {
		get { return "Test2"; }
	}
}

public class MyBaseClass3 : MyBaseClass2
{

	protected string Test3 {
		get { return base.Test1 + base.Test2 + "Test3"; }
	}
}


这篇关于如何访问几个层次结构的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆