Scala Abstract class
Abstract class
Scala defines abstract classes
An abstract class cannot be instantiated.
A class that contains an abstract method must be defined as an abstract class, that is, using the abstract keyword to define a class
Abstract classes can have abstract types (type)
Abstract classes can contain abstract fields
...............................
The following is a simple definition of an abstract class
/** * * @param name * @param age */abstract Class staff (name:string, age:int) {/** * abstract field in Scala */var _departme nt:string def department:string = _department def department_= (newvalue:string): Unit = _department = NewValue/** * The definition parameter is empty, the return value is the abstract method of unit */def doWork (): Unit/** * Abstract method * @param value * @return */def Echo (value:string): String def work () = {println (' start work ') This.dowork () println ("Work End")}
The following is the inheritance and implementation of the parent class above
/** * only the primary constructor of a subclass can call the parent class's main constructor * @param name * @param age * @ Param address */class normalstaff (name: string, age: int, address: String) extends staff (name: string, age: int) { override var _department: string = _ def this (Name: string, age: int) { this (name, age, "Unknow"); } // implementing the abstract method of the parent class override def dowork (): unit = { println ("I ' m Doing some job ") } /** * abstract methods * You must use the Override keyword * @param value * @return */ override def echo (value: string): string = value override def tostring = "Normalstaff [name=" + this.name + ", age=" + this.age + ", department=" + this._department + "]"}
Abstract types of abstract classes
Class Foodabstract class Animal {type Suitablefood <: Food def Eat (Food:suitablefood)}class Grass extends Foodclass Cow extends Animal {type Suitablefood = Grass override Def eat (Food:grass) {}}
Use the Type keyword to define an abstract type, type Suitablefood, and indicate that his upper bound is food.
Abstract class Abstract {type T def transform (x:t): t Val initial:t var current:t}class concrete extends Abstract {Override type T = String override def transform (x:t): t = x + x//In Java This is the final variable, so you must initialize the assignment override Val initial: T = "string"//in Java This private type variable, in Scala you can yoga the underscore value override var current:t = _}
In this example, the Type keyword is used to define the abstract type T, in the implementation class to overwrite and indicate the concrete type of the abstract type, override type T = String:
Abstract fields of abstract classes
Abstract class Abstract {type T def transform (x:t): t val initial:t var current:t}
In this case, the member variables defined with Val and Var are not initialized, and after Scala's compiled bytecode, it is an abstract method in the Java class after compilation.
C:\workspace5-gitosc\scala-sample\out\production\scala-sample\com\usoft3>javap-p Abstract.classCompiled from " Test.scala "public abstract class Com.usoft3.Abstract {public abstract Java.lang.Object transform (java.lang.Object); Public abstract Java.lang.Object initial (); Public abstract Java.lang.Object current (); public abstract void Current_$eq (Java.lang.Object); public com.usoft3.Abstract ();}
=====================
Val Initial:tvar current:t
The abstract approach to the corresponding Java class is to
Public abstract Java.lang.Object Initial ();p ublic abstract Java.lang.Object current ();
============================end============================
Scala Abstract class