Inheritance in Scala and Java also use the extends keyword;
The subclass can inherit or override the field and method of the parent class, and can also implement the field and method specific to the subclass
verride
In Scala, if a subclass wants to override a field or method in the parent class, use the override keyword;
super
To call the overridden method in the parent class in the subclass, use the super keyword to indicate the parent class method to be called.
note
Final modified classes cannot be inherited
Final modified fields or methods cannot be overwritten
Private modified fields and methods cannot be inherited by subclasses
Use the override keyword to override the val field
Use the override keyword to re-assign the var field (note that it is not a redefinition)
The override keyword can help developers find errors in the code as early as possible, such as method name spelling errors, parameter errors, etc., so it is recommended to add the override keyword when overwriting;
Extension: protected description
Like Java, Scala can also use the protected keyword to modify fields and methods.
But it is more strict than Java: only the inheritance relationship can be accessed, and it is not possible under the same package
In the subclass, you can directly access the protected modified field and method of the parent class without using the super keyword;
You can also use the protected[this] keyword to only allow access to the field and method of the parent class in the current class, and not allow access to the field and method of the parent class through other subclass objects.
package cn.hanjiaxiaozhi.extendsdemo
/**
* Author hanjiaxiaozhi
* Date 2020/7/16 15:52
* Desc
*/class Person {
val name ="super"var age =18
def sayName ={
println("Person--sayName")}
def sayHello={
println("hello")}}//Student继承了Person,将相当于有了Person中的非私有的一切class Student extends Person{
//如果子类要覆盖可以使用overrideoverrideval name ="sub"//val修饰的可以覆盖的
age =20//var修饰的可以重新赋值
overridedef sayName ={
//super.sayName //super表示调用父类的
println("Student--sayName")}
//sayHello如果不覆盖就直接继承过来了/*override
def sayHello={
println("hello")
}*/
//子类也可以定义自己的变量val nickName ="xx"
//子类也可以编写自己的方法def saybye():Unit={
println("good bye~~"+ nickName)}}
object Test{
def main(args: Array[String]):Unit={
val s: Student =new Student()
s.sayName
s.sayHello
s.saybye
println("==================")val p: Person =new Student()//多态,父类类型接收子类对象,不需要强转
p.sayName //本质是调用的是Student的
p.sayHello//本质是调用的是Student的//p.saybye //如果使用的Person接收Student对象,那么不能调用Student中的方法//如果非得调用子类特有的方法得强转
p.asInstanceOf[Student].saybye()}}
Type judgment, acquisition, conversion
isInstanceOf 和 asInstanceOf
obj.isInstanceOf[XX类名]
Determine whether obj is an instance of type XX (determine whether the object is an instance of the specified class and its subclasses)
Similar to in Java: obj instanceof xx
obj.asInstanceOf[XX class name]
Convert obj into an instance of type XX (the new parent class cannot be forced to be a subclass)
Similar to Java: (XX)obj
getClass 和 classOf
obj.getClass
Can accurately obtain the type of object
It is in Java: object.getClass
classOf[XX class name]
Can accurately obtain the type of the class
Similar to Java: class name.class
note:
If the object is null,
isInstanceOf must return false,
asInstanceOf must return null;
Scala and Java type checking and conversion analogy
Scala
Java
obj.isInstanceOf[XX类名]
obj instanceof XX类名
obj.asInstanceOf[XX class name]
(XX class name)obj
obj.getClass
obj.getClass
classOf[XX class name]
XX class name.class
Use pattern matching for type judgment
Use pattern matching for type judgment (pattern matching will be discussed later)
Functionally speaking, it is the same as isInstanceOf. It mainly determines whether it is an object of this class or its subclasses. It is not an accurate judgment.
Syntactically speaking, it is similar to the switch case in Java
In actual development, such as spark source code, a large number of pattern matching is used for type judgment, which is more concise, maintainable, and extensible
java: object.getClass or class name.Class or Class.forName("package class path")
Conversion
scala: object.asInstanceOf[type]
java: (type) object
Constructor execution order
Each class in Scala can have a main constructor and any number of auxiliary constructors,
The first line of the auxiliary constructor must call other auxiliary constructors or the main constructor code;
It is impossible for the auxiliary constructor of the subclass to directly call the constructor of the parent class, and the constructor of the parent class can only be called in the main constructor of the subclass.
Fields that have been defined by the constructor of the parent class. When the subclass is in use, it does not need to be modified with val/var, otherwise it will be considered that the subclass should override the field of the parent class.
Scala, like Java, executes the construction of the parent first and then the construction of the child
There are also main and auxiliary constructors in Scala
The auxiliary constructor eventually has to call the main constructor
Abstract class
Abstract classes, abstract methods, and abstract fields
If a class contains an abstract method or abstract field, you must use abstract to declare the class as an abstract class, which cannot be instantiated
If in the parent class, there are some methods that cannot be implemented immediately, and need to be overridden by different subclasses, and rewrite to implement different methods. At this time, these methods in the parent class can be written to contain only method signatures, without The form of method body, this form is called abstract method
If a field is defined in the parent class, but no initial value is given, then this field is an abstract field
note
When the abstract method of the abstract class is overridden in the subclass, the override keyword can be omitted, but it is recommended to add; the override keyword must be used to override the non-abstract method of the parent class
There can be ordinary fields and methods in abstract classes
Extension: Anonymous inner class
Description
Anonymous inner classes are very common in Scala. The source code of Spark uses a lot of anonymous inner classes;
An anonymous inner class is to define a subclass without a name, create its object directly, and then assign a reference to the object to a variable
Usually the anonymous inner class object is passed to other methods or functions for use.