Java中的双调度示例 [英] Double dispatch in Java example

查看:113
本文介绍了Java中的双调度示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 DD 上的Wikipedia文章,然后跳到" Java中的双重分发和示例"链接.下面的Serializable示例的描述对我来说似乎很混乱:

I was reading the Wikipedia article on DD and jumped over to the "Double dispatch in Java and an example" link given at the end. The description of the following Serializable example seems rather confusing to me:

A a = new A();
ObjectOutputStream oos = new ObjectOutputStream();
oos.writeObject( a);

这里是描述:

为了序列化A,ObjectOutputStream首先查看方法writeObject( ObjectOutputStream oos)是否存在.如果是这样,则它将自身作为参数调用该方法.然后,writeObject方法将调用分派回ObjectOutputStream(因此使其成为双重分派).在做这个非常简单的回叫时,ObjectOutputStream说:我将把状态写到该流上的责任委托给您".通过做出简单选择,ObjectOutputStream已将其自身与对象A分离.对象A依次说确定,这是我要写在流上的某些状态.如果该值是原始值,则可以通过重载write方法来处理它.如果没有,则可以在对象图中的下一层继续来回移动,直到所有有用的状态都放在流上为止.

In order to serialize A, ObjectOutputStream first looks to see if the method writeObject( ObjectOutputStream oos) exists. If it does, then it calls that method with itself as an argument. The writeObject method then dispatches the call back to the ObjectOutputStream (thus making this a double dispatch). In making this very simple call back, ObjectOutputStream has said: "I'm delegating back to you the responsibility of writing your state onto this stream". In making that simple choice, ObjectOutputStream has decoupled itself from our object A. Object A in turn says ok, here is some state that I want written on the stream. If the value is a primitive, then it can be dealt by the overloading of the write method. If not, the back and forth can continue at the next level down in the objects graph until all of the useful state has been placed on the stream.

我猜想该描述可能会造成混淆,因为所描述的是幕后发生的事情,而不是所呈现的代码,因为否则似乎没有多大意义.以下是让我感到困惑的部分:

I'm guessing that the description might be confusing because what is being described is what is happening behind the scenes and not the code presented because otherwise it doesn't seem to make much sense. Here are the parts that confuse me:

  • "ObjectOutputStream首先查看方法writeObject( ObjectOutputStream oos)是否存在".为什么ObjectOutputStream因为它是它自己的方法,所以需要检查此方法的存在?
  • 如果这样做,则将其自身作为参数调用该方法".在我看来,它使用A的实例作为参数来调用writeObject.回到上一项,如果用A的实例调用writeObject签名,为什么还要使用ObjectOutputStream arg查找writeObject签名?
  • 然后,writeObject方法将调用分派回ObjectOutputStream(因此将其作为双重分派)".同样,writeObject方法属于ObjectOutputStream类,因此我看不到它是如何分派回ObjectOutputStream"的,因为这似乎是原始目标.
  • "ObjectOutputStream first looks to see if the method writeObject( ObjectOutputStream oos) exists". Why would ObjectOutputStream need to check for the existence of this method since it is its own method?
  • "If it does, then it calls that method with itself as an argument". It looks to me like it's calling writeObject with an instance of A as an argument. Going back to the previous item, why would it look for the writeObject signature with an ObjectOutputStream arg if it's being called with an instance of A?
  • "The writeObject method then dispatches the call back to the ObjectOutputStream (thus making this a double dispatch)". Again, the writeObject method belongs to the ObjectOutputStream class so I fail to see how this gets "dispatched back to the ObjectOutputStream" since that appears to be the original destination.

我在这里只是想念一些基本的东西吗?或者这是一个写得不好/描述得不好的例子?如果这是一个不好的例子,我想将Wikipedia的文章更改为指向更好的文章,以便随时提供建议.

Am I just missing something fundamental here or is this a poorly written/described example? If it is a bad example, I'd like to change the Wikipedia article to point to a better one so feel free to provide suggestions.

谢谢.

推荐答案

要依次回答您的问题,

  1. ObjectOutputStream.writeObject(Object)(使用反射)检查其参数以确定该对象是否实现writeObject(ObjectOutputStream).也就是说,它或多或少地询问对象:您知道如何将结构写入ObjectOutputStream吗?"

  1. ObjectOutputStream.writeObject(Object) checks its argument (using reflection) to determine if the object implements writeObject(ObjectOutputStream). That is, it more or less asks the object, "Do you know how to write your structure to an ObjectOutputStream?"

如果答案为是",则ObjectOutputStream调用对象的 writeObject(ObjectOutputStream)方法,并将自身(ObjectOutputStream,而不是对象)作为参数传递.它基本上说:好.把你的结构写给我."

If the answer is "yes", then the ObjectOutputStream calls the object's writeObject(ObjectOutputStream) method, passing itself (the ObjectOutputStream, not the object) as the argument. It basically says, "Good. Write your structure to me."

对象中writeObject(ObjectOutputStream)的实现调用ObjectOutputStream来编写自身的元素.不应再次调用oos.writeObject(this).

The implementation of writeObject(ObjectOutputStream) in the object calls on the ObjectOutputStream to write elements of itself. It is not supposed to call oos.writeObject(this) again.

例如,假设我有一个像这样的对象:

For example, suppose I had an object like this:

class A {
    int x;
}

如果我想使它能够将自身写入ObjectOutputStream,则可以这样扩展它:

If I wanted to enable it to write itself to an ObjectOutputStream, I might expand it like this:

class A implements Serializable {
    int x;
    private void writeObject(ObjectOutputStream stream) throws IOException {
        stream.writeInt(x);
    }
}

然后输入此代码:

A a = new A();
ObjectOutputStream oos = . . .;
oos.writeObject(a);

将使用类A中的writeObject方法来编写a,而不是使用反射来编写A的所有(非瞬态)字段. )

would use the writeObject method in class A to write a, rather than using reflection to write all the (non-transient) fields of A. (In this case, however, there would be no difference.)

这篇关于Java中的双调度示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆