Scala开发教程(50): Ordered Trait
来源:互联网 发布:达达乐队知乎 编辑:程序博客网 时间:2024/06/05 22:51
http://www.imobilebbs.com/wordpress/archives/4954
比较对象也是胖接口来的比较方便的一个应用领域,当你需要比较两个有顺序关系的对象时,如果只需要一个方法就可以知道需要比较的结果就非常便利。比如,你需要“小于”关系,你希望使用“< “比较就可以了,如果是“小于等于”,使用”<=”就可以。如果使用瘦接口来定义类,也许你只定义了一个<比较方法,那么如果需要小于等于,你可能需要使用(x<y)|| (x==y)。一个胖接口定义了所有可能的比较运算符,使得你可以直接使用<=来书写代码。
但胖接口带来的便利也是有代价的,回头看看我们前面定义的Rational类;
如果我们需要定义比较操作,则需要定义如下代码:
1
class
Rational (n
:
Int, d
:
Int) {
2
...
3
4
def
< (that
:
Rational)
=
this
.numer * that.denom > that.numer *
this
.denom
5
6
def
> (that
:
Rational)
=
that <
this
7
8
def
<
=
(that
:
Rational)
=
(
this
< that) || (
this
==
that)
def
>
=
(that
:
Rational)
=
(
this
> that) || (
this
==
that)
9
10
}
这个类定义了四个比较运算符 < ,>,< =和>=。首先我们注意到后面三个比较运算符,都是通过第一个比较运算符来实现的。其次,我们也可以看到,后面三个比较操作对于任意对象都是适用的,和对象的类型无关。而需要实现这四个比较运算的胖接口都要重复这些代码。
Scala对于比较这种常见的操作,提供了Ordered Trait定义,使用它,你可以把所有的比较运算的代码使用一个compare定义来替代。这个ordered trait可以让需要实现比较运算的类,通过和Ordered特质“融合”,而只需实现一个compare方法即可。
因此我们可以修改前面的实现如下:
1
class
Rational (n
:
Int, d
:
Int)
extends
Ordered[Rational]{
2
...
3
4
override
def
compare (that
:
Rational)
=
5
(
this
.numer*that.denom)-(that.numer*that.denom)
6
7
}
要注意两点,一是Ordered 需要指明类型参数 Ordered[T] ,类型参数我们将在后面介绍,这里只需要知道添加所需比较类型的类名称,本例为Rational,此外,需要使用compare方法。 它比较有序对象,=0,表示两个对象相同,>0表示前面大于后面对象,<0表示前面小于后面对象。
下面为测试结果:
1
scala>
class
Rational (n
:
Int, d
:
Int)
extends
Ordered[Rational]{
2
| require(d!
=
0
)
3
|
private
val
g
=
gcd (n.abs,d.abs)
4
|
val
numer
=
n/g
5
|
val
denom
=
d/g
6
|
override
def
toString
=
numer +
"/"
+denom
7
|
def
+(that
:
Rational)
=
8
|
new
Rational(
9
| numer * that.denom + that.numer* denom,
10
| denom * that.denom
11
| )
12
|
def
* (that
:
Rational)
=
13
|
new
Rational( numer * that.numer, denom * that.denom)
14
|
def
this
(n
:
Int)
=
this
(n,
1
)
15
|
private
def
gcd(a
:
Int,b
:
Int)
:
Int
=
16
|
if
(b
==
0
) a
else
gcd(b, a
%
b)
17
|
18
|
override
def
compare (that
:
Rational)
=
19
| (
this
.numer*that.denom)-(that.numer*that.denom)
20
|
21
| }
22
defined
class
Rational
23
24
scala>
val
half
=
new
Rational(
1
,
2
)
25
half
:
Rational
=
1
/
2
26
27
scala>
val
third
=
new
Rational(
1
,
3
)
28
third
:
Rational
=
1
/
3
29
30
scala> half < third res
0
:
Boolean
=
false
31
scala> half >
=
third
32
res
1
:
Boolean
=
true
因此,你在需要实现比较对象时,首先需要考虑Ordered Trait,看看这个Trait能否满足要求,然后通过和这个Trait “混合”就可以很方便的实现对象之间的比较。
此外要注意Ordered Trait 没有定义equal 方法,因为如果需要定义equal方法,那么需要检查传入参数的类型,Ordered Trait无法实现,因此你如果需要==比较运算符,需要另外定义。
- Scala开发教程(50): Ordered Trait
- Scala:trait
- Scala Trait
- Scala Trait
- Scala-trait
- scala通过实现trait Ordered[T]定义一个可排序的类
- scala中的trait
- Scala Trait 的线性化
- Scala 有趣的Trait
- scala——trait
- Scala的Trait
- Scala trait介绍
- Scala trait 未完待续
- Scala的trait
- scala中的Trait
- Scala 之 trait 关键字
- scala学习trait
- Scala Trait详解
- 跨网远程桌面产品
- 矩阵图求路径条数问题(简单dp)
- webstorm快捷键记录
- Spring框架中的单例Beans是线程安全的么?
- HDU 5780 gcd
- Scala开发教程(50): Ordered Trait
- 51Nod-1010-只包含2 3 5的数
- 筛法求质数,欧拉函数
- HDU 5779/BC 85D Tower Defence
- emacs添加cscope插件
- 1100. Mars Numbers (20)
- 心态和想法,是提高编程水平的关键
- 使用Fiddler截断更改Request数据
- 湖南省第十届大学生计算机程序设计竞赛(原题) 第c题 酷酷的单词