我想问一下,这两个页面(界面上的页面或可见性修饰符上的页面)都没有提供我所追求的语法示例。
我有一个像这样的界面:
package bookyard.contracts;
public interface IAuthenticationManager<T> {
fun authenticateUser(userName: String, password : String,
appId: String, appSecret : String) : OperationResult<T>;
}
我正在为此编写一个实现,但是下面列出的三种语法都没有用。每次,编译器都会报告以下错误:
'authenticateUser'不会覆盖任何内容
类'DatabaseAuthenticationManager'必须声明为抽象或实现抽象成员public abstract fun authenticateUser(userName:String,password:String,appId:String,appSecret:String):在bookyard.contracts.IAuthenticationManager中定义的OperationResult
package bookyard.server.util
import bookyard.contracts.IAuthenticationManager;
public class DatabaseAuthenticationManager : IAuthenticationManager<User> {
/* override public fun authenticateUser(userName : String?,
password : String?,
appId : String?,
appSecret : String?) : OperationResult<User> {
}
public override fun authenticateUser(userName : String?,
password : String?,
appId : String?,
appSecret : String?) : OperationResult<User> {
}
public fun authenticateUser(userName : String?,
password : String?,
appId : String?,
appSecret : String?) : OperationResult<User> {
} */
}
您的问题是,在尝试实现接口时,您正在使用可为空的字符串。
在Kotlin中,类型系统区分可以保存null的引用(可为空的引用)和不能保存null的引用(非null引用)。
String?
(可为null的字符串),并且KotlinString
认为它们是不同的类型。因此,编译器认为您尚未实现该方法。
您有2个选择:
更新您的接口以使用可为空的参数(通过?
在每种类型的末尾添加)。
更新您的实现以使用不可为空的参数(String
)。
我认为选项2稍微干净一点,因为它不会破坏该接口的任何其他现有实现。
接口保持不变。
这是新的实现...
package bookyard.server.util
import bookyard.contracts.IAuthenticationManager
class DatabaseAuthenticationManager : IAuthenticationManager<User> {
override fun authenticateUser(userName : String,
password : String,
appId : String,
appSecret : String) : OperationResult<User> {
// Your implementation...
}
}
您可以看到我所做的就是将每个参数从更改String?
为String
。
默认情况下,类/字段/接口是公共的,public
不需要关键字。
Kotlin具有分号推断,因此您无需自己添加分号。在极少数情况下,您实际上需要这样做,但是编译器会提前警告您。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句