Getters 和 Setters、线程和 Java

发布于 2024-10-19 09:36:55 字数 198 浏览 3 评论 0原文

在学校里,他们教我们如何在 Java 中使用 getter 和 setter。最近我读到这样的东西很糟糕,而不是 OOP。好的,所以我可以编写一些代码,仅使用构造函数设置数据并返回所需的数据。

如何不在线程中使用吸气剂?当您执行线程时,它的类型始终为 void 并且 java 中有全局变量。 。 .. 那么,如何在没有 getter 方法的情况下从线程取回数据呢?

In school they taught us to use getters and setters in Java. Recently I've been reading such things are bad and not OOP. Ok, so I can make some code which only sets data by using the constructor and returns the required data.

How do you not use getters with threads? When you execute a thread it's type is always void and there are on global variables in java . . .. So how do you go about getting data back from a thread without a getter method?

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

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

发布评论

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

评论(3

时光是把杀猪刀 2024-10-26 09:36:55

最近我一直在读这些东西很糟糕,而不是 OOP。

恰恰相反,getter 和 setter 是 OOP 的基石之一(需要这种副作用)。

但是,您仍然可以在启动线程之前传递构造函数参数,例如,

new Thread(new MyRunnableObject(args)).start();

如果您希望它返回结果而不 getter,则最好实现线程执行的Callback例如,完成时。

Recently I've been reading such things are bad and not OOP.

Quite the opposite, getters and setters are one of the cornerstones of OOP (where such side effects are desired).

You can however still pass constructor arguments before starting a thread, e.g.,

new Thread(new MyRunnableObject(args)).start();

If you desire it to return a result without getters, you'd best implement a Callback which the thread executes e.g, on completion.

几度春秋 2024-10-26 09:36:55

getter 或 setter 本身并不是糟糕的面向对象设计。

不好的是编码实践,其中自动包含每个成员的 getter 和 setter,无论是否需要该 getter/setter(再加上使不应公开的成员公开) - 因为这基本上将类的实现暴露给外界违反信息隐藏/抽象。有时这是由 IDE 自动完成的,这意味着这种做法比预期的要广泛得多。

Getters or setters by themselves are not bad OO design.

What is bad is coding practice which includes a getter AND a setter for EVERY single member automatically, whether that getter/setter is needed or not (coupled with making members public which should not be public) - because this basically exposes class's implementation to outside world violating the information hiding/abstraction. Sometimes this is done automatically by IDE, which means such practice is significantly more widespread than it's hoped for.

薆情海 2024-10-26 09:36:55

最近我一直在读这样的东西很糟糕,而不是 OOP
谁说这样的废话?

Getters 和 Setters 确实是最佳实践。它们提供了一个属性访问层,因此当需要修改类中访问器的内部工作时,您可以进行单点更改。如果需要封装不同的访问策略,可以在子类中重写它们。

关于线程:

您应该使用诸如线程安全集合之类的东西,我想有一些类用于线程之间的通信。使用线程安全队列,一个线程写入,另一个线程读取。我敢打赌有一些很好的库可以做到这一点。你真的不需要全局变量。只需将相同的引用传递给需要跨线程通信的两个类即可。您还可以通过管道或 TCP 进行通信,但这更多地用于进程间通信。

Recently I've been reading such things are bad and not OOP
Who tells such crap?

Getters and Setters really are a best practice. They provide a layer for property-access so you have a single point of change when the internal workings of an accessor in a class need to be modified. You can override them in child-classes if you need to encapsulate different access strategies.

Concerning the threading:

You should use something like a threadsafe collection, I guess there are classes out there for the communication between threads. Use a threadsafe queue, which one thread writes to and the other reads from. I bet there are some good libraries for that out there. You don't really need globals. Just pass the same reference to both the classes that need to communicate across threads. You can also communicate via pipes or TCP, but that is more for inter-process communication.

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