java中如何绑定两个类?
如何将两个 Java 类绑定在一起?
我正在编写一个 3D opengl 程序,其中有几个不同的类。我知道如何将类绑定到主类,但我无法将两个单独的类绑定在一起。我有一个地形类,其中包含一些有关地形的数据的列表。然后我需要另一个名为Figure 的类中的数据,该类不是主类。我尝试将这些类绑定在一起,如下所示:
在 Terrain 类中,我有:
Figure fig;
public void bindClasses(Figure fg) {
fig = fg;
}
然后在 Figure 类中,我有:
Terrain ter;
public void bindTerrain(Terrain tr) {
ter = tr;
}
然后在这两个类中我调用这些函数。这不应该绑定它们和它们的变量吗?至少我是这样将我的班级与主班级联系起来的。
How can I bind together two Java classes?
I'm programming a 3D opengl program where I have several different classes. I know how to bind classes to the main class, but I can't bind two separate classes together. I have a class Terrain where I have a list containing some data about the terrain. Then I need that data in another class called Figure which isn't the main class. I have tried to bind these classes together like this:
In Terrain class I have:
Figure fig;
public void bindClasses(Figure fg) {
fig = fg;
}
And then in the Figure class I have:
Terrain ter;
public void bindTerrain(Terrain tr) {
ter = tr;
}
And then in both classes I call those functions. Shouldn't that bind them and their variables? At least that's how I have bound my classes with the main class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是从术语开始。 类是一个蓝图,它告诉您实例化的对象是如何的 - 当您编写
new Figure()
时,您就创建了一个实例,类图形
的一个对象(通常您有一个类的多个对象 >)。因此,当您在上面“绑定”时,您实际上并不是在绑定类,而是在绑定对象。上面的代码没问题。按照惯例,您经常使用 setter 编写此类内容,没有必要这样称呼它们,但一个非常常见的模式是:
要将两者关联在一起,您可以在主类中执行此操作,其中,正如您所看到的,它几乎与您已经拥有的一样,只是使用常规名称。
例如,如果两个对象之间存在关系,则如果没有地形来放置图形,则无法创建图形。这将使地形几乎“拥有”图形。您可以使用图中的构造函数来指示这一点。
现在您的初始化代码将改为:
Just to start off with terminology. A class is a blueprint that tells you how an instantiated object is - the moment you write
new Figure()
, you've created an instance, an object of the classFigure
(often you have several objects of one class). So when you are "binding" above, you are not actually binding classes, you are binding objects.The above code is fine. By convention, you often write that sort of things with setters, it's not necessary to call them that, but a very common pattern is:
To associate the two together you would in your main class do, which, as you see, is pretty much exactly what you would have already, just using the conventional names.
If there's a relationship between the two objects, for instance, you can't create a figure without having a terrain to put it in. Which would make terrain almost "own" figure. You could indicate this by using the constructor on figure.
Now your init code would instead look:
像您这样的双向绑定是可能的,但从设计角度和技术角度来看都是“棘手的”。
实现它的一种方法是:
现在我建议您仔细考虑是否需要类之间的这种链接?
通常,拥有一个包含对象(图形)列表的通用对象(地形)会更简单、更实用。然后通用对象询问/更新列表(例如:figure.hasRecievedEventAttack(attack) 或figure.whatDoYouWantToDo()),然后通用对象(terrain)分析答案
two way binding like yours is possible but is "tricky" from a design points view as well as technical.
a way to achieve it is:
now I would recommend you think carefully if you need this kind of linkage between the classes ?
usually it is simpler and more practical to have a general object (terrain) that contains a list of objects(figures). then the general object interogates/updates the list (for examples: figure.hasRecievedEventAttack(attack) or figure.whatDoYouWantToDo()) and then the general object(terrain) analyses a answer
对于这种情况,您可以使用控制反转 (IoC) 设计模式。
一个例子是 依赖注入,它是 IoC 的特定实现。
使用这种模式,您可以避免耦合在 Terrain 和 Figure 类之间。 (地形取决于图形,图形取决于地形)。
简而言之,您有一个容器,它将负责“注入”每个对象所需的属性,因此您不必对其进行硬编码。
Spring 是一个执行此操作的容器。
For this situation you can use Inversion of Control (IoC) design pattern.
An example is Dependency Injection which is a specific implementation of IoC
With this pattern you can avoid the coupling you have between your Terrain and Figure class. (Terrain depends on Figure and Figure depends on Terrain).
In few words you have a Container that will take care of "injecting" the attributes needed for each object so you don't have to hardcode it.
Spring is a container that does this.