How do I expose Scala constructor arguments as public members?

前端 未结 2 792

Look at this example:

class Point(x: Double, y: Double){
  override def toString = \"x: \" + x + \", y: \" + y
  def +(sourcePoint: Point) : Point = {
    re         


        
相关标签:
2条回答
  • 2020-12-17 08:02

    Yes, there is:

    class Point(val x: Int, val y: Int)
    
    0 讨论(0)
  • 2020-12-17 08:05

    using val is valid but then the parameter becomes final (constant). If you want to be able to reassign the value you should use var instead. So

    class Point(var x: Int, var y: Int)
    
    0 讨论(0)
提交回复
热议问题