快学Scala 第6章 对象 - 练习
2015-04-18 08:32 jetwill 阅读(362) 评论(0) 编辑 收藏 举报1. 编写一个Conversions对象,加入inchesToCentimeters、gallonsToLiters和milesToKilometers方法。
object
Conversions {
def
main(args
:
Array[String]){
printf(
"1 inch = %g centimeters\n"
, inchesToCentimeters(
1
))
printf(
"2 gallons = %g liters\n"
, gallonsToLiters(
2
))
printf(
"3 miles = %g centimeters\n"
, milesToKilometers(
3
))
}
def
inchesToCentimeters(inches
:
Double)
=
{
inches *
2.54
}
def
gallonsToLiters(gallons
:
Double)
=
{
gallons *
3.7854118
}
def
milesToKilometers(miles
:
Double)
=
{
miles *
1.609344
}
}
/*result
1 inch = 2.54000 centimeters
2 gallons = 7.57082 liters
3 miles = 4.82803 centimeters
*/
2. 前一个练习不是很面向对象。提供一个通用的超类UnitConversions并定义扩展该超类的inchesToCentimeters、gallonsToLiters和milesToKilometers对象。
abstract
class
UnitConversions{
def
convert(amt
:
Double)
:
Double
}
object
inchesToCentimeters
extends
UnitConversions{
override
def
convert(amt
:
Double)
=
amt *
2.54
}
object
gallonsToLiters
extends
UnitConversions{
override
def
convert(amt
:
Double)
=
amt *
3.7854118
}
object
milesToKilometers
extends
UnitConversions{
override
def
convert(amt
:
Double)
=
amt *
1.609344
}
printf(
"1 inch = %g centimeters\n"
, inchesToCentimeters.convert(
1
))
printf(
"2 gallons = %g liters\n"
, gallonsToLiters.convert(
2
))
printf(
"3 miles = %g centimeters\n"
, milesToKilometers.convert(
3
))
/*result
1 inch = 2.54000 centimeters
2 gallons = 7.57082 liters
3 miles = 4.82803 centimeters
*/
3. 定义一个扩展自java.awt.Point的Origin对象。为什么说这实际上不是个好主意?(仔细看Point类的方法。)
Point类中的getLocation方法返回的是Point对象,如果想返回Origin对象,需要Origin类才行。
import
java.awt.
_
object
Origin
extends
Point{
override
def
getLocation
:
Point
=
super
.getLocation
}
Origin.move(
2
,
3
)
println(Origin.toString)
/*result
Main$$anon$1$Origin$[x=2,y=3]
*/
4. 定义一个Point类和一个伴生对象,使得我们可以不用new而直接用Point(3, 4)来构造Point实例。
class
Point
private
(
val
x
:
Int,
val
y
:
Int){
override
def
toString
=
"Point at [x=%d,y=%d]"
.format(x,y)
}
object
Point{
def
apply(x
:
Int, y
:
Int)
=
new
Point(x,y)
}
val
a
=
Point(
3
,
4
)
println(a.toString)
/*result
Point at [x=3,y=4]
*/
5. 编写一个Scala应用程序,使用App特质,以反序打印命令行参数,用空格隔开。举例来说,scala Reverse Hello World应该打印出World Hello。
object
Reverse{
def
main(args
:
Array[String]){
for
(i <- (
0
until args.length).reverse) print(args(i) +
" "
)
}
}
/* The following code did not work for me.
object Reverse extends App{
for(i <- (0 until args.length).reverse) print(args(i) + " ")
}
*/
/*result
World Hello
*/
6. 编写一个扑克牌4种花色的枚举,让其toString方法分别返回♠♥♦或♣。
object
Poker
extends
Enumeration
with
App{
val
Heart
=
Value(
3
,
"
♥"
)
val
Diamond
=
Value(
4
,
"
♦"
)
val
Club
=
Value(
5
,
"
♣"
)
val
Spade
=
Value(
6
,
"
♠"
)
println(Poker.Heart)
println(Poker.Diamond)
println(Poker.Club)
println(Poker.Spade)
}
7. 实现一个函数,检查某张牌的花色是否为红色。
object
Card
extends
Enumeration
with
App{
val
Heart
=
Value(
3
,
""
)
val
Diamond
=
Value(
4
,
""
)
val
Club
=
Value(
5
,
""
)
val
Spade
=
Value(
6
,
""
)
def
color(c
:
Card.Value){
if
(c
==
Card.Club || c
==
Card.Spade) print(
"Black"
)
else
print(
"Red"
)
}
color(Card.Heart)
}
/*
Red
*/
8. 编写一个枚举,描述RGB立方体的8个角。ID使用颜色值(例如,红色是0xff0000)。
object
RGB
extends
Enumeration
with
App{
val
RED
=
Value(
0xff0000
,
"Red"
)
val
BLACk
=
Value(
0x000000
,
"Black"
)
val
GREEN
=
Value(
0x00ff00
,
"Green"
)
val
CYAN
=
Value(
0x00ffff
,
"Cyan"
)
val
YELLO
=
Value(
0xffff00
,
"Yellow"
)
val
WHITE
=
Value(
0xffffff
,
"White"
)
val
BLUE
=
Value(
0x0000ff
,
"Blue"
)
val
MAGENTA
=
Value(
0xff00ff
,
"Magenta"
)
}