将getPasswordAuthentication功能转换为Kotlin

问题描述 投票:0回答:1

我想将此代码块从Java转换为Kotlin:

 Session session = Session.getInstance(props,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("username", "password");
            }
          })

但是我无法适当地转换此部分:

 protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("username", "password")
            }

如何进行对话?

java kotlin
1个回答
0
投票

这是一个匿名的类实例。检查object expressions

object : Authenticator() {
    override fun getPasswordAuthentication() : PasswordAuthentication {
        return PasswordAuthentication("username", "password")
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.