Section 2 Scala Object-Oriented Programming: 9, getClass and classOf; 10, call the parent class constructor; 11, abstract abstract classes and fields;

6.3.4. Scala in getClass and classOf

Class A extends class B

B b=new A    b.getClass ==classOf[A]

B b=new B    b.getClass ==classOf[B]

 

  • IsInstanceOf only if it is determined that the specified target object class and its subclasses, and can not accurately judged, is the object that the specified class;
  • If fine is determined that the target object for the specified class, then it can only use the getClass and classOf;
  • p.getClass can be accurately acquired object class, classOf [XX] can accurately obtain class == operator can then use the determination;
  • for example:
  • Scala, each class can have any of a plurality of primary and secondary constructor constructor, and the first row of each auxiliary constructor must call the constructor or other auxiliary main constructor codes; thus the auxiliary constant constructor subclass is not possible to directly call the constructor of the parent class;
Package cn.itcast.extends_demo class person4 {} class Student4 the extends person4 Object Student4 { DEF main (args: the Array [String]) { Val p: = person4 new new Student4 
    Example // p is determined whether the class person4 the println (p.isInstanceOf [person4]) to true // 
    // determines whether the type of p type person4 the println (p.getClass == classOf [person4]) to false // 
    // determines whether the type of p type Student4 the println (p.getClass == classOf [Student4]) to true // 
  } 
}




 
   

   

   

   

 

6.3.5. Scala call the parent class constructor

 

  • You can only call the constructor of the parent class in the main constructor of the subclass.
  • If the constructor of the parent class has been defined in the field, such as name and age, when the sub-class re-use, do not use a val or var modified, otherwise it will be considered a subclass to cover the field of the parent class, and require a certain to use the override keyword.
  • for example:
Package cn.itcast.extends_demo class Person7 ( Val name: String, Val Age: Int) { var Score : Double = 0.0 var address : String = "Beijing" DEF the this (name: String, Score: Double) = { 
    // each auxiliary line must first call the constructor of the other auxiliary or main constructor constructor Code 
    // constructor main codes the this (name, 30) the this . Score = Score 
  } 
  // other auxiliary constructor DEF the this (name: String, address: String) {= the this (name, 100.0) the this . address = address 
  } 
}


 
 
 


     
     


 
     
     


class Student7(name:String,score:Double) extends Person7(name,score)

 

6.3.6. Scala abstract class

  • If the parent class, there are certain methods can not be achieved immediately, but need to rely on different subclasses override, override achieved in different ways. In this case, the parent class can be written to these methods contain only the signature method, the method body free form, this form is called an abstract method;
  • A class, or if it contains an abstract method of the abstract Field, it must be used to abstract class declared abstract class, which is not to be instantiated;
  • When the abstract method of the abstract class overridden in subclasses can override keyword without;
  • for example:
Package cn.itcast.extends_demo abstract class Person9 ( Val name: String) { 
  // return type must be noted that, otherwise return to the default Uni T DEF the sayHello : String DEF sayBye: String 
} class Student9 (name: String) the extends Person9 (name) { 
  // return type must be noted that, otherwise default DEF the sayHello: String = "the Hello," + name DEF sayBye: String = "Bye," name + 
} Object Student9 { DEF main (args: the Array [String]) { Val S = new new Student9 ( "tom") println (s.sayHello) println



 

 



 
 


 
   
   
    (s.sayBye)
  }
}

 

6.3.7. Scala abstract field

  • If the parent class, defined in the field, but did not give an initial value, this field abstract field;
  • for example:
package cn.itcast.extends_demo

abstract class Person10 (val name:String){
//抽象fields
    val age:Int
}
class Student10(name: String) extends Person10(name) {
   val age: Int = 50
}

Guess you like

Origin www.cnblogs.com/mediocreWorld/p/11367071.html