ImageVerifierCode 换一换
格式:DOCX , 页数:20 ,大小:37.43KB ,
资源ID:20019903      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/20019903.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(scala指导译稿Word文档格式.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

scala指导译稿Word文档格式.docx

1、本文的目的读者是那些已经具有一定编程经验,而想尝试一下Scala语言的人们。要阅读本文,你应当具有基础的面向对象编程的概念,尤其是Java语言的。2 A first example 第一个例子 As a first example, we will use the standard Hello worldprogram. It is not very fasci- nating but makes it easy to demonstrate the use of the Scala tools without knowing too much about the language. Here

2、 is how it looks:作为学习Scala的第一部,我们将首先写一个标准的HelloWorld,这个虽然不是很有趣,但是它可以是你对Scala有一个最直观的认识而不需要太多关于这个语言的知识。我们的Hello world看起来像这样:objectHelloWorld defmain(args: ArrayString) println(”Hello, world!”)The structure of this program should be familiar to Java programmers: it consists of one method called main wh

3、ich takes the command line arguments, an array of strings, as parameter; the body of this method consists of a single call to the pre- defined method println with the friendly greeting as argument. The main method does not return a value (it is a procedure method). Therefore, it is not necessary to

4、declare a return type.程序的结构对Java程序员来说可能很令人怀念:它由一个main函数来接受命令行参数,也就是一个String数组。这个函数的唯一一行代码把我们的问候语传递给了一个叫println的预定义函数。main函数不返回值(所以它是一个procedure method)。所以,也不需要声明返回类型。What is less familiar to Java programmers is the objectdeclaration containing the main method. Such a declaration introduces what is c

5、ommonly known as a single- ton object, that is a class with a single instance. The declaration above thus declares both a class called HelloWorld and an instance of that class, also called HelloWorld. This instance is created on demand, the first time it is used.对于Java程序员比较陌生的是关于定义了main函数的object语句。这

6、样的语句定义了一个单例对象:一个有且仅有一个实例的类。object语句在定义了一个叫HelloWorld的类的同时还定义了一个叫HelloWorld的实例。这个实例在第一次使用的时候会进行实例化。The astute reader might have noticed that the main method is not declared as static here. This is because static members (methods or fields) do not exist in Scala. Rather than defining static members, t

7、he Scala programmer declares these members in singleton objects.聪明的读者可能会发现main函数并没有使用static修饰符,这是由于静态成员(方法或者变量)在Scala中并不存在。Scala从不定义静态成员,而定义单例object取而代之。2.1 Compiling the example 编译实例 To compile the example, we use scalac, the Scala compiler. scalac works like most compilers: it takes a source file

8、as argument, maybe some options, and produces one or several object files. The object files it produces are standard Java class files.我们使用Scala编译器“scalac”来编译Scala代码。和大多数编译器一样,scalac 接受源文件名和一些选项作为参数,生成一个或者多个目标文件。scala编译生成的产物就是标准的Java类文件。If we save the above program in a file called HelloWorld.scala,

9、we can compile it by issuing the following command (the greater-than sign represents the shell prompt and should not be typed):假设我们吧上述代码保存为文件HelloWorld.scala,我们使用下面的命令编译它(大于号“”表示命令提示符,你不必输入它):2.2 Running the example 3 scalac HelloWorld.scalaThis will generate a few class files in the current directo

10、ry. One of them will be called HelloWorld.class, and contains a class which can be directly executed using the scala command, as the following section shows.这将会在当前目录生成一系列.class文件。其中的一个名为HelloWorld.class 的文件中定义了一个可以直接使用scala命令执行的类。下文中你可以看到这个例子。2.2 Running the example运行实例 Once compiled, a Scala progra

11、m can be run using the scala command. Its usage is very similar to the java command used to run Java programs, and accepts the same options. The above example can be executed using the following command, which produces the expected output:一旦完成编译,Scala程序就可以使用scala命令执行了。scala的用法和java很相似,并且连选项也大致相同。上面的

12、例子就可以使用下面的命令运行,这将会产生我们所期望的输出。 scala -classpath . HelloWorldHello, world!3 Interaction with Java与Java交互One of Scalas strengths is that it makes it very easy to interact with Java code. All classes from the java.lang package are imported by default, while others need to be imported explicitly.Scala的一个

13、强项在于可以很简单的于已有的Java代码交互,所有java.lang中的类都已经被自动导入了,而其他的类需要显式声明导入。Lets look at an example that demonstrates this. We want to obtain and format the current date according to the conventions used in a specific country, say France1 .来看看演示代码吧。我们希望对日期进行I18N的格式化处理,比如说法国。Javas class libraries define powerful ut

14、ility classes, such as Date and DateFormat. Since Scala interoperates seemlessly with Java, there is no need to implement equiv- alent classes in the Scala class librarywe can simply import the classes of the cor- responding Java packages:Java类库定义了一系列很有用的类,比如Date和DateFormat。由于Scala于Java能够进行很好的交互,我们不

15、需要在Scala类库中实现等效的代码,而只需直接吧Java的相关类导入就可以了:importjava.util.Date, Locale importjava.text.DateFormat importjava.text.DateFormat._objectFrenchDate valnow = newDatevaldf = getDateInstance(LONG, Locale.FRANCE)println(df format now)1 Other regions such as the french speaking part of Switzerland use the same

16、conventions.其他法语国家(例如瑞士法语区)使用同样的格式。4Scalas import statement looks very similar to Javas equivalent, however, it is more powerful. Multiple classes can be imported from the same package by enclosing them in curly braces as on the first line. Another difference is that when importing all the names of

17、a package or class, one uses the underscore character (_) instead of the asterisk (*). Thats because the asterisk is a valid Scala identifier (e.g. method name), as we will see later.Scala的import语句看上去与Java的非常相似,但是它更加强大。你可以使用大括号来导入同一个包里的多个类,就像上面代码中第一行所做的那样。另一个不同点是当导入一个包中所有的类或者符号时,你应该使用下划线(_)而不是星号(*)。

18、这是由于星号在Scala中是一个有效的标识符(例如作为方法名称)。这个例子我们稍后会遇到。The import statement on the third line therefore imports all members of the DateFormat class. This makes the static method getDateInstance and the static field LONG di- rectly visible.第三行的import语句导入了DataFormat类中的所有成员,这使得静态方法getDateInstance和静态变量LONG可以被直接引用

19、。Inside the main method we first create an instance of Javas Date class which by default contains the current date. Next, we define a date format using the static getDateInstance method that we imported previously. Finally, we print the current date formatted according to the localized DateFormat in

20、stance. This last line shows an interesting property of Scalas syntax. Methods taking one argument can be used with an infix syntax. That is, the expression在main函数中,我们首先建立了一个Java的Date实例。这个实例默认会包含当前时间。接下来我们一个使用刚才导入的静态函数getDateInstance定义了日期格式。最后我们将使用DataFotmat格式化好的日期打印了出来。最后一行代码显示了Scala的一个有趣的语法:只有一个参数

21、的函数可以使用()。这样一来,我们的表达式:df format nowis just another, slightly less verbose way of writing the expression其实就是下面的这个冗长的表达式的简洁写法df.format(now)This might seem like a minor syntactic detail, but it has important consequences, one of which will be explored in the next section.这看起来是一个语法细节,但是它具有一个重要的后果,我们将在下一

22、节进行说明。To conclude this section about integration with Java, it should be noted that it is also possible to inherit from Java classes and implement Java interfaces directly in Scala.总结一下与Java的交互性,我们应当注意到Scala中可以直接继承或者实现Java中的接口和类。4 Everything is an object万物皆对象Scala is a pure object-oriented language

23、in the sense that everythingis an object, including numbers or functions. It differs from Java in that respect, since Java dis- tinguishes primitive types (such as boolean and int) from reference types, and does not enable one to manipulate functions as values.Scala作为一个纯面向对象的语言,于是在Scala中万物皆对象,包括数字和函

24、数。在这方面,Scala于Java存在很大不同:Java区分原生类型(比如boolean和int)和引用类型,并且不能吧函数当初变量操纵。4.1 Numbers are objects数字和对象Since numbers are objects, they also have methods. And in fact, an arithmetic ex- pression like the following:由于数字本身就是对象,所以他们也有方法。事实上我们平时使用的算数表达式(如下例)1 + 2 * 3 / xconsists exclusively of method calls, be

25、cause it is equivalent to the following expres- sion, as we saw in the previous section:是由方法调用组成的。它等效于下面的表达式,我们在上一节见过这个描述。4.2 Functions are objects 5(1).+(2).*(3)./(x)This also means that +, *, etc. are valid identifiers in Scala.这也意味着 +,-,*,/ 在Scala中也是有效的名称。The parentheses around the numbers in the

26、 second version are necessary because Scalas lexer uses a longest match rule for tokens. Therefore, it would break the fol- lowing expression:在第二个表达式中的这些括号是必须的,因为Scala的分词器使用最长规则来进行分词。所以他会把下面的表达式:1.+(2)into the tokens 1., +, and 2. The reason that this tokenization is chosen is because 1. is a longer

27、 valid match than 1. The token 1. is interpreted as the literal 1.0, making it a Double rather than an Int. Writing the expression as:理解成表达项 1. ,+,和2的组合。这样的组合结果是由于1.是一个有效的表达项并且比表达项1要长,表达项1.会被当作1.0 ,使得它成为一个double而不是int。而下面的表达式阻止了分析器错误的理解(1).+(2)prevents 1 from being interpreted as a Double.4.2 Functi

28、ons are objects函数与对象Perhaps more surprising for the Java programmer, functions are also objects in Scala. It is therefore possible to pass functions as arguments, to store them in variables, and to return them from other functions. This ability to manipulate functions as values is one of the corners

29、tone of a very interesting programming paradigm called functional programming.也许对于Java程序员来说这比较令人惊讶,函数在Scala语言里面也是一个对象。于是吧函数作为参数进行传递、把它们存贮在变量中、或者当作另一个函数的返回值都是可能的。吧函数当成值进行操作是函数型编程语言的基石。As a very simple example of why it can be useful to use functions as values, lets consider a timer function whose aim

30、 is to perform some action every second. How do we pass it the action to perform? Quite logically, as a function. This very simple kind of function passing should be familiar to many programmers: it is often used in user-interface code, to register call-back functions which get called when some event occurs.为了解释为什么吧函数当作值进行操作是十分有用的,我们来考虑一个计时器函数。这个函数的目的是每隔一段时间就执行某些操作。那么如何吧我们要做的操作传入计时器呢?于是我们想吧他当作一个函数。这种目前的函数对于经常进行用户界面编程的程序员来说是最熟悉的:注册一个回调函数以便在事件发生后得到通知。In the following program, the timer func

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1