计算机专业外文翻译thinkinjava

上传人:痛*** 文档编号:131787206 上传时间:2022-08-07 格式:DOC 页数:22 大小:100.50KB
收藏 版权申诉 举报 下载
计算机专业外文翻译thinkinjava_第1页
第1页 / 共22页
计算机专业外文翻译thinkinjava_第2页
第2页 / 共22页
计算机专业外文翻译thinkinjava_第3页
第3页 / 共22页
资源描述:

《计算机专业外文翻译thinkinjava》由会员分享,可在线阅读,更多相关《计算机专业外文翻译thinkinjava(22页珍藏版)》请在装配图网上搜索。

1、优质文档来自:think in java 3外文原文The busy Java developers guide to Scala: Class actionIt makes sense for Java developers to use objects as a first point of reference for understanding Scala. In this second installment of The busy Java developers guide to Scala series, Ted Neward follows a basic premise of

2、language measurement: that the power of a language can be measured in direct relation to its ability to integrate new facilities - in this case, support for complex numbers. Along the way youll see some interesting tidbits related to class definitions and usage in Scala. In last months article , you

3、 saw just a touch of Scalas syntax, the bare minimum necessary to run a Scala program and observe some of its simpler features. The Hello World and Timer examples from that article let you see Scalas Application class, its syntax for method definitions and anonymous functions, just a glimpse of an A

4、rray, and a bit on type-inferencing. Scala has a great deal more to offer, so this article investigates the intricacies of Scala coding.Scalas functional programming features are compelling, but theyre not the only reason Java developers should be interested in the language. In fact, Scala blends fu

5、nctional concepts and object orientation. In order to let the Java-cum-Scala programmer feel more at home, it makes sense to look at Scalas object features and see how they map over to Java linguistically. Bear in mind that there isnt a direct mapping for some of these features, or in some cases, th

6、e mapping is more of an analog than a direct parallel. But where the distinction is important, Ill point it out.Scala has class(es), tooRather than embark on a lengthy and abstract discussion of the class features that Scala supports, lets look at a definition for a class that might be used to bring

7、 rational number support to the Scala platform (largely swiped from Scala By Example - see Resources):Listing 1. rational.scalaclass Rational(n:Int, d:Int) private def gcd(x:Int, y:Int): Int = if (x=0) y else if (x0) gcd(-x, y) else if (yjavap -private -classpath classes RationalCompiled from ration

8、al.scalapublic class Rational extends java.lang.Object implements scala.ScalaObject private int denom; private int numer; private int g; public Rational(int, int); public Rational unary_$tilde(); public java.lang.String toString(); public Rational $div(Rational); public Rational $times(Rational); pu

9、blic Rational $minus(Rational); public Rational $plus(Rational); public int denom(); public int numer(); private int g(); private int gcd(int, int); public Rational(int); public int $tag();C:Projectsscala-classescodeThe operators defined in the Scala class transmogrify into method calls in the best

10、tradition of Java programming, though they do seem to be based on funny names. Two constructors are defined on the class: one taking an int and one taking a pair of ints. And, if you happen to be at all concerned that the use of the upper-case Int type is somehow a java.lang.Integer in disguise, not

11、e that the Scala compiler is smart enough to transform them into regular Java primitive ints in the class definition.Testing, testing, 1-2-3.It is a well-known meme that good programmers write code, and great programmers write tests; thus far, I have been lax in exercising this rule for my Scala cod

12、e, so lets see what happens when you put this Rational class inside of a traditional JUnit test suite, as shown in Listing 10:Listing 10. RationalTest.java import org.junit.*;import static org.junit.Assert.*;public class RationalTest Test public void test2ArgRationalConstructor() Rational r = new Ra

13、tional(2, 5); assertTrue(r.numer() = 2); assertTrue(r.denom() = 5); Test public void test1ArgRationalConstructor() Rational r = new Rational(5); assertTrue(r.numer() = 0); assertTrue(r.denom() = 1); / 1 because of gcd() invocation during construction; / 0-over-5 is the same as 0-over-1 Test public v

14、oid testAddRationals() Rational r1 = new Rational(2, 5); Rational r2 = new Rational(1, 3); Rational r3 = (Rational) reflectInvoke(r1, $plus, r2); /r1.$plus(r2); assertTrue(r3.numer() = 11); assertTrue(r3.denom() = 15); / . some details omittedAside from confirming that the Rational class behaves, we

15、ll, rationally, the above test suite also proves that it is possible to call Scala code from Java code (albeit with a little bit of an impedance mismatch when it comes to the operators). The cool thing about this, of course, is that it lets you try out Scala slowly, by migrating Java classes over to

16、 Scala classes without ever having to change the tests that back them.The only weirdness you might notice in the test code has to do with operator invocation, in this case, the + method on the Rational class. Looking back at the javap output, Scala has obviously translated the + function into the JV

17、M method $plus, but the Java Language Specification does not allow the $ character in identifiers (which is why its used in nested and anonymous nested class names).In order to invoke those methods, you either have to write the tests in Groovy or JRuby (or some other language that doesnt pose a rest

18、riction on the $ character), or you can write a little bit of Reflection code to invoke it. I go with the latter approach, which isnt all that interesting from a Scala perspective, but the result is included in this articles code bundle, should you be curious. (SeeDownload.)Note that workarounds lik

19、e these are only necessary for function names that arent also legitimate Java identifiers.A better JavaBack when I was first learning C+, Bjarne Stroustrup suggested that one way to learn C+ was to see it as a better C (see Resources). In some ways, Java developers today might come to see Scala as a

20、 better Java, because it provides a more terse and succinct way of writing traditional Java POJOs. Consider the traditional Person POJO shown in Listing 11:Listing 11. JavaPerson.java (original POJO)public class JavaPerson public JavaPerson(String firstName, String lastName, int age) this.firstName

21、= firstName; this.lastName = lastName; this.age = age; public String getFirstName() return this.firstName; public void setFirstName(String value) this.firstName = value; public String getLastName() return this.lastName; public void setLastName(String value) this.lastName = value; public int getAge()

22、 return this.age; public void setAge(int value) this.age = value; public String toString() return Person: firstName + firstName + lastName: + lastName + age: + age + ; private String firstName; private String lastName; private int age;Now consider its equivalent written in Scala:Listing 12. person.s

23、cala (threadsafe POJO)class Person(firstName:String, lastName:String, age:Int) def getFirstName = firstName def getLastName = lastName def getAge = age override def toString = Person firstName: + firstName + lastName: + lastName + age: + age + It isnt a complete drop-in replacement, given that the o

24、riginal Person had some mutable setters. But considering the original Person also had no synchronization code around those mutable setters, the Scala version is safer to use. Also, if the goal is to truly reduce the number of lines of code in Person, you could remove the getFoo property methods enti

25、rely because Scala will generate accessor methods around each of the constructor parameters - firstName() returns a String, lastName() returns a String, and age() returns an int).Even if the need for those mutable setter methods is undeniable, the Scala version is still simpler, as you can see in Li

26、sting 13:Listing 13. person.scala (full POJO)class Person(var firstName:String, var lastName:String, var age:Int) def getFirstName = firstName def getLastName = lastName def getAge = age def setFirstName(value:String):Unit = firstName = value def setLastName(value:String) = lastName = value def setA

27、ge(value:Int) = age = value override def toString = Person firstName: + firstName + lastName: + lastName + age: + age + As an aside, notice the introduction of the var keyword on the constructor parameters. Without going into too much detail, var tells the compiler that the value is mutable. As a re

28、sult, Scala generates both accessor (String firstName(void) and mutator (void firstName_$eq(String) methods. It then becomes easy to create setFoo property mutator methods that use the generated mutator methods under the hood.ConclusionScala is an attempt to incorporate functional concepts and terse

29、ness without losing the richness of the object paradigm. As youve perhaps begun to see in this series, Scala also corrects some of the egregious (in hindsight) syntactic problems found in the Java language.This second article in the Busy Java developers guide to Scala series has focused on Scalas object facilities, which let you start using Scala without having to dive too deeply into the functional pool. Based on what youve learned so far, you can already start using Scala to reduce your programming workload. Among other things, you can use Scala to produce t

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!