Scala Abstract class

Source: Internet
Author: User

Scala Abstract class

Abstract class

Scala defines abstract classes

  1. An abstract class cannot be instantiated.

  2. A class that contains an abstract method must be defined as an abstract class, that is, using the abstract keyword to define a class

  3. Abstract classes can have abstract types (type)

  4. Abstract classes can contain abstract fields

  5. ...............................

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.