# 型態參數基礎

``class Node[T] {    var next: Node[T]      = _    private[this] var v: T = _    def value: T = v    def value_=(v: T) { this.v = v }  }``

``val first = new Node[String]first.value = "Justin"first.next = new Node[String]first.next.value = "momor"``

``val first = new Node[Int]first.value = 100first.next = new Node[Int]first.next.value = 200``

``def toOneByRow[T](array: Array[Array[T]]): Array[T] = {    val arr = new Array[T](array.length * array(0).length)    for(row <- 0 until array.length; column <- 0 until array(0).length) {        arr(row * array(0).length + column) = array(row)(column)    }    arr    }val arr1 = Array(              Array(1, 2, 3),              Array(4, 5, 6)           )          toOneByRow[Int](arr1).foreach(println)val arr2 = Array(              Array("one" , "two"  , "three"),              Array("four", "five" , "six")           )           toOneByRow[String](arr2).foreach(println))``

``def toOneByRow[T](array: Array[Array[T]]) = {    val arr = new Array[T](array.length * array(0).length)    for(row <- 0 until array.length; column <- 0 until array(0).length) {        arr(row * array(0).length + column) = array(row)(column)    }    arr    }val arr1 = Array(              Array(1, 2, 3),              Array(4, 5, 6)           )          toOneByRow(arr1).foreach(println)  // 因為類型推斷作用，無須寫明型態資訊``

``def show[T](values: T*) = {    values.foreach(println)}show(1, 2, 3)show("one", "two", "three", "four", "five")``

package scala

trait Ordered[A] {
def compare(that: A): Int
def <  (that: A): Boolean = (this compare that) <  0
def >  (that: A): Boolean = (this compare that) >  0
def <= (that: A): Boolean = (this compare that) <= 0
def >= (that: A): Boolean = (this compare that) >= 0
def compareTo(that: A): Int = compare(that)
}

Ordered[A]可以在使用時宣告A的型態，一個在 作為共用實作的特徵 看過的例子如下：
``class Ball(val radius: Int) extends Ordered[Ball] {    def volume = 4 * Math.Pi * Math.pow(radius, 3) / 3    def compare(that: Ball) = this.radius - that.radius    override def equals(a: Any) = a match {        case that: Ball => this.radius == that.radius;        case _ => false    }    override def hashCode = 41 * radius}val b1 = new Ball(10)val b2 = new Ball(20)println(b1 > b2)       // falseprintln(b1 >= b2)      // falseprintln(b1 < b2)       // trueprintln(b1 <= b2)      // trueprintln(b1 == b2)      // false``