好程序员大数据学习路线分享Scala系列之抽象类程序员
1抽象类的定义ide
定义一个抽象类:学习
若是某个类至少存在一个抽象方法或一个抽象字段,则该类必须声明为abstract。大数据
abstract class Person{
//没有初始值,抽象字段
var name:String
//没有方法体,是抽象方法
def id: Int
}
class Employ extends Person{
var name:String="Fred"
//实现,不须要overide关键字
def id = name.hashCode
}element
2抽象类的应用hash
定义带有抽象类型成员的特质:it
trait Buffer {
type T
val element: T
}class
定义一个抽象类,增长类型的上边界List
abstract class SeqBuffer extends Buffer {
type U
//
type T <: Seq[U]
def length = element.length
}程序
abstract class IntSeqBuffer extends SeqBuffer {
type U = Int
}
abstract class IntSeqBuffer extends SeqBuffer {
type U = Int
}
//使用匿名类将 type T 设置为 List[Int]
def newIntSeqBuf(elem1: Int, elem2: Int): IntSeqBuffer =
new IntSeqBuffer {
type T = List[U]
val element = List(elem1, elem2)
}
val buf = newIntSeqBuf(7, 8) println("length = " + buf.length) println("content = " + buf.element)