Tuple是一个优雅的类,给你提供一种简单的方式把不同类型的数据存放在同一个容器里。
class Person(var name: String)
你可以不用创建下面这样的临时类来存放一些信息:
class SomeThings(i: Int, s: String, p: Person)
你可以像下面这样创建一个 tuple :
val t = (3, "Three", new Person("Al"))
只需要放一些元素到圆括号里,你就创建了一个Tuple。Tuple 中可以保护2到22个元素。
scala> val d = ("Maggie", 30)
d: (String, Int) = (Maggie,30)
scala> case class Person(name: String)
defined class Person
scala> val t = (3, "Three", new Person("David"))
t: (Int, java.lang.String, Person) = (3,Three,Person(David))
访问Tuple中的元素:
scala> t._1
res1: Int = 3
scala> t._2
res2: java.lang.String = Three
scala> t._3
res3: Person = Person(David)
scala> val(x, y, z) = (3, "Three", new Person("David"))
x: Int = 3
y: String = Three
z: Person = Person(David)
当你想从方法中返回很多个值时:
def getStockInfo = {
// other code here ...
("NFLX", 100.00, 101.00) // this is a Tuple3
}
val (symbol, currentPrice, bidPrice) = getStockInfo
Tuple 不是集合类,因此也没有map,fliter之类的方法。
为 Pizza 商店写一个订单输入系统。
虽然还没说明 sealed trait 和 case object 的概念,但也还可以从前面学到的看出这些枚举的意思:
sealed trait Topping
case object Cheese extends Topping
case object Pepperoni extends Topping
case object Sausage extends Topping
case object Mushrooms extends Topping
case object Onions extends Topping
sealed trait CrustSize
case object SmallCrustSize extends CrustSize
case object MediumCrustSize extends CrustSize
case object LargeCrustSize extends CrustSize
sealed trait CrustType
case object RegularCrustType extends CrustType
case object ThinCrustType extends CrustType
case object ThickCrustType extends CrustType
import scala.collection.mutable.ArrayBuffer
Pizza信息:
class Pizza (
var crustSize: CrustSize,
var crustType: CrustType,
var toppings: ArrayBuffer[Topping]
)
订单信息:
class Order (
var pizzas: ArrayBuffer[Pizza],
var customer: Customer
)
客户信息:
class Customer (
var name: String,
var phone: String,
var address: Address
)
地址:
class Address (
var street1: String,
var street2: String,
var city: String,
var state: String,
var zipCode: String
)
class Pizza (
var crustSize: CrustSize,
var crustType: CrustType,
val toppings: ArrayBuffer[Topping]
) {
def addTopping(t: Topping): Unit = toppings += t
def removeTopping(t: Topping): Unit = toppings -= t
def removeAllToppings(): Unit = toppings.clear()
}
你或许还希望可以直接计算出价格:
def getPrice(
toppingsPrices: Map[Topping, Int],
crustSizePrices: Map[CrustSize, Int],
crustTypePrices: Map[CrustType, Int]
): Int = ???
??? 语法也是经常用到的,来表示这个方法还没有实现,但是可以正常编译通过。
class Order (
val pizzas: ArrayBuffer[Pizza],
var customer: Customer
) {
def addPizza(p: Pizza): Unit = pizzas += p
def removePizza(p: Pizza): Unit = pizzas -= p
// need to implement these
def getBasePrice(): Int = ???
def getTaxes(): Int = ???
def getTotalPrice(): Int = ???
}
import scala.collection.mutable.ArrayBuffer
object MainDriver extends App {
val p1 = new Pizza (
MediumCrustSize,
ThinCrustType,
ArrayBuffer(Cheese)
)
val p2 = new Pizza (
LargeCrustSize,
ThinCrustType,
ArrayBuffer(Cheese, Pepperoni, Sausage)
)
val address = new Address (
"123 Main Street",
"Apt. 1",
"Talkeetna",
"Alaska",
"99676"
)
val customer = new Customer (
"Alvin Alexander",
"907-555-1212",
address
)
val o = new Order(
ArrayBuffer(p1, p2),
customer
)
o.addPizza(
new Pizza (
SmallCrustSize,
ThinCrustType,
ArrayBuffer(Cheese, Mushrooms)
)
)
// print the order
o.printOrder
}