object Scala { def main( args : Array[ String ] ) : Unit = { val p = new Point( 1, 2 ); println( p ); p.move( 12, 13 ); println( p ); val p2 = new Point println( p2 ) val p3 = new Point( y = 2 ) println( p3 ) val point4 = new Point2 point4.x = 99 point4.x_=( 98 ); point4.y = 101 // prints the warning println( point4 ) /** * * <pre> * Primary constructor parameters with val and var are public. * However, because vals are immutable, * you can’t write the following. * class Point(val x: Int, val y: Int) * val point = new Point(1, 2) * point.x = 3 // <-- does not compile * </pre> * <pre> * 首要构造器上参数都是public.如果没有明确写出是var x:Int,默认是val * val是不可变的,所以如上代码不能通过编译 * </pre> */ } } class Point( var y : Int = 0, var x : Int = 0 ) { def move( dy : Int, dx : Int ) : Unit = { this.x = x; this.y = y; } override def toString() : String = s"($x,$y)" } class Point2 { private var _x = 0; private var _y = 0; private var bound = 100; //define method x for access _x def x = _x; /** * <pre> * def x_= and def y_= are for validating and setting * the value of _x and _y. * Notice the special syntax for the setters: * the method has _= appended to the identifier of the * getter and the parameters come after * </pre> * <pre> * def x_=和def y_=为了检验设置的值.注意setters方法上特殊的语法. * </pre> */ def x_=( newValue : Int ) : Unit = { if ( newValue < bound ) _x = newValue else printWarning } //define method y for access _y def y = _y; def y_=( newValue : Int ) : Unit = { if ( newValue < bound ) _x = newValue else printWarning } private def printWarning = println( "WARNING: Out of bounds" ); override def toString() : String = s"($x,$y)" }
下面这份代码更能说明class的setter和getter
object Scala { def main( args : Array[ String ] ) : Unit = { val point4 = new Point point4.xx = 99 point4.y = 101 // prints the warning println( point4 ) } } class Point { private var _x = 0; private var _y = 0; private var bound = 100; //define method x for access _x def xx = _x; /** * <pre> * def x_= and def y_= are for validating and setting * the value of _x and _y. * Notice the special syntax for the setters: * the method has _= appended to the identifier of the * getter and the parameters come after * </pre> * <pre> * def x_=和def y_=为了检验设置的值.注意setters方法上特殊的语法. * </pre> */ def xx_=( newValue : Int ) : Unit = { if ( newValue < bound ) _x = newValue else printWarning } //define method y for access _y def y = _y; def y_=( newValue : Int ) : Unit = { if ( newValue < bound ) _x = newValue else printWarning } private def printWarning = println( "WARNING: Out of bounds" ); override def toString() : String = s"($xx,$y)" }
多个构造函数
object Scala { def main( args : Array[ String ] ) : Unit = { val point4 = new Point point4.xx = 99 point4.y = 101 // prints the warning println( point4 ) val point5 = new Point( 50 ) point5.y = 101 // prints the warning println( point5 ) } } class Point { private var _x = 0; private var ny = 0; private var bound = 100; def this( xxx : Int ) { this() xx = xxx; println("here") } //define method x for access _x def xx = _x; /** * <pre> * def x_= and def y_= are for validating and setting * the value of _x and _y. * Notice the special syntax for the setters: * the method has _= appended to the identifier of the * getter and the parameters come after * </pre> * <pre> * def x_=和def y_=为了检验设置的值.注意setters方法上特殊的语法. * </pre> */ def xx_=( newValue : Int ) : Unit = { if ( newValue < bound ) _x = newValue else printWarning } //define method y for access _y def y = ny; def y_=( newValue : Int ) : Unit = { if ( newValue < bound ) _x = newValue else printWarning } private def printWarning = println( "WARNING: Out of bounds" ); override def toString() : String = s"($xx,$y)" }