Chapter 10 Inheritance
10.1 inherited class
class Person { var name = "" }
class Employee extends Person { var salary = 0.0 def description = "An employee with name " + name + " and salary " + salary }
prompt:
10.2 Overriding methods
class Person { var name = "" override def toString = getClass.getName + "[name=" + name + "]" }
class Employee extends Person { var salary = 0.0 override def toString = super.toString + "[salary=" + salary + "]" }
Type checking and conversion 10.3
1) classOf [String] like Java have String.class
2) obj.isInstanceOf [T] Like the Java Obj isInstanceof T
3) obj.asInstanceOf [T] Like the Java (T) Obj
println("Hello".isInstanceOf[String]) println("Hello".asInstanceOf[String]) println(classOf[String])
10.4 protected fields and methods
Scala protected in Java to be more stringent than that, namely, inheritance can access only, not the next time the same package.
Construction 10.5 superclass
Class has a constructor, and any number of main secondary structure, while each auxiliary constructor must be based on the previously defined auxiliary builder
Or call the primary constructor start. Auxiliary constructors eventually invoke the subclass constructor, only the primary constructor can call the superclass
Constructor. Auxiliary constructor will never directly call the constructor of the superclass. In Scala's constructor, you can not call super (params).
** Auxiliary current class constructor], will eventually call the primary constructor [] of the current class
** subclass primary constructor, eventually calling the parent class constructor (the constructor may be an auxiliary, may be a master builder)
class Dog(age: Int){
def this(){
this(10)
}
} val dog = new Dog(20) val dog = new Dog()
class Person(val name: String, val age: Int) { override def toString = getClass.getName + "[name=" + name + ",age=" + age + "]" }
class Employee(name: String, age: Int, val salary : Double) extends Person(name, age) { override def toString = super.toString + "[salary=" + salary + "]" }
10.6 rewrite field (overwrite field)
class Person1 (Val name: String, var Age: Int) { println ( "primary constructor has been called" ) Val School = "Wudaokou Vocational and Technical College" DEF SLEEP = "8 hours" the override DEF toString: String = "My the school is: "+ school +", my name: "+ name +", my age: "+ Age }
class Person2(name: String, age: Int) extends Person1(name, age){
override val school: String = "清华大学"
}
transfer:
// overwrite field Val the Person = new new Person2 ( "Nick", 20 ) println (the Person) // primary constructor has been called // My school is: Tsinghua University, my name: nick, My Age: 20
Screaming Tip:
1, def def can only rewrite another
2, val val or another rewrite only def can not be rewritten without parameters var
3, var can only rewrite a abstract var
A = val "123" // def can not be rewritten def = A "123" def // no parameters can be rewritten val
What is abstract var?
sbstract class person3 {
var name: String // This is an abstract var
}
10.7 anonymous subclass
class Person(val name: String) { override def toString = getClass.getName + "[name=" + name + "]" }
使用: val alien = new Person("Fred") { def greeting = "Greetings, Earthling! My name is Fred." }
println(alien.greeting)
10.8 abstract class
abstract class Person(val pname: String) { val id: Int // No initializer—this is an abstract field with an abstract getter method var name: String // Another abstract field, with abstract getter and setter methods def idString: Int // No method body—this is an abstract method }
class Employee(pname: String) extends Person(pname) { val id = 5; var name = ">>>" def idString = pname.hashCode // override keyword not required }
10.9 construction order and defined in advance
10.10 Scala inheritance hierarchy