本文介绍了为什么 JavaMail Transport.send() 是一个静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在修改不是我编写的使用 JavaMail 的代码,并且在理解 JavaMail API 的设计方式时遇到了一些麻烦.我有一种感觉,如果我理解了,我可以做得更好.

I'm revising code I did not write that uses JavaMail, and having a little trouble understanding why the JavaMail API is designed the way it is. I have the feeling that if I understood, I could be doing a better job.

我们称:

transport = session.getTransport("smtp");
transport.connect(hostName, port, user, password);

那么为什么 Eclipse 会警告我:

So why is Eclipse warning me that this:

transport.send(message, message.getAllRecipients());

是对静态方法的调用吗?

is a call to a static method?

如果我不能使用该对象来发送消息,为什么我会得到一个 Transport 对象并提供特定于它的设置?Transport 类如何知道使用什么服务器和其他设置来发送消息?它运行良好,这令人难以置信.如果我为两个不同的服务器实例化了 Transport 对象会怎样?它怎么知道使用哪个?

Why am I getting a Transport object and providing settings that are specific to it if I can't use that object to send the message? How does the Transport class even know what server and other settings to use to send the message? It's working fine, which is hard to believe. What if I had instantiated Transport objects for two different servers; how would it know which one to use?

在写这个问题的过程中,我发现我真的应该打电话:

In the course of writing this question, I've discovered that I should really be calling:

transport.sendMessage(message, message.getAllRecipients());

那么静态 Transport.send() 方法的目的是什么?这只是糟糕的设计,还是有这样的原因?

So what is the purpose of the static Transport.send() method? Is this just poor design, or is there a reason it is this way?

推荐答案

Transport.send() 基本上是一种方便的方法.是的,如果您正在管理自己的 Transport 实例,请调用 sendMessage().

Transport.send() is basically a convenience method. Yes, if you're managing your own Transport instance, call sendMessage().

我不确定我是否认为它是糟糕的设计,因为您经常不关心管理发送和监控传输的细节.打开 send() 方法,看看它为你做了什么.命名或放置此方法的其他方式可能会稍微好一些.

I'm not sure I consider it bad design, since frequently you don't care to manage the details of sending and monitoring transport. Crack open the send() method to see what it does for you. Other ways of naming or placing this method could be marginally better.

这篇关于为什么 JavaMail Transport.send() 是一个静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 21:48