stay foolish,stay hungry.

导航

scala 面向对象之 继承

scala 面向对象之 继承

scala


 

1.extends

Scala中,让子类继承父类,与Java一样,也是使用extends关键字

继承就代表,子类可以从父类继承父类的field和method;然后子类可以在自己内部放入父类所没有,子类特有的field和method;使用继承可以有效复用代码
 
  1. class Person {
  2. private var name = "leo"
  3. def getName = name
  4. }
  5. class Student extends Person {
  6. private var score = "A"
  7. def getScore = score
  8. }

子类可以覆盖父类的field和method;但是如果父类用final修饰,则该类是无法被继承的,field和method用final修饰,field和method是无法被覆盖的

 
  1. final class Person { //final embellish class Person it not be extends
  2. private var name = "leo"
  3. def getName = name
  4. }
  5. class Student extends Person { //error
  6. private var score = "A"
  7. def getScore = score
  8. }

eg:

scala> final class Person {
 |   private var name = "leo"
 |   def getName = name
 | }
defined class Person
scala> class Student extends Person {
 |   private var score = "A"
 |   def getScore = score
 | }
<console>:8: error: illegal inheritance from final class Person
   class Student extends Person {

override 父类final修饰的字段 方法

 
  1. class Person {
  2. private var name = "leo"
  3. final val age = 10; //final embellish field age it not be override by child class
  4. def getName = name
  5. }
  6. class Student extends Person {
  7. private var score = "A"
  8. def getScore = score
  9. override val age = 20 //error
  10. override def getName = "my name is" + super.getName
  11. }

eg:

scala> class Person {
     |   private var name = "leo"
     |   final val age = 10;   //final embellish field age it not be override by child class
     |   def getName = name
     | }
defined class Person
scala> class Student extends Person {
     |   private var score = "A"
     |   def getScore = score
     |   override val age = 20 //error
     |   override def getName = "my name is" + super.getName
     | }
<console>:11: error: overriding value age in class Person of type Int(10);
 value age cannot override final member
         override val age = 20 //error
 

2.isInstanceOf 和 asInstanceOf

如果我们创建了子类的对象,但是又将其赋予了父类类型的变量。则在后续的程序中,我们又需要将父类类型的变量转换为子类类型的变量,应该如何做?

首先,需要使用isInstanceOf判断对象是否是指定类的对象,如果是的话,则可以使用asInstanceOf将对象转换为指定类型
注意,如果对象是null,则isInstanceOf一定返回false,asInstanceOf一定返回null
注意,如果没有用isInstanceOf先判断对象是否为指定类的实例,就直接用asInstanceOf转换,则可能会抛出异常
 
  1. class Person
  2. class Student extends Person
  3. val p: Person = new Student
  4. var s: Student = null //note:isInstanceOf and asInstanceOf use[] instead of ()
  5. if (p.isInstanceOf[Student]) s = p.asInstanceOf[Student]

但,isInstanceOf只能判断是否为某个类或者某个类的子类,无法精确判断。 
eg:

scala> print(p.isInstanceOf[Student])
true
scala> print(p.isInstanceOf[Person])
true
scala>
 

3.getClass 和 classOf

isInstanceOf只能判断出对象是否是指定类以及其子类的对象,而不能精确判断出,对象就是指定类的对象 
如果要求精确地判断对象就是指定类的对象,那么就只能使用getClass和classOf了 
对象.getClass可以精确获取对象的类,classOf[类]可以精确获取类,然后使用==操作符即可判断

 
  1. class Person
  2. class Student extends Person
  3. val p: Person = new Student
  4. p.isInstanceOf[Person]
  5. p.getClass == classOf[Person]
  6. p.isInstanceOf[Student]
  7. p.getClass == classOf[Student]

eg:

scala> p.getClass == classOf[Person]
res20: Boolean = false
scala> p.getClass == classOf[Student]
res21: Boolean = true
 

4.使用匹配模式来进行类型判断

但是在实际开发中,比如spark的源码中,大量的地方都是使用了模式匹配的方式来进行类型的判断,这种方式更加地简洁明了,而且代码得可维护性和可扩展性也非常的高 
使用模式匹配,功能性上来说,与isInstanceOf一样,也是判断主要是该类以及该类的子类的对象即可,不是精准判断的

 
  1. class Person
  2. class Student extends Person
  3. val p: Person = new Student
  4. p match {
  5. case per: Person => println("it's Person's object")
  6. case _ => println("unknown type")
  7. }
 

3.protected

跟java一样,scala中同样可以使用protected关键字来修饰field和method,这样在子类中就不需要super关键字,直接就可以访问非private修饰的field和method(非private修饰的field可以直接访问) 
还可以使用protected[this],则只能在当前子类对象中访问父类的field和method,无法通过其他子类对象访问父类的field和method

 
  1. class Person {
  2. protected var name: String = "leo"
  3. protected[this] var hobby: String = "game"
  4. }
  5. class Student extends Person {
  6. def sayHello = println("Hello, " + name)
  7. def makeFriends(s: Student) { //this s is not be current instance object
  8. println("my hobby is " + hobby + ", your hobby is " + s.hobby)// error
  9. }
  10. }

eg1: error

scala> class Person {
     |   protected var name: String = "leo"
     |   protected[this] var hobby: String = "game"
     | }
defined class Person
scala> class Student extends Person {
     |   def sayHello = println("Hello, " + name)
     |   def makeFriends(s: Student) {
     |     println("my hobby is " + hobby + ", your hobby is " + s.hobby)
     |   }
     | }
<console>:15: error: value hobby is not a member of Student
           println("my hobby is " + hobby + ", your hobby is " + s.hobby)

eg2:

scala> class Person {
     |   protected var name: String = "leo"
     |   protected var hobby:String = "game"
     | }
defined class Person
scala> class Student extends Person {
     |   def sayHello = println("Hello, " + name)
     |   def makeFriends(s: Student) { //this s is not be current instance object
     |     println("my hobby is " + hobby + ", your hobby is " + s.hobby)
     |   }
     | }
defined class Student
 

4.调用父类的constructor

Scala中,每个类可以有一个主constructor和任意多个辅助constructor,而每个辅助constructor的第一行都必须是调用其他辅助constructor或者是主constructor;因此子类的辅助constructor是一定不可能直接调用父类的constructor的 
只能在子类的主constructor中调用父类的constructor,以下这种语法,就是通过子类的主构造函数来调用父类的构造函数 
注意!如果是父类中接收的参数,比如name和age,子类中接收时,就不要用任何val或var来修饰了,否则会认为是子类要覆盖父类的field

 
  1. class Person(val name: String, val age: Int)
  2. class Student(name: String, age: Int, var score: Double) extends Person(name, age) {
  3. def this(name: String) {
  4. this(name, 0, 0)
  5. }
  6. def this(age: Int) {
  7. this("leo", age, 0)
  8. }
  9. }
 

5.匿名内部类

在Scala中,匿名子类是非常常见,而且非常强大的。Spark的源码中也大量使用了这种匿名子类。 
匿名子类,也就是说,可以定义一个类的没有名称的子类,并直接创建其对象,然后将对象的引用赋予一个变量。之后甚至可以将该匿名子类的对象传递给其他函数。

 
  1. class Person(protected val name: String) {
  2. def sayHello = "Hello, I'm " + name
  3. }
  4. val p = new Person("leo") {//p is a anonymity child class's object
  5. override def sayHello = "Hi, I'm " + name
  6. }
  7. def greeting(p: Person { def sayHello: String }) {
  8. println(p.sayHello)
  9. }
  10. greeting(p)//use anonymity class's object as a method parameter
 

6.抽象类 abstract class and abstract field

如果在父类中,有某些方法无法立即实现,而需要依赖不同的子类来覆盖,重写实现自己不同的方法实现。此时可以将父类中的这些方法不给出具体的实现,只有方法签名,这种方法就是抽象方法。

而一个类中如果有一个抽象方法,那么类就必须用abstract来声明为抽象类,此时抽象类是不可以实例化的 
在子类中覆盖抽象类的抽象方法时,不需要使用override关键字

 
  1. abstract class Person(val name: String) {
  2. def sayHello: Unit
  3. }
  4. class Student(name: String) extends Person(name) {
  5. def sayHello: Unit = println("Hello, " + name)
  6. }

如果在父类中,定义了field,但是没有给出初始值,则此field为抽象field 
抽象field意味着,scala会根据自己的规则,为var或val类型的field生成对应的getter和setter方法,但是父类中是没有该field的 
子类必须覆盖field,以定义自己的具体field,并且覆盖抽象field,不需要使用override关键字

 
    1. abstract class Person {
    2. val name: String
    3. }
    4. class Student extends Person {
    5. val name: String = "leo"
    6. }

posted on 2017-03-18 11:59  red_and_black  阅读(2711)  评论(0编辑  收藏  举报