Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 新对象运行此方法的原因是什么,尽管它不是构造函数?_Java_Email - Fatal编程技术网

Java 新对象运行此方法的原因是什么,尽管它不是构造函数?

Java 新对象运行此方法的原因是什么,尽管它不是构造函数?,java,email,Java,Email,新对象(Authenticator auth=new-SMTPAuthenticator();)运行的是getPasswordAuthentication()方法,尽管它不是构造函数 通常,如果我们创建一个对象,它的构造函数会运行,但这个方法不是构造函数。 我真的被这段代码弄糊涂了。非常感谢您的帮助。这是构造函数链接。 构造函数链接是通过使用继承来实现的。子类构造函数方法的第一个任务是调用其超类的构造函数方法 首先运行基类的构造函数,然后只运行子类或派生类的构造函数 由于您已经扩展了类javax

新对象(
Authenticator auth=new-SMTPAuthenticator();
)运行的是
getPasswordAuthentication()
方法,尽管它不是构造函数

通常,如果我们创建一个对象,它的构造函数会运行,但这个方法不是构造函数。
我真的被这段代码弄糊涂了。非常感谢您的帮助。

这是构造函数链接。

构造函数链接是通过使用继承来实现的。子类构造函数方法的第一个任务是调用其超类的构造函数方法

首先运行基类的构造函数,然后只运行子类或派生类的构造函数


由于您已经扩展了类
javax.mail.Authenticator
,构造函数将自动从javax.mail.Authenticator的JavaDocs调用

需要密码身份验证时调用。子类应该 重写返回null的默认实现

当提供程序需要知道用户名或密码时,它会调用SMTPAuthenticatorAuthenticator子类中的getPasswordAuthentication()方法。这将返回PasswordAuthentication对象:

package com.robin;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import java.util.Properties;
public class main{
    private static final String SMTP_HOST_NAME = "";
    private static final String SMTP_AUTH_USER = "";
    private static final String SMTP_AUTH_PWD  = "";

    public static void main(String[] args) throws Exception{
        new main().test();
    }

    public void test() throws Exception{
        Properties props = new Properties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "2525");

        Authenticator auth = new SMTPAuthenticator();
        Session mailSession = Session.getDefaultInstance(props, auth);
        // uncomment for debugging infos to stdout
        // mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        message.setContent("This is a test", "text/plain");
        message.setFrom(new InternetAddress(""));
        message.addRecipient(Message.RecipientType.TO,
                new InternetAddress(""));

        transport.connect();
        transport.sendMessage(message,
                message.getRecipients(Message.RecipientType.TO));
        transport.close();
    }

    private class SMTPAuthenticator extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            return new PasswordAuthentication(username, password);
        }
    }
}

请尽我所能澄清您的问题,您混淆了静态方法和非静态方法
main
是类的名称,因此
new main()
创建一个新的
main
对象,以便可以调用非静态的成员方法
test
。在我看来,
test()
应该是一个静态方法,因此您可以将其称为
test()
main.test()
…而且您没有遵循类是大写的命名约定
main
看起来像一个变量(小写),但实际上它是一个类。好的,我将从现在开始学习。我刚刚练习了你所说的,现在我知道了。我写的代码:我了解到,创建新对象称之为超级类的构造函数。此构造函数正在调用getPasswordAuthentication()方法,该方法被newclass@user2536419,实际上,这并没有回答您的问题:是什么激发了getPasswordAuthentication()方法@实际上是这样的,因为他需要重写这个方法,否则它会像你说的那样返回null。这只是因为我在回答中提到的原因。请仔细阅读答案,并且仅限于comment@user2536419很高兴我能帮忙
@Override
protected PasswordAuthentication getPasswordAuthentication(){....}