这是我的方法:
private static class AvatarNotFound
extends Throwable
{
public AvatarNotFound(String message, @NonNull AddressableAvatarView addressableAvatarView)
{
super(message + getErrorMessageSuffix(addressableAvatarView));
}
private static String getErrorMessageSuffix(@NonNull AddressableAvatarView addressableAvatarView)
{
return ". Addressable: " + addressableAvatarView.toString();
}
}
转换为Kotlin会返回此代码,该代码将变为:
private open class AvatarNotFound(message: String, addressableAvatarView: AddressableAvatarView) : Throwable(message + getErrorMessageSuffix(addressableAvatarView)) {
private fun getErrorMessageSuffix(addressableAvatarView: AddressableAvatarView): String {
return ". Addressable: $addressableAvatarView"
}
}
但是它说getErrorMessageSuffix
是一个未解决的引用吗?
如何让构造函数识别它?
答案 0 :(得分:1)
尝试一下可能会帮助您:
companion object{
private fun getErrorMessageSuffix(addressableAvatarView: AddressableAvatarView): String {
return ". Addressable: " + addressableAvatarView.toString()
}
}
}
答案 1 :(得分:0)
您可以在此处使用伴随对象,这将使您可以使用类名访问方法。
companion object
{
private fun getErrorMessageSuffix(addressableAvatarView: AddressableAvatarView): String
{
return ". Addressable: $addressableAvatarView"
}
}