The "extendable language" of Scala learning

Source: Internet
Author: User
Tags constructor inheritance
Let me start with Scala and get to know Scala in this chapter:
1, why I study Scala, the difference between her and Java
2, basic types in Scala and Operations
3, Scala classes and objects
Why I study Scala, the difference between her and Java

Scala is a programming language that combines functional programming with object-oriented thinking. She can write various styles of language including instruction, but the essence wants us to write functional style. I like the functional style, this style gives greater flexibility and stability. And Scala can be mixed with the Java language and has an almost identical ecosystem to the Java language, and Scala's features and performance allow her to better implement big data and algorithmic things like spark. That's why I chose Scala as a second language.

Recommend an article on the status of Scala:
https://yq.aliyun.com/articles/54799

Object-oriented thinking in Java:
http://blog.csdn.net/weixin_39471249/article/details/79325275

The idea in Scala programming: Scala is object-oriented. She is more purely object-oriented than Java (), in the Scala world, everything is an object, including numbers, operators. In Scala, for example, describing 1+2 is actually equivalent to calling the + method in the Int class. The name definition is the same as the method name and the operator. Then there are assembled objects, in Java, through abstract class inheritance, class instance assembly, interface implementation, Scala's characteristics (trait) has a more powerful ability to achieve "fat thin Interface", stacking changes and other extensions. She looks like the interface in Java, multiple inheritance, but in fact, the trait is a trait, not with the interface, multiple inheritance confused, which is more helpful to our understanding. Scala is a functional type. we want to be clear that 1th, in functional programming, the function is the first equivalent, that is, the function is also the value, and the integer and the string is the same status. It can be passed as a parameter to other functions, or it can be returned from a function as a result or saved in an object. You can define other functions in a function, just as you would define other integers in a function, or you can define an anonymous function and arbitrarily insert it anywhere in your code, as you would with a literal of 42. Functions are the concept of first-class values, simplifying the abstraction of operators and the creation of control structures. 2nd, the operation of a program in functional programming should be to map input values to output values instead of in-place modification data. For the 2nd, I prefer another explanation that the method should not have side effects. The simple understanding is that, regardless of the same parameters and invocations, the resulting results are always the same. basic types and operations in Scala

Byte\short\char\int\float\long\double\boolean

In addition to the initial capitalization, the other attributes are identical to the 8 basic types in Java. One thing we need to be aware of is that Scala's operations are actually calls to methods. We can see that the Int class is filled with various arithmetic methods.


There is no longer much to describe here. Let's put our energies into other places to get a bigger harvest. classes and objects in Scala

Let's look at some basic code:

Package

/**
  * @author Wangzha * * *
/Class Firstscala (Name:string, Age:int, sex:string) {
    / /Override construction Method
    def this (name:string, age:int) = {This
        (name, age, "Man")
    }
    //member variable
    val time = System.curre Nttimemillis ()/

    Study (time:long): Long = System.currenttimemillis ()/1000-time

    def getName (): Stri ng = this.name
    //overriding ToString method
    override def tostring:string = name + "s age is" + age + "and sex are" + sex
  
   }


  

The creation of classes in Scala is basically similar to Java, including the Declaration of objects. The difference is in the constructor, where the default constructor is followed by the class name (called the primary constructor), and the overriding constructor eventually passes through the main constructor. You cannot define static members in Scala, but instead use singleton objects instead (Singleton object). The singleton object replaces the class keyword singleton with the object keyword with no arguments, and the class can. Because singleton objects are not instantiated with the new keyword, there is no chance of entering or exiting parameters. Each singleton object is implemented as an instance of a fictional class. and point to static variables, so they have and Java
Static classes have the same initialization semantics. Singleton objects are initialized the first time they are accessed. A singleton object with the same name as a class is called a companion object, and that class is called the companion class. The associated class and the associated object must be defined in a source file, and the class and associated objects can access their private members to each other. A singleton object without a companion class is called a standalone object, usually as a tool class, or as an entry point for a Scala app (see below).

object Firstscala {def main (args:array[string]): Unit = {val F = new Firstscala ("W Angzha "," Thread.Sleep () println (F.getname () + "studied for a long time:" + f.study (F.time))}} 

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.