资源预览内容
第1页 / 共22页
第2页 / 共22页
第3页 / 共22页
第4页 / 共22页
第5页 / 共22页
第6页 / 共22页
第7页 / 共22页
第8页 / 共22页
第9页 / 共22页
第10页 / 共22页
亲,该文档总共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 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 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 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 blends 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, 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 bring 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 Rational 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);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 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, note 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 code, so lets see what - 8 -happens when you 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);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 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, well, 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 - 9 -that it lets you try out Scala slowly, by migrating Java classes over to 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 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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号