当我运行以下代码时,我得到一个警告“readpage”,它不会覆盖第42行中的任何内容,即类override fun readpage()中的eBook。那是为什么,我该怎么解决这个问题?我正在学习科特林和跟踪一个udacity教程。两个星期以来,我一直试图自己解决这个问题,并花了10+时间,但仍然不知道为什么?
package Aquarium
fun main(args: Array<String> ) {
delegation()
}
fun delegation(){
val pleco=Plecotomus()
println("Fish has color ${pleco.color}")
pleco.eat()
}
interface FishAction{
fun eat()
}
interface FishColor{
val color:String
}
class Plecotomus(fishcolor:FishColor=GoldColor):
FishAction by PrintingFishAction(food="eat munch algae"),
FishColor by fishcolor
object GoldColor:FishColor{
override val color="gold"
}
object RedColor:FishColor{
override val color="red"
}
class PrintingFishAction(val food:String):FishAction{
override fun eat(){
println(food)
}
}
open class Book(val title:String, val author:String){
private var currentPage=1
open fun readPage(){
currentPage++
}
}
class eBook(title:String,author:String,var format:String="text"){
private var wordsCount=0
override fun readPage(){
wordsCount=wordsCount+250
}
}发布于 2021-05-10 08:23:27
add :Book(title,author)之后的类eBook() =>类eBook() :Book(title,author){}将解决这个问题。
类eBook(标题:字符串,作者:String,var format:String=“text”):Book(标题,作者){私有var wordsCount=0覆盖乐趣readPage(){ wordsCount=wordsCount+250} }
发布于 2021-05-08 13:04:17
这门课
class eBook(val title:String, val author:String, val format:String="text"){
private var wordsCount=0
override fun readPage() {
wordsCount=wordsCount+250
}
}不会继承任何东西。但是您正在尝试override函数readPage。由于没有什么可重写的,所以编译器会给出错误。
“readPage”没有覆盖任何东西
请删除关键字override,您将能够编译代码。
class eBook(title:String,author:String,var format:String="text"){
private var wordsCount=0
fun readPage(){
wordsCount=wordsCount+250
}
}注意事项:函数eBook不在任何地方使用。但是当你在学习代码的时候,花点时间去探索和犯错误吧。
https://stackoverflow.com/questions/67445541
复制相似问题