如何调用 Creator 方法
如果我们有一个类
public class A
{
public void resetValues()
{
//DoSomething
}
B b = new B();
}
public class B
{
public B()
{
}
void doSomething()
{
//Is there a way here to call A(the Creator) method resetValues();
//Like Creator.resetValues();
}
}
,那么有没有一种方法可以调用 Creator 方法,就像这个示例中的类 A 方法的 . 当我使用一个表单来显示另一个表单时,这是非常有必要的: 来自 FormA
FormB frmB = new FormB();
frmB.Show();
this.hide();
我应该 onFormClose
FormB 事件再次显示 FormA
编辑 首先我认为将 Ref 解析为对象,但后来将引用存储为字段我发现这是不可能的!
首先我想也许使用反射我们可以识别并调用 Creator 方法,但我认为我不匹配一些OOP 设计模式
If we have a Class
public class A
{
public void resetValues()
{
//DoSomething
}
B b = new B();
}
public class B
{
public B()
{
}
void doSomething()
{
//Is there a way here to call A(the Creator) method resetValues();
//Like Creator.resetValues();
}
}
So is there a way to call the Creator methods like in this Example Class A method's .
This is is very needful when i use a Form to show Another Form :
from FormA
FormB frmB = new FormB();
frmB.Show();
this.hide();
Than i should onFormClose
Event of FormB to Show again the FormA
EDIT
First i thought parsing A by Ref as Object ,but Storing a Reference as a Field later i founded out that's impossible !
First i thought maybe using reflection we can Identify and Call Creator method's but i think i mismatched some of OOP Design Pattern's
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
唯一的方法是在调用构造函数时传入“creator”:
当然,这意味着
B
需要有一个具有适当参数类型的构造函数,该构造函数应该与它需要执行的任何操作相匹配它的创造者。编辑:正如评论中所指出的,它实际上并不需要在构造时完成 - 它可能是一个属性:
但它们基本上是相同的东西。
老实说,我认为这通常是一个坏主意。它在两个类之间引入了相当紧密的耦合。对于
B
来说,发布一个创建者可以订阅的事件可能会更好,以便处理B
上适当的状态更改等。The only way is to pass in the "creator" when calling the constructor:
Of course, that means that
B
needs to have a constructor with the appropriate parameter type, which should match whatever it needs to do with its creator.EDIT: As noted in comments, it doesn't really need to be done at construction - it could be a property:
They amount to basically the same thing though.
I would suggest this is usually a bad idea, to be honest. It's introducing a reasonably tight coupling between the two classes. It might be better for
B
to publish an event which the creator can subscribe to, in order to handle appropriate state changes etc onB
.继 @Jon Skeet 的 答案,B类最好在发生事件时发出事件,而A类则更可取消耗该事件。
这样,类 B 就不再依赖于类 A。您可以将类 B 与类 A 以外的类重用,而无需修改类 B 的内部结构。从维护的角度来看,这是更可取的。
您可以按如下方式设置:
和
Following on from @Jon Skeet's answer, it would be preferable for class B to emit an event when something happens and for class A to consume that event.
In this way, class B has no dependency on class A. You could reuse class B with classes other than class A without modifying the internals of class B. From a maintenance POV, this is far more preferable.
You could set it up as follows:
and
您可以简单地使用
Action
代替Func
,返回 bool 允许传回指示操作是否成功执行的状态。You can use simply
Action
insteadFunc<bool>
, returning bool allows passing back a state which indicating whether operation was executed successfully.