函数的类依赖 [英] Class Dependency of Functions

查看:67
本文介绍了函数的类依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

所以这就是问题,我有两个类,每个类都有各自的功能,比如说一类"和二类".

现在,第二讲说了一些我不希望人们编辑的功能,但是可以从第一讲使用,并且使用了第一讲的一些功能.

第一类是用户编写的类,但也使用第二类功能.

现在,在每个类的顶部,我互相声明一个新实例(那些已经学会了此实例的人将知道它的Cyclic,并且会抛出一些错误-Stack Errors).我知道我做错了事,我知道原因是事实,我创建了彼此的新实例,并且反过来继续创建永无止境的循环,直到编译器完成为止.

我的问题是,如何在不创建该循环的情况下让第一类使用第二类中的函数,反之亦然?

我的简单解决方案也是不得已的方法,是将所有功能从第一类复制到第二类.....但由于没有编辑第二类(我想保持不变)的人而迷失了.
谢谢

Hi people,

So here''s the thing, I have two classes each with their own functions Say Class One and Class Two.

Now Class Two Say has Functions that I don''t want people to edit, but is usable from Class One and uses some functions from Class One.

Class One is user written class but also uses Class Two functions.

Now at the top of each class I declare a new instance of each other ( those who picked up on this already will know its Cyclic and It''ll throw some errors - Stack Errors ). I know I''ve done something wrong and I know the cause is the FACT that I create new instances of each other and that In turn Keeps creating a Never-ending Loop until the Compiler has had enough.

My question is how can I have class One use functions from class two and vice versa without creating that loop.

My simple solution and last resort is to copy over all functions from Class One to Class two.... but losing on people not editing Class Two(which I want to remain constant) .....

Thanks

推荐答案

您最好的选择是创建第三类作为帮助者,并让Class1Class2使用Helper.
Your best option is to create a third class as a helper and have Class1 and Class2 use the Helper.


恕我直言,这里要问的关键问题是: Class1和Class2的实例在哪里/如何创建的?

如果它们都是由第三类创建的,那么有一个简单的解决方案.如果Class1创建Class2,则另一个解决方案.

假设Class2是由Class1创建的,还是由同时创建Class1的第三个类创建的:

将公共属性放入类型为"Class1"的Class2中,并在创建Class2实例时,将对Class1实例的引用插入其中:
imho the key question to ask here is: where/how are the instances of Class1 and Class2 created ?

If they are both created by a third class, there''s an easy solution. If Class1 creates Class2, another solution.

Assuming Class2 is either created by Class1, or created by a third class that also creates Class1:

Put a public property in Class2 of type ''Class1'' and when the instance of Class2 is created, insert the reference to the instance of Class1 into it:
// in Class1
public Class1 ReferenceToClass2 { get; set; }

// in Class2
public Class1 ReferenceToClass1 { get; set; }

// in the code where an instance of Class2 is created
// assume an instance of Class1, named MyClass1, has been created

Class2 MyClass2 = new Class2();

MyClass2.ReferenceToClass1 = MyClass1;

MyClass1.ReferenceToClass2 = MyClass2;


这是我对其他答案的补充:您的问题很简单:您想做一些不应该做的事情.这不是技术问题:不再想要您想要的东西,而将精力集中在最终目标上,问题将自动消失.您倾向于以一种非常危险的方式进行思考:您想要保留原始设计,并且在扔掉它并将其正确地解决实际问题时遇到问题,而不是仅仅存在于您的想象中的问题.更勇敢地这样做.

—SA
This is my addition to other answers: your problem is simple: you want to do something which you should not do. This is not a technical problem: stop wanting what you want and concentrate on ultimate goal, and the problem will disappear by itself. You tend to think in a very dangerous way: you want to preserve your original design and have problem throwing it away and put it right to solve the real problem, not the one which exists only in your imagination. Be more brave to do so.

—SA


这篇关于函数的类依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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