使用类或不使用类
我有一个关于是否应该使用类来解决这个问题的问题。这是我的场景:
我将每秒接收包含 2 组数据的数据包。数据A和数据B。获取数据后,我需要为该数据添加时间戳。我需要对数据进行计算并保留答案。我正在考虑为我收到的每个数据包使用一个类。并让该类成为时间戳和我需要的所有其他内容的成员。
这是个好主意吗?有更好的办法吗?
我是java新手,我来自c++,在使用完这些类后我是否需要解构它们,或者java会为我做这件事吗?当我用完该物体后,我该如何摆脱它?
提前致谢!
I have a question about whether I should use a class for this problem or not. Here is my scenario:
I am going to be receiving data packets every second containing 2 sets of data. dataA and dataB. Once I get the data, I need to timestamp that data. I need to perform calculations on the data and keep the answers. I am thinking about using a class for each data packet that comes to me. And have that class a member for timestamp and all the other stuff I need.
Is this a good idea? Is there a better way?
I am new to java, I come from c++, do I need to deconstruct these classes after I am finished using them or does java do that for me? Once I am done with the object, how do I get rid of it?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对我来说当然是有效的。您将获取原始数据,并将其包装在包含它和有关它的元数据信息的类中。在Java中,你必须有一个类来存储代码,并且由于你想封装所有这些东西,我认为这是一个很好的设计决策。
至于必须自己销毁该类,通常不会。 Java VM 有一个垃圾收集器,可以搜索并销毁未引用的对象。对于“托管”对象(完全存在于程序内存“沙箱”内的对象),它通常非常有效。
Certainly seems valid to me. You're getting primitive data, and wrapping it in a class that contains it and metadata information about it. In Java, you HAVE to have a class to store code, and since you want to encapsulate all this stuff I say it's a good design decision.
As for having to destroy the class yourself, usually no. The Java VM has a garbage collector that searches for and destroys unreferenced objects. It usually does so pretty efficiently in the case of "managed" objects (objects that live totally within the program's memory "sandbox").
嗯,在 Java 中几乎都是类。
是的,将包数据及其所有操作建模为一个类听起来很合适。
不,您不必手动解构对象,当没有任何东西再引用它们时*,它们会被垃圾收集器收集(最终)。
*实际规则比这更复杂一些,任何无法从活动线程访问的对象都被视为有资格进行垃圾收集。这意味着您不必担心对象之间的循环引用。
Well, In Java it's pretty much all classes.
Yes, it sounds appropiate to model your package data and all the operations on it as a class.
And no, you don't have to deconstruct objects manually, when nothing references them anymore*, they are collected by the garbage collector (eventually).
*The actual rules are a bit more complicated than that, any object that isn't reachable from an active thread is considered as eligible for garbage collection. This means that you don't have to worry about circular references between your objects.
创建您自己的类。这就是 Java 的方式。
除非该类是私有内部类,否则您应该使用私有字段和 getter/setter 方法来定义它以访问和更新它们。或者,如果您不需要更新字段,则只需更新 setter 方法。 (从 C/C++ 的角度来看,这可能听起来有点沉重,但从长远来看,getter 和 setter 往往会带来回报,而且 JIT 编译器通常会内联调用。)
您不应该担心内存管理。 Java 垃圾收集器将自动删除任何无法访问的实例。在大多数情况下,最好让 GC 来处理问题。
还有其他选择(虽然不是很好,IMO):
使用通用映射类型(例如
HashMap
或Properties
)将对象表示为名称值对。从很多角度来看,这是一个糟糕的选择:性能很差,您没有明确的 API(对于您的“对象”),并且您的代码更脆弱。使用数组或列表类型。这更糟糕,因为您甚至没有字段的“名称”。
根本不具体化数据包,而是单独传递位。呃...
Create your own class. That's the Java way.
Unless the class is a private inner class, you should define it with private fields and getter / setter methods to access and update them. Or if you don't need to update the fields, only setter methods. (This may sound a bit heavy-weight from a C/C++ perspective, but getters and setters tend to pay off in the long term, and the JIT compiler typically inlines the calls anyway.)
You shouldn't worry about memory management. The Java garbage collector will automatically get rid of any instances that become inaccessible. In most situations, it is best to just let the GC handle the problem.
There are alternatives (though not good ones, IMO):
Use a generic map type such as
HashMap
orProperties
to represent the objects as name value pairs. This is a bad choice from lots of standpoints: performance is poor, you don't have a definite API (for your "objects"), and your code is more fragile.Use an array or list type. This is even worse, because you don't even have "names" for the fields.
Don't reify the data packets at all, but pass the bits around separately. Ughh ...
您可以使用不可变类。恕我直言,这是对远程数据进行建模的更好方法。一旦您完成了它们,如果您取消引用它们,垃圾收集器将为您释放内存。
祝你工作顺利。
You can use immutable class. IMHO it is the better way to model remote data. Ince you finished with them, if you dereference them, the garbage collector will free the memory for you.
Have a good job.