您的位置:首页 > 其它

第54讲:Scala中复合类型实战详解

2015-09-10 23:28 218 查看
今天学习了scala的复合类型的内容,让我们通过实战来看看代码:

trait Compound_Type1
trait Compound_Type2
class Compound_Type extends Compound_Type1 with Compound_Type2

object test54 {
def compound_Type(x:Compound_Type1 with Compound_Type2) = {println ("Compound_Type test")}
def main(args:Array[String]){

compound_Type(new Compound_Type1 with Compound_Type2)
object compound_Type_oject extends Compound_Type1 with Compound_Type2
compound_Type(compound_Type_oject)

type compound_Type_Alias = Compound_Type1 with Compound_Type2
def compound_Type_Local(x:compound_Type_Alias)=println("Compound_Type test2")
val compound_Type_Class = new Compound_Type
compound_Type_Local(compound_Type_Class)

type Scala = Compound_Type1 with Compound_Type2 {def init():Unit}

}
}

首先,定义了两个特质Compound_Type1和Compound_Type2,然后定义了类Compound_Type继承自它们。

然后我们定义一个compound_Type方法,它只允许传入Compound_Type1 with Compound_Type2类型的对像,这种类型,就叫做复合类型。

当我们新定义一个对像时并调用该方法时,便会打印Compound_Type test

当然,我们也可以使用type方法来给复合类型定义一个别名,如:

type compound_Type_Alias = Compound_Type1 with Compound_Type2
def compound_Type_Local(x:compound_Type_Alias)=println("Compound_Type test2")

分享下更多的scala资源吧:

百度云盘:http://pan.baidu.com/s/1gd7133t

微云云盘:http://share.weiyun.com/047efd6cc76d6c0cb21605cfaa88c416

360云盘: http://yunpan.cn/cQN9gvcKXe26M (提取码:13cd)

信息来源于 DT大数据梦工厂微信公众账号:DT_Spark

关注微信账号,获取更多关于scala学习内容
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: