1 / 24
文档名称:

The Scala API.ppt

格式:ppt   大小:368KB   页数:24页
下载后只包含 1 个 PPT 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

The Scala API.ppt

上传人:陈潇睡不醒 2020/8/23 文件大小:368 KB

下载得到文件列表

The Scala API.ppt

相关文档

文档介绍

文档介绍:TheScalaAPITheScalaAPIScalahasareputationofbeingadifficultlanguageSomepeoplefeelthatScalaisbeyondwhattheaverageprogrammercanmasterOtherssayit’snomoredifficultthanJavaSomethingshavebeenadded,butalothasbeensimplifiedEveryoneagreesthattheScalaAPIishardertounderstandthantheJavaAPIThiscertainlymakesScalaseemmoredifficultthanitisYoudon’thavetounderstandeverythingaboutamethodinordertouseiteffectivelyThislectureisanattempttode-mystifytheScalaAPI*SearchingTheAPIissearchoriented;everyframehasasearchfieldYoucanclickonaletterandsearchformethods*panionobject*ObjectsAclassisatemplateforcreatingobjectsButifyouneedonlyoneobjectofagiventype,youcanjustcreateitdirectlyobjectInstructor{valname="DavidMatuszek"} Scalaprovidesanumberofpredefinedobjects,suchasConsoleobjectTryIt{ defmain(args:Array[String]):Unit={ valc=Console ("Hello,console!") } }Hello,console!*ClassesAclassisatemplateforcreatingobjects;itmaytakeparametersClassesmaybemarkedascase,abstract,orfinalcaseclasseshavespecialfeaturesTocreateanobject,youmayomitthewordnewCaseclassescanbeusedinpatternmatchingCaseclasseshaveautomaticallygeneratedtoString,hashCode,andequalsmethods(whichusetheconstructorparameters)abstractclassescannotbeinstantiatedfinalclassescannotbesubclassed*CreatingnewobjectsfromclassesWhenyoudefineaclass,youusuallyusethewordnewtocreateobjectsofthatclassobjectTryIt{ defmain(args:Array[String]):Unit={ valt=newThing(1) } } classThing(number:Int){ println(s"I'mThing$number!") }Ifyoumaketheclassacaseclass,youdon’tneedthewordnewobjectTryIt{ defmain(args:Array[String]):Unit={ valt=Thing(1) } } caseclassThing(number:Int){ println(s"I'mThing$number!") }Bothobjectsproducethisoutput:I'mThing1!*CompanionobjectsandclassesIfaclassandanobjecthavethesamenameandaredefinedonthesamesourcefile,esstoallthefeaturesoftheotherobjectThing{ privatevarcount=0 defmain(args:Array[String]):Unit={ newThing(1,2,3) println(s"That's$countThings!") } } classThing(valnumbers:Int*){ for(n<-numbers){ println(s"I'mThing$n!") +=1 } }I