简体   繁体   English

这应该是班级成员吗?

[英]Should this be a class member?

A class implements a public function that calculates some value. 类实现了一个计算某个值的公共函数。 During the calculation the function creates temp data that I also want to retrieve. 在计算过程中,该函数会创建我也想要检索的临时数据。

I could either add a class member that stores that data which I can get after the function has finished, or give the function another parameter so that the callee has to take care of it. 我可以添加一个类成员来存储我在函数完成后可以获得的数据,或者给函数另一个参数,以便被调用者必须处理它。 So it's basically 所以基本上就是这样

class A {
    TempData member;
    ...
    public Output function(Input input) {
        // calculate return value and save temp data to member
    }
}

vs.

class B {
    ...
    public Output function(Input input, TempData fillme) {
        // calculate return value and save temp data to fillme
    }
}

My problem: both versions look wrong to me. 我的问题:两个版本对我来说都不对。 The temp data doesn't really belong to class A and the function with the additional parameter in class B, I don't know... 临时数据实际上并不属于A类和B类附加参数的函数,我不知道......

Is there another solution? 还有其他解决方案吗? If not, which one of those two would you choose and why? 如果没有,你会选择哪两个?为什么?

It sounds like you are struggling with a multi-output function problem. 听起来你正在努力解决多输出功能问题。 The best solution is to create a separate class that represents the return value of this function and holds your Output and TempData objects. 最好的解决方案是创建一个单独的类来表示此函数的返回值,并保存Output和TempData对象。 Then you can cleanly return an instance of this class. 然后,您可以干净地返回此类的实例。

It's really hard, if not impossible, to say from this. 从这个角度说,如果不是不可能的话,真的很难。 You should ask yourself what the object is. 你应该问问自己对象是什么。

So is "TempData" some feature of your class A? 那么“TempData”是你班A的一些特色吗? If you have an object A, would you expect it to be able to tell you what "TempData" is? 如果你有一个对象A,你会期望它能告诉你“TempData”是什么吗? Then it should have that as a member. 那么它应该作为一个成员。 This has probably more to do with OO design then with your current situation, especially with classes called 'A' 这可能更多地与OO设计有关,然后与您当前的情况有关,特别是对于名为'A'的类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM