我有两个模型对象/ DTO定义为数据类
public data class ModelA(val limit: Int, val offset: Int, val someDataA: DataAlpha)
public data class ModelB(val limit: Int, val offset: Int, val someDataB: DataBeta)
我想基于 Paginable 特征这样的内容来概括分页:
trait Paginable {
var limit: Int
var offset: Int
}
但是使ModelA实现Paginable会导致错误:
错误:'offset'隐藏超类'Paginable'的成员,需要'覆盖'修饰符
添加覆盖:
public data class ModelB(override val limit: Int, override val offset: Int, val someDataB: DataBeta) : Paginable
导致更有趣的错误,导致编译器崩溃:
Error:java.lang.ClassCastException: org.jetbrains.kotlin.psi.JetParameter cannot be cast to org.jetbrains.kotlin.psi.JetProperty
at org.jetbrains.kotlin.resolve.OverrideResolver$3.varOverriddenByVal(OverrideResolver.java:562)
at org.jetbrains.kotlin.resolve.OverrideResolver.checkOverridesForMemberMarkedOverride(OverrideResolver.java:606)
at org.jetbrains.kotlin.resolve.OverrideResolver.checkOverrideForMember(OverrideResolver.java:529)
at org.jetbrains.kotlin.resolve.OverrideResolver.checkOverridesInAClass(OverrideResolver.java:269)
at org.jetbrains.kotlin.resolve.OverrideResolver.checkOverrides(OverrideResolver.java:260)
at org.jetbrains.kotlin.resolve.OverrideResolver.check(OverrideResolver.java:67)
at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer.analyzeDeclarations(LazyTopDownAnalyzer.java:299)
at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzerForTopLevel.analyzeDeclarations(LazyTopDownAnalyzerForTopLevel.java:77)
at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzerForTopLevel.analyzeFiles(LazyTopDownAnalyzerForTopLevel.java:69)
at org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.java:147)
at org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegrationWithCustomContext(TopDownAnalyzerFacadeForJVM.java:100)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$2.invoke(KotlinToJVMBytecodeCompiler.java:307)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$2.invoke(KotlinToJVMBytecodeCompiler.java:300)
at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.java:232)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.java:299)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyzeAndGenerate(KotlinToJVMBytecodeCompiler.java:282)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileBunchOfSources(KotlinToJVMBytecodeCompiler.java:208)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.java:189)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.java:49)
at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.java:148)
at org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile.callCompiler(Tasks.kt:86)
at org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile.compile(Tasks.kt:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:63)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:218)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:211)
at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:200)
at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:579)
at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:562)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)
at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:42)
at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
我知道这个特性可以在类体内“手动”实现,但这会打破数据类,因为等于, hashCode 和复制将忽略添加的属性。
答案 0 :(得分:1)
您可以在两侧(val
和var
)使用相同的媒体资源类型trait
/ data class
)来修复代码。
答案 1 :(得分:0)
我一直想在Kotlin中实现多重继承,遇到了特质。但是后来发现,特征不再存在,并且一旦重命名为与Java 8语义匹配的接口,它们的语义就发生了显着变化。
关键字trait是Kotlin中的关键字,但现在已被删除。在Kotlin M12版本中已弃用。
您可以找到更多here。