计算机专业外文翻译think in java

上传人:第*** 文档编号:30571813 上传时间:2018-01-30 格式:DOC 页数:22 大小:115.50KB
返回 下载 相关 举报
计算机专业外文翻译think in java_第1页
第1页 / 共22页
计算机专业外文翻译think in java_第2页
第2页 / 共22页
计算机专业外文翻译think in java_第3页
第3页 / 共22页
计算机专业外文翻译think in java_第4页
第4页 / 共22页
计算机专业外文翻译think in java_第5页
第5页 / 共22页
点击查看更多>>
资源描述

《计算机专业外文翻译think in java》由会员分享,可在线阅读,更多相关《计算机专业外文翻译think in java(22页珍藏版)》请在金锄头文库上搜索。

1、 - 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

2、 of 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 ,

3、 you 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

4、an Array, 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 blend

5、s functional 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

6、, the 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 b

7、ring 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) yelse if (xjavap -private -classpath classes RationalCompiled from rational.scalapublic class Ra

8、tional extends java.lang.Object implements scala.ScalaObjectprivate 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);public Rational $minus(Rational);p

9、ublic 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 tradition of Java programming, though t

10、hey 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, note that the Scala compiler is smart enou

11、gh 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 code, so lets see what - 8 -happens when y

12、ou put this Rational class inside of a traditional JUnit test suite, as shown in Listing 10:Listing 10. RationalTest.javaimport org.junit.*;import static org.junit.Assert.*;public class RationalTestTest public void test2ArgRationalConstructor()Rational r = new Rational(2, 5);assertTrue(r.numer() = 2

13、);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 void testAddRationals()Rational r1 = new Ratio

14、nal(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, well, rationally, the above test suite also proves th

15、at 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 - 9 -that it lets you try out Scala slowly, by migrating Java classes over to Scala classes without ever having to change t

16、he 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 JVM 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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号