## 2016年8月4日 星期四

### [Scala 小學堂] Scala Gossic : 繼續深入 - 進階型態 (初始抽象 val 成員)

Source From Here

1. abstract class Circle(r: Double) {
2.     val radius = r
3.     val area = Math.Pi * radius * radius
4. }
5.
6. def calRadius = 10.0
7.
8. class RedCircle(r: Double) extends Circle(r)
9.
10. val c = new RedCircle(calRadius)
11. println(c.area)     // 314.1592653589793

1. abstract class Circle(r: Double) {
2.     val radius = r
3.     val area = Math.Pi * radius * radius
4. }
5.
6. def calRadius = 10.0
7.
8. val c = new Circle(calRadius) {
9.             // 其它實作
10.         }
11.
12. println(c.area)     // 314.1592653589793

1. trait Circle {
2.     val radius: Double
3.     val area = Math.Pi * radius * radius
4. }
5.
6. def calRadius = 10.0
7.
8. class RedCircle(r: Double) extends Circle {
9.     val radius = r
10. }
11.
12. val c = new RedCircle(calRadius)
13. println(c.area)    // 314.1592653589793

1. trait Circle {
2.     val radius: Double
3.     val area = Math.Pi * radius * radius
4. }
5.
6. def calRadius = 10.0
7.
8. val c = new Circle {
10.         }
11.
12. println(c.area)，     // 0.0

1. trait Circle {
2.     val radius: Double
3.     val area = Math.Pi * radius * radius
4. }
5.
6. def calRadius = 10.0
7.
8. val c = new { val radius = calRadius } with Circle {
9.             // 其它定義
10.         }
11.
12. println(c.area)    // 314.1592653589793

## 關於我自己

Where there is a will, there is a way!