如果您无法下载资料,请参考说明:
1、部分资料下载需要金币,请确保您的账户上有足够的金币
2、已购买过的文档,再次下载不重复扣费
3、资料包下载后请先用软件解压,在使用对应软件打开
AScalaTutorialforJavaprogrammersVersion1.3March15,2009MichelSchinz,PhilippHallerTranslatedtoSimplifiedChinesebyBearicePROGRAMMINGMETHODSLABORATORYEPFLSWITZERLAND21Introduction简介ThisdocumentgivesaquickintroductiontotheScalalanguageandcompiler.ItisintendedforpeoplewhoalreadyhavesomeprogrammingexperienceandwantanoverviewofwhattheycandowithScala.Abasicknowledgeofobject-orientedprogramming,especiallyinJava,isassumed.本文仅在对Scala语言和其编译器进行简要介绍。本文的目的读者是那些已经具有一定编程经验,而想尝试一下Scala语言的人们。要阅读本文,你应当具有基础的面向对象编程的概念,尤其是Java语言的。2Afirstexample第一个例子Asafirstexample,wewillusethestandardHelloworldprogram.Itisnotveryfasci-natingbutmakesiteasytodemonstratetheuseoftheScalatoolswithoutknowingtoomuchaboutthelanguage.Hereishowitlooks:作为学习Scala的第一部,我们将首先写一个标准的HelloWorld,这个虽然不是很有趣,但是它可以是你对Scala有一个最直观的认识而不需要太多关于这个语言的知识。我们的Helloworld看起来像这样:objectHelloWorld{defmain(args:Array[String]){println(”Hello,world!”)}}ThestructureofthisprogramshouldbefamiliartoJavaprogrammers:itconsistsofonemethodcalledmainwhichtakesthecommandlinearguments,anarrayofstrings,asparameter;thebodyofthismethodconsistsofasinglecalltothepre-definedmethodprintlnwiththefriendlygreetingasargument.Themainmethoddoesnotreturnavalue(itisaproceduremethod).Therefore,itisnotnecessarytodeclareareturntype.程序的结构对Java程序员来说可能很令人怀念:它由一个main函数来接受命令行参数,也就是一个String数组。这个函数的唯一一行代码把我们的问候语传递给了一个叫println的预定义函数。main函数不返回值(所以它是一个proceduremethod)。所以,也不需要声明返回类型。WhatislessfamiliartoJavaprogrammersistheobjectdeclarationcontainingthemainmethod.Suchadeclarationintroduceswhatiscommonlyknownasasingle-tonobject,thatisaclasswithasingleinstance.ThedeclarationabovethusdeclaresbothaclasscalledHelloWorldandaninstanceofthatclass,alsocalledHelloWorld.Thisinstanceiscreatedondemand,thefirsttimeitisused.对于Java程序员比较陌生的是关于定义了main函数的object语句。这样的语句定义了一个单例对象:一个有且仅有一个实例的类。object语句在定义了一个叫HelloWorld的类的同时还定义了一个叫HelloWorld的实例。这个实例在第一次使用的时候会进行实例化。Theastutereadermighthavenoticedthatthemainmethodisnotdeclaredasstatichere.Thisisbecausestaticmembers(methodsorfields)don