C# 嵌套类访问父成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1105955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Nested Class Access Parent Member
提问by Rob
Is it possible to access a parent member in a child class...
是否可以访问子类中的父成员...
class MainClass {
class A { Whatever }
class B {
List<A> SubSetList;
public void AddNewItem(A NewItem) {
Check MasterListHere ????
}
}
List<A> MasterList;
}
So... my main class will have a master list. It will also have a bunch of instances of B. In each instance of B, I want to add new A's to the particular B, but only if they exist in the Master List. I toyed with making the MasterList static and it works ... until I have more than one instance of MainClass... which I will have.
所以......我的主班会有一个大师名单。它还会有一堆 B 的实例。在 B 的每个实例中,我想将新的 A 添加到特定的 B,但前提是它们存在于主列表中。我玩弄使 MasterList 静态并且它可以工作......直到我有不止一个 MainClass 实例......我将拥有它。
I could pass a reference to MasterList to each instance of B, but I will eventually have multiple of these "MasterLists" and i don't want to have to pass lots of references if i don't have to.
我可以将 MasterList 的引用传递给 B 的每个实例,但我最终将拥有多个这些“MasterLists”,如果不需要,我不想传递大量引用。
采纳答案by Alex
You can use something like this:
你可以使用这样的东西:
class B {
private MainClass instance;
public B(MainClass instance)
{
this.instance = instance;
}
List SubSetList;
public void AddNewItem(A NewItem) {
Check MasterListHere ????
}
}
回答by Steven Sudit
Regardless of whether you nest class B inside class A, any given instance of A will still need to know about the instance of B that it is held by. So you might want to initialize A with a reference to B and keep it in a field. This likely includes un-nesting it.
不管你是否在类 A 中嵌套类 B,任何给定的 A 实例仍然需要知道它所持有的 B 的实例。因此,您可能希望使用对 B 的引用来初始化 A 并将其保存在一个字段中。这可能包括取消嵌套它。
回答by Steve Guidi
With your definition, instances of class B
may access the private methods and static fields of class MainClass
.
根据您的定义, 的实例class B
可以访问 的私有方法和静态字段class MainClass
。
回答by Jeremy
Yes, but you would need to have a reference to the instance of MainClass from within you instance of B.
是的,但是您需要从 B 的实例中引用 MainClass 的实例。
Have you thought of re-working your classes a little bit? Istead of having the AddNewItem method in B, you could have it in MainClass. That way it would be easy to check the MasterList from within MainClass.
你有没有想过重新学习一下你的课程?您可以在 MainClass 中使用它,而不是在 B 中使用 AddNewItem 方法。这样就可以很容易地从 MainClass 中检查 MasterList。
回答by Joseph
This might work for you:
这可能对你有用:
public void AddNewItem(A newItem, Func<A, bool> checkForItemInMasterList)
{
if (checkForItemInMasterList.Invoke(newItem)
SubList.Add(newItem);
}
You would then have something in your MainClass to use the method like this:
然后你会在你的 MainClass 中有一些东西来使用这样的方法:
public void AddItem(A newItem)
{
new B().AddNewItem(newItem, x => MasterList.Contains(x));
}
To summarize it might look something like this:
总而言之,它可能看起来像这样:
class MainClass {
class A { Whatever }
class B {
List<A> SubSetList;
public void AddNewItem(A newItem, Func<A, bool> checkForItemInMasterList)
{
if (checkForItemInMasterList.Invoke(newItem)
SubList.Add(newItem);
}
}
//I don't know how you're adding items to instances of B.
//This is purely speculative.
public void AddItem(A newItem)
{
new B().AddNewItem(newItem, x => MasterList.Contains(x));
}
List<A> MasterList;
}
回答by Mircea Grelus
In C# there is actually no implicit reference to the instance of the enclosing class, so you need to pass such a reference, and a typical way of doing this is through the nested class' constructor.
在 C# 中,实际上没有对封闭类的实例的隐式引用,因此您需要传递这样的引用,而这样做的典型方法是通过嵌套类的构造函数。