鍍金池/ 教程/ Scala/ Trait 示例–Rectangular 對(duì)象
包對(duì)象
Ordered Trait
組合和繼承–定義 final 成員
基本數(shù)據(jù)類型
Match 表達(dá)式
類和對(duì)象 (三)
操作基本數(shù)據(jù)類型
for 表達(dá)式
組合和繼承–重載成員函數(shù)和方法
類和對(duì)象 (二)
組合和繼承–定義 factory 對(duì)象
組合和繼承–多態(tài)和動(dòng)態(tài)綁定
Trait 的基本概念
if 表達(dá)式
組合和繼承–抽象類
函數(shù)–函數(shù)字面量的一些簡(jiǎn)化寫法
while 循環(huán)
組合和繼承–使用組合還是繼承?
訪問控制修飾符
Trait 示例–Rectangular 對(duì)象
組合和繼承–定義參數(shù)化成員變量
組合和繼承–定義無參數(shù)方法
類和對(duì)象 (一)
函數(shù)–閉包
函數(shù)–類成員函數(shù)
Scala 基本數(shù)據(jù)類型的實(shí)現(xiàn)方法
try 表達(dá)式處理異常
選擇瘦接口還是胖接口設(shè)計(jì)?
組合和繼承–小結(jié)
創(chuàng)建新的控制結(jié)構(gòu)
使用 import
為訪問控制修飾符添加作用域
Scala 的類層次關(guān)系
類和對(duì)象 (五)
傳名參數(shù)
柯里化函數(shù)
函數(shù)–頭等公民
組合和組合和繼承–定義 heighten 和 widen 函數(shù)
使用 Package–將代碼放入包中
隱含的 import
所有類的公共子類–底層類型
進(jìn)一步 Scala
函數(shù)–局部函數(shù)
引用包中的代碼
組合和繼承–使用 override 修飾符
組合和繼承–實(shí)現(xiàn)類 Element 的 above,beside 和 toString()方法
類和對(duì)象 (四)
函數(shù)–尾遞歸
沒有“break”和“continue”的日子
組合和繼承–調(diào)用基類構(gòu)造函數(shù)
減低代碼重復(fù)
函數(shù)–函數(shù)–可變參數(shù),命名參數(shù),缺省參數(shù)
起步 Scala
組合和繼承–擴(kuò)展類
函數(shù)–部分應(yīng)用的函數(shù)
開始神奇的 Scala編程之旅
組合和繼承–概述
Trait 用來實(shí)現(xiàn)可疊加的修改操作

Trait 示例–Rectangular 對(duì)象

在設(shè)計(jì)繪圖程序庫(kù)時(shí)常常需要定義一些具有矩形形狀的類型:比如窗口,bitmap 圖像,矩形選取框等。為了方便使用這些矩形對(duì)象,函數(shù)庫(kù)對(duì)象類提供了查詢對(duì)象寬度和長(zhǎng)度的方法(比如 width,height)和坐標(biāo)的 left,right,top 和 bottom 等方法。然而在實(shí)現(xiàn)這些函數(shù)庫(kù)的這樣方法,如果使用 Java 來實(shí)現(xiàn),需要重復(fù)大量代碼,工作量比較大(這些類之間不一定可以定義繼承關(guān)系)。但如果使用 Scala 來實(shí)現(xiàn)這個(gè)圖形庫(kù),那么可以使用 Trait,為這些類方便的添加和矩形相關(guān)的方法。

首先我們先看看如果使用不使用 Trait 如何來實(shí)現(xiàn)這些類,首先我們定義一些基本的幾何圖形類如 Point 和Rectangle:

class Point(val x:Int, val y:Int)
class Rectangle(val topLeft:Point, val bottomRight:Point){
  def left =topLeft.x
  def right =bottomRight.x
  def width=right-left 
  // and many more geometric methods
}

這里我們定義一個(gè)點(diǎn)和矩形類,Rectangle 類的主構(gòu)造函數(shù)使用左上角和右下角坐標(biāo),然后定義了 left,right,和 width 一些常用的矩形相關(guān)的方法。

同時(shí),函數(shù)庫(kù)我們可能還定義了一下 UI 組件(它并不是使用 Retangle 作為基類),其可能的定義如下:

abstract class Component {
  def topLeft :Point
  def bottomRight:Point
  def left =topLeft.x
  def right =bottomRight.x
  def width=right-left
  // and many more geometric methods
}

可以看到 left,right,width 定義和 Rectangle 的定義重復(fù)??赡芎瘮?shù)庫(kù)還會(huì)定義其它一些類,也可能重復(fù)這些定義。

如果我們使用 Trait,就可以消除這些重復(fù)代碼,比如我們可以定義如下的 Rectangular Trait 類型:

trait Rectangular {
  def topLeft:Point
  def bottomRight:Point
  def left =topLeft.x
  def right =bottomRight.x
  def width=right-left
  // and many more geometric methods
}

然后我們修改 Component 類定義使其“融入”Rectangular 特性:

abstract class Component extends Rectangular{
 //other methods
}

同樣我們也修改 Rectangle 定義:

class Rectangle(val topLeft:Point, val bottomRight:Point) extends Rectangular{
  // other methods
}

這樣我們就把矩形相關(guān)的一些屬性和方法抽象出來,定義在 Trait 中,凡是“混合”了這個(gè) Rectangluar 特性的類自動(dòng)包含了這些方法:

object TestConsole extends App{
   val rect=new Rectangle(new Point(1,1),new Point(10,10))
    println (rect.left)
    println(rect.right)
    println(rect.width)
}

運(yùn)行結(jié)果如下:

1
 10
 9