Scala Introductory Tutorial: Object-oriented definition classes, constructors, and inheritance in Scala

Source: Internet
Author: User

We know that everything in Scala is an object, a function is an object, a number is an object, and it's a more object-oriented language than Java.

Simple classes that define Scala
Class Point(val x:int, val y:int)      

The previous line of code is the definition of a Scala class:

    1. The first is the keyword class
    2. followed by the class name point
    3. The parentheses after the class name are the constructor's argument list, which is equivalent to the two constants that define the object, with the name X, y, and the type int

The class above is consistent with the following classes, but it's more streamlined.

Class Point(xarg:int, yarg:int){== Yarg} 

Let's write a main method to see how to use this point class.

Object App {DefMain(Args: Array[String]) {    val P = newpoint (10,20 )     println ( "point Info"  + P.+  +. Y + )   }}          

An instance of the point class is initialized in the main method with Val p = new Point (10,10), and then the p.x and P.y fields are accessible.

The constructor of a Scala object is simply the constructor of the initialized field

As an example of the point class, the Isorigin is initialized in the constructor to indicate whether this is the origin point

Class point  (val X :int, Val y: int)  {  Val Isoriginal:boolean = Span class= "pun" >{    x == 0 && y ==0  }}    

The isoriginal in the preceding code is the constructor in curly braces.

The Scala constructor defined by this
Class Point (Val X:Int,Val y:Int) {Val isoriginal:Boolean = {X== 0 && y == 0  }  def  this (xargint)  {
println< Span class= "pun" > ( "Hello, I ' m This constructor" )     this (xarg,0   }}

If the code above defines a constructor that has only one argument, the constructor calls the default constructor, Xarg, and 0 values. The constructor of this writing can do things outside of the initialization field. Between constructors is available through this (...) Called from one another.

Scala's inheritance implements inheritance

Below we implement a point's inheriting class Talkpoint this point has the ability to print its own coordinates.

Class Talkpoint(X:Int,Y:Int) extends point  (x,y)  {< Span class= "PLN" >  def Talk ()  = {    println ( span class= "str" > "my position is (" +x+ "," Span class= "pun" >+y+ ")" )   }}       

The Talkpoint class above inherits from point, noting that its default constructor, X, Y does not have a Val decoration before, and extends point (x, y) will automatically call the base class's constructor. We can call this subclass's Talk method by using the following code

Object app {  def Main (args :  Array[string])  {    val P = new talkpoint (0,0      p. ()   }}   

Run the program output: My position is (0,0)

Methods for overriding base classes

Next we create a subclass of Talkpoint, called Happytalkpoint, which overrides the Talkpoint talk method. The following code:

Class Happytalkpoint(X:Int,Y:Int) extends talkpoint ( Span class= "PLN" >x,y)   {  override def ()  = {    println ( "I ' m Happy"     super. ()   }}   

There is an override keyword in front of the DEF keyword in the talk method to indicate that the keyword must be overridden, otherwise the compiler will error, which is different from Java. In the implementation of this method we first print out the I ' M happy, and then call the method of the parent class through Super.talk ().

Let's modify the main method of the App class:

Object app {  def Main (args :  Array[string])  {    val P = new happytalkpoint (0,0      p. ()   }}   

The running program will output:

I' m happymy position is (0,0)

Scala Introductory Tutorial: Object-oriented definition classes, constructors, and inheritance in Scala

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.