c#中如何访问父对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1940165/
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
How to access to the parent object in c#
提问by Jean Duprez
I have a "meter" class. One property of "meter" is another class called "production". I need to access to a property of meter class (power rating) from production class by reference. The powerRating is not known at the instantiation of Meter.
我有一个“仪表”类。“meter”的一个属性是另一个称为“production”的类。我需要通过引用从生产类访问仪表类(额定功率)的属性。在 Meter 实例化时不知道 powerRating。
How can I do that ?
我怎样才能做到这一点 ?
Thanks in advance
提前致谢
public class Meter
{
private int _powerRating = 0;
private Production _production;
public Meter()
{
_production = new Production();
}
}
采纳答案by Christian
Store a reference to the meter instance as a member in Production:
将仪表实例的引用存储为生产中的成员:
public class Production {
//The other members, properties etc...
private Meter m;
Production(Meter m) {
this.m = m;
}
}
And then in the Meter-class:
然后在 Meter 类中:
public class Meter
{
private int _powerRating = 0;
private Production _production;
public Meter()
{
_production = new Production(this);
}
}
Also note that you need to implement an accessor method/property so that the Production class can actually access the powerRating member of the Meter class.
另请注意,您需要实现访问器方法/属性,以便 Production 类可以实际访问 Meter 类的 powerRating 成员。
回答by bwarner
You would need to add a property to your Production class and set it to point back at its parent, this doesn't exist by default.
您需要向 Production 类添加一个属性并将其设置为指向其父类,默认情况下不存在。
回答by Tony The Lion
something like this:
像这样:
public int PowerRating
{
get { return base.PowerRating; } // if power inherits from meter...
}
回答by Rob Levine
Why not change the constructor on Production
to let you pass in a reference at construction time:
为什么不更改构造函数Production
以让您在构造时传递引用:
public class Meter
{
private int _powerRating = 0;
private Production _production;
public Meter()
{
_production = new Production(this);
}
}
In the Production
constructor you can assign this to a private field or a property. Then Production
will always have access to is parent.
在Production
构造函数中,您可以将其分配给私有字段或属性。然后Production
将始终可以访问 is parent。
回答by Gary Willoughby
You could maybe add a method to your Production object called 'SetPowerRating(int)' which sets a property in Production, and call this in your Meter object before using the property in the Production object?
您可以向名为“SetPowerRating(int)”的 Production 对象添加一个方法,该方法在 Production 中设置一个属性,并在使用 Production 对象中的属性之前在 Meter 对象中调用它?
回答by Jeroen
I wouldn't reference the parent directly in the child objects. In my opinion the childs shouldn't know anything about the parents. This will limits the flexibility!
我不会在子对象中直接引用父对象。在我看来,孩子不应该对父母一无所知。这将限制灵活性!
I would solve this with events/handlers.
我会用事件/处理程序解决这个问题。
public class Meter
{
private int _powerRating = 0;
private Production _production;
public Meter()
{
_production = new Production();
_production.OnRequestPowerRating += new Func<int>(delegate { return _powerRating; });
_production.DoSomething();
}
}
public class Production
{
protected int RequestPowerRating()
{
if (OnRequestPowerRating == null)
throw new Exception("OnRequestPowerRating handler is not assigned");
return OnRequestPowerRating();
}
public void DoSomething()
{
int powerRating = RequestPowerRating();
Debug.WriteLine("The parents powerrating is :" + powerRating);
}
public Func<int> OnRequestPowerRating;
}
In this case I solved it with the Func<> generic, but can be done with 'normal' functions. This why the child(Production) is totally independent from it's parent(Meter).
在这种情况下,我用 Func<> 泛型解决了它,但可以用“普通”函数来完成。这就是为什么孩子(生产)完全独立于它的父母(米)。
But! If there are too many events/handlers or you just want to pass a parent object, i would solve it with an interface:
但!如果事件/处理程序太多,或者你只想传递一个父对象,我会用一个接口来解决它:
public interface IMeter
{
int PowerRating { get; }
}
public class Meter : IMeter
{
private int _powerRating = 0;
private Production _production;
public Meter()
{
_production = new Production(this);
_production.DoSomething();
}
public int PowerRating { get { return _powerRating; } }
}
public class Production
{
private IMeter _meter;
public Production(IMeter meter)
{
_meter = meter;
}
public void DoSomething()
{
Debug.WriteLine("The parents powerrating is :" + _meter.PowerRating);
}
}
This looks pretty much the same as the solution mentions, but the interface could be defined in another assembly and can be implemented by more than 1 class.
这看起来与解决方案提到的几乎相同,但接口可以在另一个程序集中定义,并且可以由 1 个以上的类实现。
Regards, Jeroen van Langen.
问候, Jeroen van Langen。
回答by WannabeMe
I would give the parent an ID, and store the parentID in the child object, so that you can pull information about the parent as needed without creating a parent-owns-child/child-owns-parent loop.
我会给父一个 ID,并将 parentID 存储在子对象中,以便您可以根据需要提取有关父的信息,而无需创建 parent-owns-child/child-owns-parent 循环。