资源描述:
《计算机专业毕业设计外文翻译29》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、来自:thinkinjava(3)外文原文ThebusyJavadeveloper'sguidetoScala:ClassactionItmakessenseforJava™developerstouseobjectsasafirstpointofreferenceforunderstandingScala.InthissecondinstallmentofThebusyJavadeveloper'sguidetoScalaseries,TedNewardfollowsabasicpremiseoflanguagemeasurement:tha
2、tthepowerofalanguagecanbemeasuredindirectrelationtoitsabilitytointegratenewfacilities--inthiscase,supportforcomplexnumbers.Alongthewayyou'llseesomeinterestingtidbitsrelatedtoclassdefinitionsandusageinScala.Inlastmonth'sarticle,yousawjustatouchofScala'ssyntax,thebareminimumne
3、cessarytorunaScalaprogramandobservesomeofitssimplerfeatures.TheHelloWorldandTimerexamplesfromthatarticleletyouseeScala'sApplicationclass,itssyntaxformethoddefinitionsandanonymousfunctions,justaglimpseofanArray[],andabitontype-inferencing.Scalahasagreatdealmoretooffer,sothisa
4、rticleinvestigatestheintricaciesofScalacoding.Scala'sfunctionalprogrammingfeaturesarecompelling,butthey'renottheonlyreasonJavadevelopersshouldbeinterestedinthelanguage.Infact,Scalablendsfunctionalconceptsandobjectorientation.InordertolettheJava-cum-Scalaprogrammerfeelmoreath
5、ome,itmakessensetolookatScala'sobjectfeaturesandseehowtheymapovertoJavalinguistically.Bearinmindthatthereisn'tadirectmappingforsomeofthesefeatures,orinsomecases,the"mapping"ismoreofananalogthanadirectparallel.Butwherethedistinctionisimportant,I'llpointitout.Scalahasclass(es)
6、,tooRatherthanembarkonalengthyandabstractdiscussionoftheclassfeaturesthatScalasupports,let'slookatadefinitionforaclassthatmightbeusedtobringrationalnumbersupporttotheScalaplatform(largelyswipedfrom"ScalaByExample"--seeResources):Listing1.rational.scalaclassRational(n:Int,d:I
7、nt){privatedefgcd(x:Int,y:Int):Int={if(x==0)yelseif(x<0)gcd(-x,y)elseif(y<0)-gcd(x,-y)-22-elsegcd(y%x,x)}privatevalg=gcd(n,d)valnumer:Int=n/gvaldenom:Int=d/gdef+(that:Rational)=newRational(numer*that.denom+that.numer*denom,denom*that.denom)def-(that:Rational)=newRational(num
8、er*that.denom-that.numer*denom,denom*that.denom)def*(that:Rational)=newRati