与Java一样,也是使用extends关键字,使用继承可以有效复用代码
- class Person {
- private var name = "leo"
- def getName = name
- }
- class
- Student
- extends
- Person
- {
- private var score = "A"
- def getScore = score
- }
- defined class Person
- defined class Student
- scala> val s = new Student
- s: Student = Student@53432aef
- scala> s.getName
- res27: String = leo
子类可以覆盖父类的field和method;但如果父类用final修饰,则该类是无法被继承的,如果field和method用final修饰,则field和method是无法被覆盖的。
跟Java一样,在Scala中,如果子类要覆盖一个父类中的非抽象方法,则必须要使用override关键字。
但再覆盖之后,如果我们需要在子类中调用父类被覆盖的方法该怎么办呢?那就可以使用super关键字,显式地指定要调用的父类方法。
- class Person {
- private var name = "leo"
- def getName = name
- }
- class
- Student
- extends
- Person
- {
- private var score = "A"
- def getScore = score
- override
- def getName
- =
- "Hi, I'm"
- +
- super
- .getName
- }
在Scala中,子类可以覆盖父类的val field,而且子类的val field还可以覆盖父类val field的getter方法;只要在子类中使用override关键字。
- class Person {
- val name: String = "Person"
- def age
- :
- Int
- =
- 0
- }
- class
- Student
- extends
- Person
- {
- override
- val
- name:
- String
- =
- "leo"
- override
- val
- age:
- Int
- =
- 30
- }
- defined class Person
- defined class Student
- scala> val s = new Student
- // 测试覆盖了父类的getter方法
- scala> s.age
- res28: Int = 30
使用isInstanceOf 和 asInstanceOf可以安全的做到父类与子类对象之间类型的转化。
首先,需要使用isInstanceOf判断对象是否是指定类的对象,如果是的话,则可以使用asInstanceOf将对象转换为指定类型。
- class Person
- class
- Student
- extends
- Person
- val
- p:
- Person
- =
- new
- Student
- var s: Student = null
- if (p.isInstanceOf[Student]) s = p.asInstanceOf[Student]
- scala> s
- res31: Student = Student@7ebfe01a
注:
- 如果对象时null,则isInstanceOf返回false,asInstanceOf返回null
- 如果没有用isInstanceOf先判断就直接asInstanceOf转换,则可能抛出异常
isInstanceOf 只能判断出对象是否是指定类以及其子类的对象,而不能精确判断出,对象就是指定类的对象。
使用getCalss 和 classOf 就可以精确判断。
- class Person
- class
- Student
- extends
- Person
- val
- p:
- Person
- =
- new
- Student
- scala> p.getClass == classOf[Person]
- res32: Boolean = false
- scala> p.getClass == classOf[Student]
- res33: Boolean = true
跟Java一样,Scala中同样可以使用protected关键字来修饰field和method,这样在子类中就不需要super关键字,直接就可以访问。
还可以使用protected[this],则只能在当前子类对象中访问父类的field和method,无法在其他子类对象中访问,跟private[this]一样。
- class Person {
- protected
- var
- name:
- String
- =
- "leo"
- protected
- [
- this
- ]
- var
- hobby:
- String
- =
- "game"
- }
- class
- Student
- extends
- Person
- {
- def sayHelllo
- = println(
- "hello, "
- + name)
- def makeFriends
- (s:
- Student
- ) {
- println("my hobby is " + hobby + ",your hobby is " + s.hobby)
- }
- }
- // 因为父类中使用了protected[this],所以这里s.hobby访问出错
- <console>:19: error: value hobby is not a member of Student
- println("my hobby is " + hobby + ",your hobby is " + s.hobby)
- ^
在Scala中,每个类可以有一个主constructor和任意多个辅constructor,而每个辅助constructor的第一行都必须是调用其他辅助constructor或者是主constructor,因此子类的辅助constructor是一定不可能直接调用父类的constructor的。
只能在子类的主constructor中调用父类的constructor,采用以下语法:
- class
- Person
- (
- val name: String, val age: Int
- )
- /* 注意,如果是父类接收的参数,比如name和age,子类中接收时,就不要用任何val或者var来修饰,否则会被认为是子类覆盖父类的field。*/
- class Student(name: String, age: Int, var score: Double) extends Person(name, age) {
- def this
- (name:
- String
- ) {
- this(name, 0, 0)
- }
- def this
- (age:
- Int
- ) {
- this("sparks", age, 0)
- }
- }
- defined class Person
- defined class Student
- scala>
- val
- s =
- new
- Student
- (
- "sparks"
- )
- s: Student = Student@4c3fcbe7
- scala>
- val
- s =
- new
- Student
- (
- 20
- )
- s: Student = Student@7852ab30
在Scala中,匿名内部类是非常强大且常见的,Spark源码中也大量使用了这种匿名内部类。
定义一个类的没有名字的子类,并直接创建其对象,然后将该对象的引用赋予一个变量,之后甚至可以将该对象传递给其他函数。
- class
- Person
- (
- protected val name: String
- )
- {
- def sayHello
- =
- "hello, I'm "
- + name
- }
- // 定义匿名内部类
- val
- p =
- new
- Person
- (
- "leo"
- ){
- override
- def sayHello
- =
- "Hi, I'm "
- + name
- }
- // 定义使用匿名内部类的函数
- def greeting
- (p:
- Person
- {
- def sayHello
- :
- String
- }){
- println(p.sayHello)
- }
- // 测试
- scala> greeting(p)
- Hi, I'm leo
跟Java相似,一个类中如果有一个抽象方法,那么类就必须用abstract来声明类,此时抽象类不可以实例化。
在子类覆盖抽象类的抽象方法时,不需要使用override 关键字
- abstract
- class
- Person
- (
- val name: String
- )
- {
- def sayHello
- :
- Unit
- }
- class
- Student
- (
- name: String
- )
- extends
- Person
- (
- name
- )
- {
- def sayHello
- :
- Unit
- = println(
- "Hello, "
- + name)
- }
如果在抽象类中定义了field,但没有给出初始值,则此field为抽象field
抽象field意味着:scala会根据自己的规则,为var或val类型的field生成对应的getter和setter方法,但是父类中是没有该field的,子类必须覆盖field,不需要使用override关键字。
- abstract
- class Person
- {
- val name: String
- }
- // 子类必须要覆盖抽象field
- class
- Student
- extends
- Person
- {
- val name: String = "leo"
- }
来源: http://www.cnblogs.com/LiCheng-/p/8030090.html