spark notes 3

scala entry

1. basic grammar

  • Identifiers are case sensitive
  • Capitalize the first letter of the name of class
  • Method name to be lowercase first letter
  • Program file name to be the same as the object name
  • The main method is to program the inlet Scala, Scala each program must define this method.
  • Identifiers
    only contain characters, numbers, and underscores, and can only begin with the characters and the underscore, as in Scala identifiers: name, _value, test_1 and so on.
  • Mixing identifier
    mixed character identified by a numeric identifier, and a computing underscore identifiers, such as: unary _ +, var_ =. Here, unary_ + defines a unary + operator, myvar_ = name is defined as a method for an assignment operator.
  • Text identifier
    contains any character with a `` symbol is a literal identifier

2. Data Types

The same type java, memory space occupied and accuracy are the same
but there is no visual java in these

Types of description
Unit It represents no value
Null Empty or a null reference
Nothing Each of the other types of sub-types, including non-value
Any Any type of super-type, any type of object is Any
AnyRef Any reference type supertype
  • Variables and constants
    var to declare variables, using the val declared value (that is constant)

3. The use of function definitions and

General Definitions

Def defined by function, in Scala, needs to function parameter specifies the type of signature follows a specific format

def functionName ([list of parameters]) : [return type] = {
   function body
   return [expr]
}

No-argument function

If the function without parameters, when the call can not write braces, the function body statements if only one expression, you can omit the braces function of
the operation record it

  • Part of the function of the application
    which is underlined to achieve, is a characteristic of a language in the past and learned very different

    You can use an underscore _ a part of the application function, the result will get another function. Scala underlined use different things in different contexts, you can usually see it as a magical wildcard unnamed. In the context of _ + {2}, the anonymity parameter which represents a. You can use any part of the parameters in the parameter list, not just the last one. You can use it like

def add(x: Int, y: Int) :Int = {
    return x + y
}
var add1 = add(1, _:Int)
var add2 = add1(2)
println(add2)

The effect is such that when the first call to add back the parameters with an underscore to replace lost

Cauchy function of

Now I understand this is to say that there are multiple brackets, each of the brackets is a parameter and its type annotations

Variable length parameter

The same type of function parameters, the number is variable, at least one of
this example is to capitalize words entered

// 把所有单词改为首字母大写
def capitalizeAll(args: String*) = {
    args.map { arg =>
        arg.capitalize
    }
}
var str = capitalizeAll("hello", "world")
println(str)

4. Class

The method defined by the class def field values ​​defined by val

Class constructor

The inside of a section talking about the constructor should be is instantiated when the object parameter passed to the class method, and java is not the same body which does not need to specify a class name is the class name of the method
which is a showcase scala "for expression "example, blue, black, white assignment of color are binding in the expression above

class Calculator(brand: String) {
    /**
     * 一个构造函数
     */
    val color: String = if (brand == "shiyanlou") {
        "blue"
    } else if (brand == "hello") {
        "black"
    } else {
        "white"
    }

    // 实例方法
    def add(m: Int, n: Int): Int = m + n
}

Class inheritance

Inherited the same keywords and java but also extends followed by the name of the parent class is followed by parentheses

Method overloading

Scientific calculator category above that, inside the log method, in the following example which is overloaded
test the properties of the original color, the two-parameter log method, and the new method overloading

Inherited abstract class

5. qualities

Trait (Traits) is a collection of some of the fields and behavior, can be extended or mixed (mixin) your class. Unlike class inheritance, class can be extended a plurality Traits.
By with keywords, a class can be extended multiple traits

But let's have a class character way or extends, extends just behind the character you can add more
to use to connect with several traits between

6. collection

Scala 提供了丰富的集合库,包括:列表(List)、集合(Set)、映射(Map)、选项(Option)、元组(Tuple)

set和list的话,最直观的结果是这样的

而immutable的意思是永恒不变的,但是并不能帮助我在字面意思上理解set和list的不同,还是等具体实践操作吧
map映射的话,是这样用的

用括号括起来就是元组,不禁让人联想起来数据库里面关系模式什么的

选项option的话我理解这个字面是什么意思,但是没有搞清楚这个操作是在干什么

查了人家的博客,好像是说,Scala鼓励你在变量和函数返回值可能不会引用任何值的时候使用Option类型。
在没有值的时候,使用None,这是Option的一个子类。如果有值可以引用,就使用Some来包含这个值。
Some也是Option的子类

Option类型的值通常作为Scala集合类型(List,Map等)操作的返回类型。比如Map的get方法。Option有两个子类别,Some和None。当程序回传Some的时候,代表这个函式成功地给了你一个String,而你可以透过get()函数拿到那个String,如果程序返回的是None,则代表没有字符串可以给你

参考资料

Scala 技术笔记之 Option Some None

Guess you like

Origin www.cnblogs.com/ltl0501/p/12107496.html