0601.類別(Classes)

Classes

基本結構

  • 預設的 classes, fields, methods 存取權皆為 public
  • 建構子/方法 若不需傳遞參數, 則可省略( )
  • 在宣告 class 可一並顯示在類別名稱後的( )中顯式宣告建構函數, 並依給於的前置詞以 compiled 出相對的 getter / setter.
    • val
      • private final [type] getter/setter
      • public [method] // getter
    • var
      •  private [type] fieldName
      • public [method] // getter / setter
    • [none]
      • private field
      • private method
  • 預設經由 compiler 產出的 getter / setter 並不遵從 JavaBean習慣用法
    • 欲使用 JavaBean 慣用 -> 
      • @BeanProperty
      • @scala.reflect.BeanProperty var name: type = _

YouTube Video



import scala.math._

class Car(val year: Int) {
  private var miles: Int = 0

  def getMiles() = miles
  def drive(distance: Int) {
    miles += abs(distance)
  }
}

$ scalac -deprecation script.scala

$ javap -private Car
Compiled from "script.scala"
public class Car extends java.lang.Object implements scala.ScalaObject{
    private final int year;
    private int miles;
    public int year();
    private int miles();
    private void miles_$eq(int);
    public int getMiles();
    public void drive(int);
    public Car(int);
}


class Car2(x: Int, y: Boolean, z: Int)

Compiled from "ss.scala"
public class Car2 extends java.lang.Object implements scala.ScalaObject{
    public Car2(int, boolean, int);
}

class Car2(val x: Int, var y: Boolean, z: Int)

$ javap -private Car2
Compiled from "ss.scala"
public class Car2 extends java.lang.Object implements scala.ScalaObject{
    private final int x;
    private boolean y;
    public int x();
    public boolean y();
    public void y_$eq(boolean);
    public Car2(int, boolean, int);
}


class Car2(val x: Int = 10, y: Boolean, z: Int)

$ javap -private Car2
Compiled from "ss.scala"
public class Car2 extends java.lang.Object implements scala.ScalaObject{
    private final int x;
    public static final int init$default$1();
    public int x();
    public Car2(int, boolean, int);
}


var / =_


以預設值進行初始化, val 因必須在初始化時即指定 value, 故不適用.
scala> var x: Short   
<console>:5: error: only classes can have declared but undefined members
(Note that variables need to be initialized to be defined)
       var x: Short
           ^

scala> class X {                         
     | var x: Int =_                     
     | var x1: String = _                
     | }
defined class X

scala> class X {
     | var x: Int
     | var x1: String
     | }             
<console>:5: error: class X needs to be abstract, since:
variable x1 is not defined
(Note that variables need to be initialized to be defined)
variable x1 is not defined
(Note that variables need to be initialized to be defined)
variable x is not defined
(Note that variables need to be initialized to be defined)
variable x is not defined
(Note that variables need to be initialized to be defined)
       class X {
             ^

scala> class X(var x: Int, var z: String) 
defined class X
                       
scala> val x = new X(2, "H")
x: X = X@3b39d369



def this( ) / this


多重建構函數



extends / override



  • extends 
  • Java 的 @Override 是可被省略, 但 Scala 在覆寫父類別的方法時, 嚴格要求標記 override 關鍵字



override toString( )