found: Unit required: Int - How to correct this?

后端 未结 3 841
萌比男神i
萌比男神i 2021-01-15 22:55
scala> class Rectangle (Length: Int, Width: Int) {; def getLlength (): Int = println (Length); def getWidth ():Int = println (Width); }
:11: error:         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-15 23:27

    The error message is telling you that println has the return type Unit, so since your getLlength method returns the result of println, it will return Unit. This is an error because you said that getLlength should have a return type of Int.

    If you want your getLlength method to print the length, you probably should change its return type to Unit (and change the name to something that makes this clear).

    If you want your getLlength method to return the length, your should not be calling println, but rather just return the length.

提交回复
热议问题