Scala类型 8:type 关键字
2015-02-27 21:49
232 查看
在Scala中,除了class、trait、object 会产生类型,还可以用type关键字声明类型。
type 声明别名(alias)(通常用于声明复杂类型或定义一个抽象类型):
声明复杂类型 (如:声明一个结果类型):
U是一个抽象类型。T类型的具体实例,如下:
抽象类型:
type 声明别名(alias)(通常用于声明复杂类型或定义一个抽象类型):
scala> type S = String defined type alias S
声明复杂类型 (如:声明一个结果类型):
type T = Serializable { type U; def foo(): Unit }
U是一个抽象类型。T类型的具体实例,如下:
scala> object A extends Serializable{ type X=String; def foo(){} } scala> typeOf[A.type] <:< typeOf[T] res19: Boolean = true
抽象类型:
scala> trait A { type T ; def foo(i:T) = print(i) } scala> class B extends A { type T = Int } scala> val b = new B scala> b.foo(200) 200 scala> class C extends A { type T = String } scala> val c = new C scala> c.foo("hello") hello
相关文章推荐
- scala类型系统 type关键字
- scala类型系统:8) type关键字
- Scala新手指南中文版 -第五篇 The Option Type(Option类型)
- Scala类型 6:复合类型 & with关键字
- 【Scala类型系统】自身类型(self type)引用
- 存储类型关键字(Storage type keywords) static 与const
- Scala之自身类型(Self Type)与蛋糕模式(Cake Pattern)
- c语言==测量各种数据类型的长度以及typedf关键字对数据类型的作用
- Scala类型 10 :交集类型(intersection type)与联合类型(union type)
- 【Scala类型系统】自身类型(self type)引用
- Scala 抽象类型Type
- Scala新手指南中文版 -第五篇 The Option Type(Option类型)
- Scala 类型: Type vs. Class(类型与类)
- scala类型系统:3) 单例类型与this.type
- Scala 自身类型(self-type) 解析
- 测量各种数据类型的长度以及typedf关键字对数据类型的作用
- scala的implicit关键字----不仅仅是隐式类型转换
- scala学习笔记1-泛型、界定、形变、this.type、复合类型、抽象类型
- scala类型系统:6) 复合类型与with关键字
- Scala新手指南中文版 -第七篇 The Either type(Either类型)