[英]Method inside anonymous inner class
public void run() {
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
byte[] buf = createBytesMessage(5120);
BytesMessage message = session.createBytesMessage();
message.writeBytes(buf);
message.setLongProperty("_publish_time", System.currentTimeMillis());
return message;
}
});
}
I have this code snippet, what I can make out is its using anonymous class. 我有此代码段,我可以确定的是使用匿名类。 But I am confused upon how the createMessage() method will be called when the run() is invoked by thread?
但是我对线程调用run()时如何调用createMessage()方法感到困惑吗?
Also somewhere I read there is nothing like "anonymous class" but instead its "anonymous inner class". 在我读过的某个地方,没有什么像“匿名类”,而是“匿名内部类”。 Why its like that?
为什么这样呢?
But I am confused upon how the createMessage() method will be called when the run() is invoked by thread?
但是我对线程调用run()时如何调用createMessage()方法感到困惑吗?
The jmsTemplate
will expect a MessageCreator
instance to be passed and it will responsible for calling the MessageCreator's createMessage()
method. jmsTemplate
将期望传递MessageCreator
实例,并将负责调用MessageCreator的createMessage()
方法。
Also somewhere I read there is nothing like "anonymous class" but instead its "anonymous inner class".
在我读过的某个地方,没有什么像“匿名类”,而是“匿名内部类”。 Why its like that?
为什么这样呢?
Yes. 是。 The proper definition is anonymous inner class , since there is no such thing as anonymous public class .
正确的定义是匿名内部类 ,因为不存在匿名公共类 。 While local classes are class declarations, anonymous classes are expressions, which means that you define the class in another expression and so they are called inner .
本地类是类声明,而匿名类是表达式,这意味着您需要在另一个表达式中定义该类,因此它们称为inner 。
More info: 更多信息:
It's an "Anonymous inner class" because it isn't a class that stands by itself, it's a class with some abstract method that you've declared within another class. 这是一个“匿名内部类”,因为它不是一个独立存在的类,它是具有在另一个类中声明的某种抽象方法的类。
As for the method itself, before I look up the class I'm assuming that the handler you send it to ( msTemplate
) just accepts an interface with the method createMessage
至于方法本身,在查找类之前,我假设您将其发送给(
msTemplate
)的处理程序仅接受带有createMessage
方法的接口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.