作品。传回一个变量

发布于 2024-11-01 07:21:38 字数 329 浏览 1 评论 0原文

假设你有两个类 A 和 B。如果它们之间的关系是 has-a

即 A has-a B,

你如何将信息从 B 传递到 A?例如,在 B 中进行计算并需要 A 中的答案。

除了将指向 A 类的指针传递给 B 类并调用将答案作为参数的函数之外,还有其他方法可以做到这一点吗?

希望这是有道理的,

医学博士。

抱歉,我应该

从下面的评论中引用更具体的内容。

“我举了一个简单的例子。我正在用 java 进行编程,我的 B 类运行一个新线程并将计算答案。因此我不能只从 A 类调用该函数,因为我不知道计算何时会进行完全的。”

Say you had two classes A and B. If the relationship between is has-a

i.e. A has-a B

how can you pass information from B into A? Say for example in B you work out a calculation and need the answer in A.

Is there any other way of doing this besides passing a pointer to class A into class B and calling a function which takes the answer as a parameter.

Hope this makes sense,

MD.

Sorry I should have been more specific

quote from my comment below.

"well I gave a simple example. I am programming this in java and my class B runs a new thread and and will calculate the answer. Therefore I cannot just call the function from class A as I don't know when the calculation will be completed."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的黑色迷你裙 2024-11-08 07:21:38

A 中需要计算结果的方法应该调用 B 中执行计算的方法。

这个答案是如此明显,以至于您可能没有告诉我们一些事情(?)

好吧,所以问题实际上是关于线程的。是的,然后传递对所有者对象的引用并回调它可能是一个好主意。更好的想法可能是返回一个封装计算结果的 future 对象

The method in A that needs the result of the computation should call into the method in B that does the computation.

This answer is so obvious that there may be something you're not telling us (?)

Ok, so the question is really about threading. Yes, then passing a reference to owner object and calling back into it may be a good idea. A better idea might be to return a future object that encapsulates the result of the computation.

萌面超妹 2024-11-08 07:21:38

如果我理解正确的话,你有这种关系:

class Car {
    Engine engine;

    int test() {
        int fuelLevel = engine.getFuelLevel();

        // do sth with fuel level, store it, use it etc.
   }
}

这个例子展示了如何在两个类之间传递信息:例如作为函数的结果。 Car 对象(您的 A 类)调用 Engine 对象(B 类)上的方法并 int 这样他就可以获得所需的信息。这可以很容易地转化为 B 类所做的任何类型的工作。

If I understand correctly, you have this kind of relationship:

class Car {
    Engine engine;

    int test() {
        int fuelLevel = engine.getFuelLevel();

        // do sth with fuel level, store it, use it etc.
   }
}

This example shows how can you pass information between the two classes: for instance as a result of function. Car object (your class A) calls a method on Engine object (class B) and int this way he obtains desired information. This can be easily translated to any kind of work that class B does.

看透却不说透 2024-11-08 07:21:38

基本上有两种方法来管理“异步”调用。
第一个是回调,另一个是轮询。
您所描述的就是回调。当 B 完成时,它需要以某种方式调用 A 来表明它已经完成。这可以通过将 A 的地址“提供”给 B 来完成,这样它就知道要调用什么,或者使用中间对象 C,它同步调用 B 并将结果发送回 A。然后 C 需要了解 A。

轮询是指 A 定期检查 B 是否已完成。这种解决方案通常在智力上不太令人满意,并且消耗更多的CPU。当 B 完成时,您也不会收到确切的通知。 (当B结束时,什么也没有发生,你必须等待下一次民意调查才能意识到这一点)。然而,这样,B 不需要知道任何关于 A 的信息。

我将使用第一个模式和中间对象(和特殊的类 C)。这样你的模型仍然是干净的(B 不需要知道 A 或 C)。我还建议您查看观察者模式

There are basically two ways to manage "asynchronous" calls.
The first is having a callback and the other polling.
Having a callback is what , you describe. When B has finished, it needs to call A somehow that it has finished. That can been done by "giving" the adress of A to B, so it knows what to call, or by using a intermediate object C, which calls B synchronously and send the result back to A. C then needs to know about A.

Polling is when A check regularly if B has finished. This solution is usually less satisfying intellectually and more CPU consuming. You are also not notified exactly when B finished. (When B finish, nothing happend, you'll have to wait for the next poll to be aware of it). However, that way , B doesn't need no know anything about A.

I would use the first pattern with an intermediate object (and special class C). So that your model is still clean (B doesn't need to know about A or C). I suggest also you have a look at the Observer pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文