楼主: ReneeBK
1296 8

Scala Tutorial [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49407 个
通用积分
51.8704
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

本帖隐藏的内容

Scala Tutorial.pdf (1.71 MB, 需要: 5 个论坛币)



About theTutorial

Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. Scala has been created by Martin Odersky and he released the first version in 2003. Scala smoothly integrates the features of object-oriented and functional languages. This tutorial explains the basics of Scala in a simple and reader-friendly way.


Audience
This tutorial has been prepared for beginners to help them understand the basics of Scala in simple and easy steps. After completing this tutorial, you will find yourself at a moderate level of expertise in using Scala from where you can take yourself to next levels.
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Tutorials Tutorial tutor Point SCALA

本帖被以下文库推荐

沙发
ReneeBK 发表于 2017-1-21 04:09:06 |只看作者 |坛友微信交流群
  1. Function Declarations

  2. A Scala function declaration has the following form −

  3. def functionName ([list of parameters]) : [return type]
  4. Methods are implicitly declared abstract if you don’t use the equals sign and the method body.

  5. Function Definitions

  6. A Scala function definition has the following form −

  7. Syntax

  8. def functionName ([list of parameters]) : [return type] = {
  9.    function body
  10.    return [expr]
  11. }
  12. Here, return type could be any valid Scala data type and list of parameters will be a list of variables separated by comma and list of parameters and return type are optional. Very similar to Java, a return statement can be used along with an expression in case function returns a value. Following is the function which will add two integers and return their sum −

  13. Syntax

  14. object add {
  15.    def addInt( a:Int, b:Int ) : Int = {
  16.       var sum:Int = 0
  17.       sum = a + b
  18.       return sum
  19.    }
  20. }
  21. A function, that does not return anything can return a Unit that is equivalent to void in Java and indicates that function does not return anything. The functions which do not return anything in Scala, they are called procedures.

  22. Syntax

  23. Here is the syntax −

  24. object Hello{
  25.    def printMe( ) : Unit = {
  26.       println("Hello, Scala!")
  27.    }
  28. }
  29. Calling Functions

  30. Scala provides a number of syntactic variations for invoking methods. Following is the standard way to call a method −

  31. functionName( list of parameters )
  32. If a function is being called using an instance of the object, then we would use dot notation similar to Java as follows −

  33. [instance.]functionName( list of parameters )
  34. Try the following example program to define and then call the same function.

  35. Example

  36. object Demo {
  37.    def main(args: Array[String]) {
  38.       println( "Returned Value : " + addInt(5,7) );
  39.    }
  40.    
  41.    def addInt( a:Int, b:Int ) : Int = {
  42.       var sum:Int = 0
  43.       sum = a + b

  44.       return sum
  45.    }
  46. }
  47. Save the above program in Demo.scala. The following commands are used to compile and execute this program.

  48. Command

  49. \>scalac Demo.scala
  50. \>scala Demo
  51. Output

  52. Returned Value : 12
复制代码

使用道具

藤椅
ReneeBK 发表于 2017-1-21 04:12:45 |只看作者 |坛友微信交流群
  1. Scala - Variables

  2. https://www.tutorialspoint.com/scala/scala_variables.htm
  3. Copyright © tutorialspoint.com

  4. Variables are nothing but reserved memory locations to store values. This means that when you create a variable, you reserve some space in memory.

  5. Based on the data type of a variable, the compiler allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.

  6. Variable Declaration

  7. Scala has a different syntax for declaring variables. They can be defined as value, i.e., constant or a variable. Here, myVar is declared using the keyword var. It is a variable that can change value and this is called mutable variable. Following is the syntax to define a variable using var keyword −

  8. Syntax

  9. var myVar : String = "Foo"
  10. Here, myVal is declared using the keyword val. This means that it is a variable that cannot be changed and this is called immutable variable. Following is the syntax to define a variable using val keyword −

  11. Syntax

  12. val myVal : String = "Foo"
  13. Variable Data Types

  14. The type of a variable is specified after the variable name and before equals sign. You can define any type of Scala variable by mentioning its data type as follows −

  15. Syntax

  16. val or val VariableName : DataType = [Initial Value]
  17. If you do not assign any initial value to a variable, then it is valid as follows −

  18. Syntax

  19. var myVar :Int;
  20. val myVal :String;
  21. Variable Type Inference

  22. When you assign an initial value to a variable, the Scala compiler can figure out the type of the variable based on the value assigned to it. This is called variable type inference. Therefore, you could write these variable declarations like this −

  23. Syntax

  24. var myVar = 10;
  25. val myVal = "Hello, Scala!";
  26. Here, by default, myVar will be Int type and myVal will become String type variable.

  27. Multiple assignments

  28. Scala supports multiple assignments. If a code block or method returns a Tuple (Tuple − Holds collection of Objects of different types), the Tuple can be assigned to a val variable. [Note − We will study Tuples in subsequent chapters.]

  29. Syntax

  30. val (myVar1: Int, myVar2: String) = Pair(40, "Foo")
  31. And the type inference gets it right −

  32. Syntax

  33. val (myVar1, myVar2) = Pair(40, "Foo")
  34. Example Program

  35. The following is an example program that explains the process of variable declaration in Scala. This program declares four variables — two variables are defined with variable declaration and remaining two are without variable declaration.

  36. Example

  37. object Demo {
  38.    def main(args: Array[String]) {
  39.       var myVar :Int = 10;
  40.       val myVal :String = "Hello Scala with datatype declaration.";
  41.       var myVar1 = 20;
  42.       val myVal1 = "Hello Scala new without datatype declaration.";
  43.       
  44.       println(myVar); println(myVal); println(myVar1);
  45.       println(myVal1);
  46.    }
  47. }
  48. Save the above program in Demo.scala. The following commands are used to compile and execute this program.

  49. Command

  50. \>scalac Demo.scala
  51. \>scala Demo
  52. Output

  53. 10
  54. Hello Scala with datatype declaration.
  55. 20
  56. Hello Scala without datatype declaration.
  57. Variable Scope

  58. Variables in Scala can have three different scopes depending on the place where they are being used. They can exist as fields, as method parameters and as local variables. Below are the details about each type of scope.

  59. Fields

  60. Fields are variables that belong to an object. The fields are accessible from inside every method in the object. Fields can also be accessible outside the object depending on what access modifiers the field is declared with. Object fields can be both mutable and immutable types and can be defined using either var or val.

  61. Method Parameters

  62. Method parameters are variables, which are used to pass the value inside a method, when the method is called. Method parameters are only accessible from inside the method but the objects passed in may be accessible from the outside, if you have a reference to the object from outside the method. Method parameters are always immutable which are defined by val keyword.

  63. Local Variables

  64. Local variables are variables declared inside a method. Local variables are only accessible from inside the method, but the objects you create may escape the method if you return them from the method. Local variables can be both mutable and immutable types and can be defined using either var or val.
复制代码

使用道具

板凳
ReneeBK 发表于 2017-1-21 04:16:38 |只看作者 |坛友微信交流群
  1. Scala - Classes & Objects

  2. https://www.tutorialspoint.com/scala/scala_classes_objects.htm
  3. Copyright © tutorialspoint.com

  4. This chapter takes you through how to use classes and objects in Scala programming. A class is a blueprint for objects. Once you define a class, you can create objects from the class blueprint with the keyword new. Through the object you can use all functionalities of the defined class.

  5. The following diagram demonstrates the class and object by taking an example of class student, which contains the member variables nameandrollnonameandrollno and member methods setName(setName( and setRollNo). Finally all are members of the class. Class is a blue print and objects are real here. In the following diagram, Student is a class and Harini, John, and Maria are the objects of Student class, those are having name and roll-number.

  6. Scala Classes and Objects
  7. Basic Class

  8. Following is a simple syntax to define a basic class in Scala. This class defines two variables x and y and a method: move, which does not return a value. Class variables are called, fields of the class and methods are called class methods.

  9. The class name works as a class constructor which can take a number of parameters. The above code defines two constructor arguments, xc and yc; they are both visible in the whole body of the class.

  10. Syntax

  11. class Point(xc: Int, yc: Int) {
  12.    var x: Int = xc
  13.    var y: Int = yc

  14.    def move(dx: Int, dy: Int) {
  15.       x = x + dx
  16.       y = y + dy
  17.       println ("Point x location : " + x);
  18.       println ("Point y location : " + y);
  19.    }
  20. }
  21. As mentioned earlier in this chapter, you can create objects using a keyword new and then you can access class fields and methods as shown below in the example −

  22. Example

  23. import java.io._

  24. class Point(val xc: Int, val yc: Int) {
  25.    var x: Int = xc
  26.    var y: Int = yc
  27.    
  28.    def move(dx: Int, dy: Int) {
  29.       x = x + dx
  30.       y = y + dy
  31.       println ("Point x location : " + x);
  32.       println ("Point y location : " + y);
  33.    }
  34. }

  35. object Demo {
  36.    def main(args: Array[String]) {
  37.       val pt = new Point(10, 20);

  38.       // Move to a new location
  39.       pt.move(10, 10);
  40.    }
  41. }
  42. Save the above program in Demo.scala. The following commands are used to compile and execute this program.

  43. Command

  44. \>scalac Demo.scala
  45. \>scala Demo
  46. Output

  47. Point x location : 20
  48. Point y location : 30
  49. Extending a Class

  50. You can extend a base Scala class and you can design an inherited class in the same way you do it in Java (use extends key word), but there are two restrictions: method overriding requires the override keyword, and only the primary constructor can pass parameters to the base constructor. Let us extend our above class and add one more class method.

  51. Example

  52. Let us take an example of two classes Point class assameexampleasaboveassameexampleasabove and Location class is inherited class using extends keyword. Such an ‘extends’ clause has two effects: it makes Location class inherit all non-private members from Point class, and it makes the type Location a subtype of the type Point class. So here the Point class is called superclass and the class Location is called subclass. Extending a class and inheriting all the features of a parent class is called inheritance but Scala allows the inheritance from just one class only.

  53. Note − Methods move method in Point class and move method in Location class do not override the corresponding definitions of move since they are different definitions forexample,theformertaketwoargumentswhilethelattertakethreeargumentsforexample,theformertaketwoargumentswhilethelattertakethreearguments.

  54. Try the following example program to implement inheritance.

  55. import java.io._

  56. class Point(val xc: Int, val yc: Int) {
  57.    var x: Int = xc
  58.    var y: Int = yc
  59.    
  60.    def move(dx: Int, dy: Int) {
  61.       x = x + dx
  62.       y = y + dy
  63.       println ("Point x location : " + x);
  64.       println ("Point y location : " + y);
  65.    }
  66. }

  67. class Location(override val xc: Int, override val yc: Int,
  68.    val zc :Int) extends Point(xc, yc){
  69.    var z: Int = zc

  70.    def move(dx: Int, dy: Int, dz: Int) {
  71.       x = x + dx
  72.       y = y + dy
  73.       z = z + dz
  74.       println ("Point x location : " + x);
  75.       println ("Point y location : " + y);
  76.       println ("Point z location : " + z);
  77.    }
  78. }

  79. object Demo {
  80.    def main(args: Array[String]) {
  81.       val loc = new Location(10, 20, 15);

  82.       // Move to a new location
  83.       loc.move(10, 10, 5);
  84.    }
  85. }
  86. Save the above program in Demo.scala. The following commands are used to compile and execute this program.

  87. Command

  88. \>scalac Demo.scala
  89. \>scala Demo
  90. Output

  91. Point x location : 20
  92. Point y location : 30
  93. Point z location : 20
  94. Implicit Classes

  95. Implicit classes allow implicit conversations with class’s primary constructor when the class is in scope. Implicit class is a class marked with ‘implicit’ keyword. This feature is introduced in Scala 2.10.

  96. Syntax − The following is the syntax for implicit classes. Here implicit class is always in the object scope where all method definitions are allowed because implicit class cannot be a top level class.

  97. Syntax

  98. object <object name> {
  99.    implicit class <class name>(<Variable>: Data type) {
  100.       def <method>(): Unit =
  101.    }
  102. }
  103. Example

  104. Let us take an example of an implicit class named IntTimes with the method times. It means the times  contain a loop transaction that will execute the given statement in number of times that we give. Let us assume the given statement is “4 times println “Hello”“Hello”” means the println “”Hello”“”Hello” statement will execute 4 times.

  105. The following is the program for the given example. In this example two object classes are used RunandDemoRunandDemo so that we have to save those two classes in different files with their respective names as follows.

  106. Run.scala − Save the following program in Run.scala.

  107. object Run {
  108.    implicit class IntTimes(x: Int) {
  109.       def times [A](f: =>A): Unit = {
  110.          def loop(current: Int): Unit =
  111.          
  112.          if(current > 0){
  113.             f
  114.             loop(current - 1)
  115.          }
  116.          loop(x)
  117.       }
  118.    }
  119. }
  120. Demo.scala − Save the following program in Demo.scala.

  121. import Run._

  122. object Demo {
  123.    def main(args: Array[String]) {
  124.       4 times println("hello")
  125.    }
  126. }
  127. The following commands are used to compile and execute these two programs.

  128. Command

  129. \>scalac Run.scala
  130. \>scalac Demo.scala
  131. \>scala Demo
  132. Output

  133. Hello
  134. Hello
  135. Hello
  136. Hello
  137. Note −

  138. Implicit classes must be defined inside another class/object/trait notintoplevelnotintoplevel.

  139. Implicit classes may only take one non –implicit argument in their constructor.

  140. Implicit classes may not be any method, member or object in scope with the same name as the implicit class.

  141. Singleton Objects

  142. Scala is more object-oriented than Java because in Scala, we cannot have static members. Instead, Scala has singleton objects. A singleton is a class that can have only one instance, i.e., Object. You create singleton using the keyword object instead of class keyword. Since you can't instantiate a singleton object, you can't pass parameters to the primary constructor. You already have seen all the examples using singleton objects where you called Scala's main method.

  143. Following is the same example program to implement singleton.

  144. Example

  145. import java.io._

  146. class Point(val xc: Int, val yc: Int) {
  147.    var x: Int = xc
  148.    var y: Int = yc
  149.    
  150.    def move(dx: Int, dy: Int) {
  151.       x = x + dx
  152.       y = y + dy
  153.    }
  154. }

  155. object Demo {
  156.    def main(args: Array[String]) {
  157.       val point = new Point(10, 20)
  158.       printPoint

  159.       def printPoint{
  160.          println ("Point x location : " + point.x);
  161.          println ("Point y location : " + point.y);
  162.       }
  163.    }
  164. }
  165. Save the above program in Demo.scala. The following commands are used to compile and execute this program.

  166. Command

  167. \>scalac Demo.scala
  168. \>scala Demo
  169. Output

  170. Point x location : 10
  171. Point y location : 20
复制代码

使用道具

报纸
auirzxp 学生认证  发表于 2017-1-21 04:33:58 |只看作者 |坛友微信交流群
已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

地板
huhuhuhu 发表于 2017-1-21 07:41:13 |只看作者 |坛友微信交流群
谢谢分享
已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

7
franky_sas 发表于 2017-1-21 08:17:48 |只看作者 |坛友微信交流群

使用道具

8
HappyAndy_Lo 发表于 2017-1-21 13:55:49 |只看作者 |坛友微信交流群

使用道具

9
erm2wi 发表于 2017-1-21 19:22:24 |只看作者 |坛友微信交流群
Thanks for sharing

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-26 18:41