-
Notifications
You must be signed in to change notification settings - Fork 92
/
Contents.swift
40 lines (29 loc) · 867 Bytes
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//: Playground - noun: a place where people can play
import UIKit
// Associate Value 可以是多个
enum Shape{
case Square(side: Double)
case Rectangle(width: Double, height: Double)
case Circle(centerx: Double, centery: Double, radius: Double)
case Point //可以没有Associate Value
}
let square = Shape.Square(side: 10)
let rectangle = Shape.Rectangle(width: 20, height: 30)
let circle = Shape.Circle(centerx: 0, centery: 0, radius: 15)
let point = Shape.Point
func area(shape: Shape) -> Double{
switch shape {
case let .Square(side):
return side*side
case let .Rectangle( width , height ):
return width * height
case let .Circle( _ , _ , r ): // 使用_忽略没用的参数
return M_PI*r*r
case .Point:
return 0
}
}
area(square)
area(rectangle)
area(circle)
area(point)