[[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> val oneHalf = new Rational(1, 2); <console>:7: error: not found: type Rational val oneHalf = new Rational(1, 2); ^ scala> import scala.math._ import scala.math._ scala> val oneHalf = new Rational(1, 2); <console>:10: error: not found: type Rational val oneHalf = new Rational(1, 2); ^ scala> val pair = (99, "Luftballons") pair: (Int, java.lang.String) = (99,Luftballons) scala> println(pair._1) 99 scala> println(pair._2) Luftballons scala> [[email protected] test]# [[email protected] test]# scala Ration.scala Exception in thread "main" java.lang.RuntimeException: Cannot figure out how to run target: Ration.scala at scala.sys.package$.error(package.scala:27) at scala.tools.nsc.GenericRunnerCommand.scala$tools$nsc$GenericRunnerCommand$$guessHowToRun(GenericRunnerCommand.scala:38) at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48) at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48) at scala.Option.getOrElse(Option.scala:108) at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:48) at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:17) at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:33) at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89) at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala) [[email protected] test]# vim Ration.scala [[email protected] test]# scalac Ration.scala [[email protected] test]# scala Ration.scala [[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> val oneHalf = new Rational(1, 2); Created 1/2 oneHalf: Rational = [email protected] scala> [[email protected] test]# [[email protected] test]# cat Ration.scala class Rational(n: Int, d: Int) { println("Created "+n+"/"+d) } [[email protected] test]# vim Rational.scala [[email protected] test]# scalac Ration.scala [[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> val oneHalf = new Rational(1, 2); Created 1/2 oneHalf: Rational = [email protected] scala> val x = new Rational(1, 3) Created 1/3 x: Rational = [email protected] scala> [[email protected] test]# [[email protected] test]# scalac Rational.scala [[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> val x = new Rational(1, 3) x: Rational = 1/3 scala> val oneHalf = new Rational(1, 2); oneHalf: Rational = 1/2 scala> [[email protected] test]# [[email protected] test]# cat Rational.scala class Rational(n: Int, d: Int) { override def toString = n +"/"+ d } [[email protected] test]# cat Ration.scala class Rational(n: Int, d: Int) { println("Created "+n+"/"+d) } [[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> new Rational(1, 2) res0: Rational = 1/2 scala> val oneHalf = new Rational(1, 2); oneHalf: Rational = 1/2 scala> 这个最初的Rational例子凸显了Java和Scala之间的不同。Java类具有可以带参数的构造器,而Scala类可以直接带参数。 Scala的写法更简洁——类参数可以直接在类的主体中使用;没必要定义字段然后写赋值函数把构造器的参数复制到字段里。 这可以潜在地节省很多固定写法,尤其是对小类来说。 Scala编译器将把你放在类内部的任何不是字段的部分或者方法定义的代码, 编译进主构造器。例如,你可以像这样打印输出一条除错消息: class Rational(n: Int, d: Int) { println("Created "+n+"/"+d) } 根据这个代码,Scala编译器将把println调用放在Rational的主构造器。 因此,println调用将在每次创建一个新的Rational实例时打印这条除错信息。 scala> new Rational(5, 0) res1: Rational = 5/0 scala> 除数为零并没有报错。 [[email protected] test]# [[email protected] test]# vim Rational.scala [[email protected] test]# scala Rational.scala /user/local/test/Rational.scala:2: error: not found: value requre requre(d!=0) ^ one error found [[email protected] test]# vim Rational.scala [[email protected] test]# scala Rational.scala [[email protected] test]# scalac Rational.scala [[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> new Rational(5, 0) java.lang.IllegalArgumentException: requirement failed at scala.Predef$.require(Predef.scala:202) at Rational.<init>(Rational.scala:2) at .<init>(<console>:8) at .<clinit>(<console>) at .<init>(<console>:11) at .<clinit>(<console>) at $print(<console>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704) at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920) at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43) at scala.tools.nsc.io.package$$anon$2.run(package.scala:25) at java.lang.Thread.run(Thread.java:745) scala> 除数为零报错正常现象。 scala> val oneHalf = new Rational(1, 2); oneHalf: Rational = 1/2 scala> scala> new Rational(5, 0) res1: Rational = 5/0 scala> [[email protected] test]# [[email protected] test]# vim Rational.scala [[email protected] test]# scala Rational.scala /user/local/test/Rational.scala:2: error: not found: value requre requre(d!=0) ^ one error found [[email protected] test]# vim Rational.scala [[email protected] test]# scala Rational.scala [[email protected] test]# scalac Rational.scala [[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> new Rational(5, 0) java.lang.IllegalArgumentException: requirement failed at scala.Predef$.require(Predef.scala:202) at Rational.<init>(Rational.scala:2) at .<init>(<console>:8) at .<clinit>(<console>) at .<init>(<console>:11) at .<clinit>(<console>) at $print(<console>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704) at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920) at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43) at scala.tools.nsc.io.package$$anon$2.run(package.scala:25) at java.lang.Thread.run(Thread.java:745) scala> val oneHalf = new Rational(1, 2); oneHalf: Rational = 1/2 scala> scala> [[email protected] test]# [[email protected] test]# vim Rationa.scala [[email protected] test]# scalac Rationa.scala Rationa.scala:5: error: value d is not a member of Rational new Rational(n * that.d + that.n * d, d * that.d) ^ Rationa.scala:5: error: value d is not a member of Rational new Rational(n * that.d + that.n * d, d * that.d) ^ two errors found [[email protected] test]# cat Rationa.scala class Rational(n: Int, d: Int) { // 编译不过 require(d != 0) override def toString = n +"/"+ d def add(that: Rational): Rational = new Rational(n * that.d + that.n * d, d * that.d) } [[email protected] test]# vim Rationa_1.scala [[email protected] test]# scala Rationa_1.scala [[email protected] test]# scalac Rationa_1.scala [[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> val oneHalf = new Rational(1, 2) oneHalf: Rational = 1/2 scala> val twoThirds = new Rational(2, 3) twoThirds: Rational = 2/3 scala> oneHalf add(twoThirds) res0: Rational = 7/6 scala> [[email protected] test]# [[email protected] test]# cat Rationa_1.scala class Rational(n: Int, d: Int) { require(d != 0) val numer: Int = n val denom: Int = d override def toString = numer+"/"+denom def add(that: Rational): Rational = new Rational( numer * that.denom + that.numer * denom, denom * that.denom ) } [[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> val five = new Rational(5) <console>:7: error: not enough arguments for constructor Rational: (n: Int, d: Int)Rational. Unspecified value parameter d. val five = new Rational(5) ^ scala> [[email protected] test]# [[email protected] test]# cp Rationa_1.scala R RangeArray.scala Rational.class Rationa.scala RegexD1.scala RegexD.scala RegexTest1.scala Rationa_1.scala Rational.scala Ration.scala RegexD2.scala RegexReplace.scala RegexTest2.scala [[email protected] test]# cp Rationa_1.scala Rationa_2.scala [[email protected] test]# cat Rationa_1.scala class Rational(n: Int, d: Int) { require(d != 0) val numer: Int = n val denom: Int = d override def toString = numer+"/"+denom def add(that: Rational): Rational = new Rational( numer * that.denom + that.numer * denom, denom * that.denom ) } [[email protected] test]# vim Rationa_2.scala [[email protected] test]# scala Rationa_2.scala /user/local/test/Rationa_2.scala:5: error: value update is not a member of this.Rational this(n:Int)=this(n,1) ^ one error found [[email protected] test]# vim Rationa_2.scala [[email protected] test]# scalac Rationa_2.scala [[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> val five = new Rational(5) five: Rational = 5/1 scala> five. add asInstanceOf denom isInstanceOf numer toString scala> five.denom res0: Int = 1 scala> five.numer res1: Int = 5 scala> [[email protected] test]# [[email protected] test]# vim Rationa_3.scala [1]+ Stopped vim Rationa_3.scala [[email protected] test]# cp Rationa_2.scala Rationa_3.scala [[email protected] test]# vim Rationa_3.scala [[email protected] test]# scalac Rationa_3.scala [[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> five.denom <console>:8: error: not found: value five five.denom ^ scala> val five = new Rational(5) five: Rational = 5/1 scala> five.denom res1: Int = 1 scala> val oneHalf = new Rational(1, 2) oneHalf: Rational = 1/2 scala> val twoThirds = new Rational(2, 3) twoThirds: Rational = 2/3 scala> oneHalf.max def max(that: Rational): Rational scala> oneHalf.max(twoThirds) res2: Rational = 2/3 scala> twoThirds.max(oneHalf) res3: Rational = 2/3 scala> twoThirds.lessThan(oneHalf) res4: Boolean = false scala> oneHalf.lessThan(twoThirds) res5: Boolean = true scala> [[email protected] test]# [[email protected] test]# cat Rationa_3.scala class Rational(n: Int, d: Int) { require(d != 0) val numer: Int = n val denom: Int = d def this(n:Int)=this(n,1) override def toString = numer+"/"+denom def add(that: Rational): Rational = new Rational( numer * that.denom + that.numer * denom, denom * that.denom ) def lessThan(that: Rational) = this.numer * that.denom < that.numer * this.denom // 移项法 this.numer/this.denom < that.numer/that.denom def max(that: Rational) = if (this.lessThan(that)) that else this } [[email protected] test]# [[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> new Rational(66, 42) res0: Rational = 66/42 scala> [[email protected] test]# [[email protected] test]# vim Rationa_4.scala [[email protected] test]# scalac Rationa_4.scala [[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> new Rational(66, 42) res0: Rational = 11/7 scala> [[email protected] test]# [[email protected] test]# cat Rationa_4.scala class Rational(n: Int, d: Int) { require(d != 0) private val g = gcd(n.abs, d.abs) val numer = n / g val denom = d / g def this(n: Int) = this(n, 1) def add(that: Rational): Rational = new Rational( numer * that.denom + that.numer * denom, denom * that.denom ) override def toString = numer+"/"+denom private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b) } 这个版本的Rational里,我们添加了私有字段,g,并修改了numer和denom的初始化器(初始化器:initializer是初始化变量, 例如初始化numer的“n / g”,的代码)。因为g是私有的,它只能在类的主体之内,而不能在外部被访问。我们还添加了一个私有方法, gcd,用来计算传入的两个Int的最大公约数。比方说,gcd(12, 8)是4。正如你在4.1节中看到的,想让一个字段或方法私有化你只要把 private关键字放在定义的前面。私有的“助手方法”gcd的目的是把类的其它部分,这里是主构造器,需要的代码分离出来。为了确保 g始终是正的,我们传入n和d的绝对值,调用abs即可获得任意整数的绝对值。 Scala编译器将把Rational的三个字段的初始化代码依照它们在源代码中出现的次序放入主构造器。所以g的初始化代码,gcd(n.abs, d.abs), 将在另外两个之前执行,因为它在源文件中出现得最早。g将被初始化为类参数,n和d,的绝对值的最大公约数。然后再被用于numer和denom的初始化。 通过把n和d整除它们的最大公约数,g,每个Rational都将被构造成它的最简形式: [[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> 12.abs res0: Int = 12 scala> -12.abs res1: Int = 12 scala> (-12).abs res2: Int = 12 scala> -10.abs res3: Int = 10 scala> 10.abs res4: Int = 10 scala> 0.009.abs res5: Double = 0.009 scala> -0.009.abs res6: Double = 0.009 scala> scala> new Rational(66, 42) res7: Rational = 11/7 scala> new Rational(40, 10) res8: Rational = 4/1 scala> new Rational(40, 20) res9: Rational = 2/1 scala> new Rational(40,0) java.lang.IllegalArgumentException: requirement failed at scala.Predef$.require(Predef.scala:202) at Rational.<init>(Rationa_4.scala:2) at .<init>(<console>:8) at .<clinit>(<console>) at .<init>(<console>:11) at .<clinit>(<console>) at $print(<console>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704) at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920) at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43) at scala.tools.nsc.io.package$$anon$2.run(package.scala:25) at java.lang.Thread.run(Thread.java:745) scala> new Rational(40) res11: Rational = 40/1 scala> 2+3 res12: Int = 5 scala> 2/3+2/4 res13: Int = 0 scala> 2/3 add 2/4 <console>:8: error: value add is not a member of Int 2/3 add 2/4 ^ scala> (2/3).add(2/4) <console>:8: error: value add is not a member of Int (2/3).add(2/4) ^ scala> 2 add 3 <console>:8: error: value add is not a member of Int 2 add 3 ^ scala> val oneHalf = new Rational(1, 2) oneHalf: Rational = 1/2 scala> val twoThirds = new Rational(2, 3) twoThirds: Rational = 2/3 scala> oneHalf.add(twoThirds) res17: Rational = 7/6 scala> oneHalf add twoThirds res18: Rational = 7/6 scala> oneHalf + twoThirds <console>:10: error: type mismatch; found : Rational required: String oneHalf + twoThirds ^ scala> [[email protected] test]# [[email protected] test]# vim Rationa_5.scala [[email protected] test]# scalac Rationa_5.scala [[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> val oneHalf = new Rational(1, 2) oneHalf: Rational = 1/2 scala> val twoThirds = new Rational(2, 3) twoThirds: Rational = 2/3 scala> oneHalf + twoThirds res0: Rational = 7/6 scala> oneHalf *twoThirds res1: Rational = 1/3 scala> [[email protected] test]# [[email protected] test]# cat Rationa_5.scala class Rational(n: Int, d: Int) { require(d != 0) private val g = gcd(n.abs, d.abs) val numer = n / g val denom = d / g def this(n: Int) = this(n, 1) def +(that: Rational): Rational = new Rational( numer * that.denom + that.numer * denom, denom * that.denom ) def *(that: Rational): Rational = new Rational(numer * that.numer, denom * that.denom) override def toString = numer+"/"+denom private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b) } [[email protected] test]# [[email protected] test]# cat Rationa_5.scala class Rational(n: Int, d: Int) { require(d != 0) private val g = gcd(n.abs, d.abs) val numer = n / g val denom = d / g def this(n: Int) = this(n, 1) def +(that: Rational): Rational = new Rational( numer * that.denom + that.numer * denom, denom * that.denom ) def *(that: Rational): Rational = new Rational(numer * that.numer, denom * that.denom) override def toString = numer+"/"+denom private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b) } [[email protected] test]# scala Welcome to Scala version 2.9.3 (Java HotSpot(TM) Client VM, Java 1.7.0_67). Type in expressions to have them evaluated. Type :help for more information. scala> oneHalf *twoThirds <console>:8: error: not found: value oneHalf oneHalf *twoThirds ^ scala> val twoThirds = new Rational(2, 3) twoThirds: Rational = 2/3 scala> val oneHalf = new Rational(1, 2) oneHalf: Rational = 1/2 scala> oneHalf *twoThirds res1: Rational = 1/3 scala> oneHalf + twoThirds res2: Rational = 7/6 scala> oneHalf .+ twoThirds <console>:9: error: missing arguments for method + in class Rational; follow this method with `_' if you want to treat it as a partially applied function oneHalf .+ twoThirds ^ scala> oneHalf .+ (twoThirds) res4: Rational = 7/6 scala> scala> val x = new Rational(1, 2) x: Rational = 1/2 scala> val y = new Rational(2, 3) y: Rational = 2/3 scala> x.+(y) res5: Rational = 7/6 scala> x + y res6: Rational = 7/6 scala> x + x * y res7: Rational = 5/6 scala> (x + x) * y res8: Rational = 2/3 scala> x + (x * y) res9: Rational = 5/6 scala> [[email protected] test]# ctrl + d [[email protected] test]# ll total 280 -rw-r--r-- 1 root root 65 Jul 10 23:59 2qw -rw-r--r-- 1 root root 946 Jul 8 00:31 AppTest$$anonfun$1.class -rw-r--r-- 1 root root 1425 Jul 8 00:31 AppTest.class -rw-r--r-- 1 root root 1956 Jul 8 00:31 AppTest$.class -rw-r--r-- 1 root root 1137 Jul 8 00:31 AppTest$delayedInit$body.class -rw-r--r-- 1 root root 161 Jul 8 00:31 AppTest.scala -rw-r--r-- 1 root root 632 Jul 5 02:15 ArrayTest.scala drwxr-xr-x 2 root root 4096 Jul 5 00:58 bak -rw-r--r-- 1 root root 650 Jul 5 04:58 BreakTest1.scala -rw-r--r-- 1 root root 401 Jul 5 04:56 BreakTest.scala -rw-r--r-- 1 root root 330 Jul 4 19:45 CaseTest2.scala -rw-r--r-- 1 root root 354 Jul 4 20:07 CaseTest3.scala -rw-r--r-- 1 root root 263 Jul 4 19:38 CaseTest.scala drwxr-xr-x 5 root root 4096 Jul 5 05:29 classes -rw-r--r-- 1 root root 351 Jul 5 02:27 ConcatArray.scala -rw-r--r-- 1 root root 239 Jul 5 02:03 ConsoleTes.scala -rw-r--r-- 1 root root 197 Jul 5 01:51 ConsoleTest.scala -rw-r--r-- 1 root root 239 Jul 5 05:02 DoWhileTest.scala -rw-r--r-- 1 root root 238 Jul 5 03:42 Factor.scala -rw-r--r-- 1 root root 238 Jul 5 03:42 FactorTest.scala -rw-r--r-- 1 root root 475 Jul 4 23:37 File1.scala -rw-r--r-- 1 root root 529 Jul 4 23:40 File2.scala -rw-r--r-- 1 root root 411 Jul 4 23:32 File.scala -rw-r--r-- 1 root root 189 Jul 5 01:26 FileWR.scala -rw-r--r-- 1 root root 403 Jul 5 03:37 FormatTest.scala -rw-r--r-- 1 root root 585 Jul 4 20:15 ForTest.scala -rw-r--r-- 1 root root 414 Jul 5 03:59 Fun2.scala -rw-r--r-- 1 root root 388 Jul 5 03:49 Fun.scala -rw-r--r-- 1 root root 325 Jul 5 05:29 HelloWorld23.scala -rw-r--r-- 1 root root 104 Jul 4 18:44 HelloWorld2.scala -rw-r--r-- 1 root root 88 Jul 4 04:18 HelloWorld.scala -rw-r--r-- 1 root root 86 Jul 5 05:24 Hl.scala -rw-r--r-- 1 root root 204 Jul 5 05:04 IfTest.scala -rw-r--r-- 1 root root 438 Jul 5 02:21 MatrixTest.scala -rw-r--r-- 1 root root 183 Jul 5 04:35 Other.scala -rw-r--r-- 1 root root 223 Jul 5 04:21 Outer1.scala -rw-r--r-- 1 root root 169 Jul 5 04:14 Outer.scala -rwxr-xr-x 1 root root 193 Jul 4 04:06 person.sh -rw-r--r-- 1 root root 365 Jul 5 04:38 ProtectPub.scala -rw-r--r-- 1 root root 424 Jul 5 03:25 RangeArray.scala -rw-r--r-- 1 root root 262 Jul 11 00:44 Rationa_1.scala -rw-r--r-- 1 root root 290 Jul 11 01:05 Rationa_2.scala -rw-r--r-- 1 root root 475 Jul 11 01:24 Rationa_3.scala -rw-r--r-- 1 root root 425 Jul 11 02:11 Rationa_4.scala -rw-r--r-- 1 root root 516 Jul 11 02:44 Rationa_5.scala -rw-r--r-- 1 root root 2274 Jul 11 02:44 Rational.class -rw-r--r-- 1 root root 87 Jul 11 00:19 Rational.scala -rw-r--r-- 1 root root 200 Jul 11 00:29 Rationa.scala -rw-r--r-- 1 root root 65 Jul 10 23:59 Ration.scala -rw-r--r-- 1 root root 234 Jul 4 22:12 RegexD1.scala -rw-r--r-- 1 root root 250 Jul 4 22:18 RegexD2.scala -rw-r--r-- 1 root root 349 Jul 4 22:07 RegexD.scala -rw-r--r-- 1 root root 231 Jul 4 21:54 RegexReplace.scala -rw-r--r-- 1 root root 270 Jul 4 21:48 RegexTest1.scala -rw-r--r-- 1 root root 243 Jul 4 20:49 RegexTest2.scala -rwxr-xr-x 1 root root 166 Jul 4 04:13 script.sh -rw-r--r-- 1 root root 602 Jul 5 00:00 Some1.scala -rw-r--r-- 1 root root 633 Jul 5 00:01 Some.scala -rw-r--r-- 1 root root 205 Jul 5 01:55 SourceTest.scala -rw-r--r-- 1 root root 435 Jul 5 03:35 Str.scala -rw-r--r-- 1 root root 111 Jul 5 05:21 Symb.scala -rw-r--r-- 1 root root 769 Jul 4 19:07 Test2.scala -rw-r--r-- 1 root root 426 Jul 4 19:13 Test3.scala -rw-r--r-- 1 root root 408 Jul 5 00:08 TestExtractor.scala -rw-r--r-- 1 root root 493 Jul 4 19:00 Test.scala -rw-r--r-- 1 root root 27 Jul 5 01:57 test.txt -rw-r--r-- 1 root root 815 Jul 4 19:31 TraitTest.scala -rw-r--r-- 1 root root 238 Jul 5 04:42 While.scala -rw-r--r-- 1 root root 176 Jul 5 04:46 WhileTest1.scala -rw-r--r-- 1 root root 238 Jul 5 04:44 WhileTest.scala [[email protected] test]# sc scala scl_enabled scrollkeeper-get-extended-content-list scala.bat sclient scrollkeeper-get-index-from-docpath scalac scp scrollkeeper-get-toc-from-docpath scalac.bat script scrollkeeper-get-toc-from-id scaladoc scriptreplay scrollkeeper-install scaladoc.bat scrollkeeper-config scrollkeeper-preinstall scalap scrollkeeper-extract scrollkeeper-rebuilddb scalap.bat scrollkeeper-gen-seriesid scrollkeeper-uninstall schemagen scrollkeeper-get-cl scrollkeeper-update scl scrollkeeper-get-content-list scsi_id [[email protected] test]# script.sh bash: script.sh: command not found [[email protected] test]# ./ script.sh bash: ./: is a directory [[email protected] test]# sh script.sh Hello, world! List() [[email protected] test]# Unix和Windows的Scala脚本 [[email protected] test]# cat script.sh #!/bin/sh exec scala "$0" "[email protected]" !# object HelloWorld { def main(args: Array[String]) { println("Hello, world! " + args.toList) } } HelloWorld.main(args) [[email protected] test]# cat script.sh hahahajj #!/bin/sh exec scala "$0" "[email protected]" !# object HelloWorld { def main(args: Array[String]) { println("Hello, world! " + args.toList) } } HelloWorld.main(args) cat: hahahajj: No such file or directory [[email protected] test]# sh script.sh haha jj hehe hiehie Hello, world! List(haha, jj, hehe, hiehie) [[email protected] test]# vim shellarg.sh [[email protected] test]# ls -l shellarg.sh -rw-r--r-- 1 root root 102 Jul 11 04:19 shellarg.sh [[email protected] test]# chmod a+x shellarg.sh [[email protected] test]# ls -l shellarg.sh -rwxr-xr-x 1 root root 102 Jul 11 04:19 shellarg.sh [[email protected] test]# sh shellarg.sh chmoda+x shellarg.sh: line 3: syntax error near unexpected token `"Hello, "' shellarg.sh: line 3: `println("Hello, " + args(0) + "!")' [[email protected] test]# sh shellarg.sh chmoda shellarg.sh: line 3: syntax error near unexpected token `"Hello, "' shellarg.sh: line 3: `println("Hello, " + args(0) + "!")' [[email protected] test]# vim shellarg.sh [[email protected] test]# sh shellarg.sh chmoda error: script file does not close its header with !# or ::!# one error found [[email protected] test]# cat shellarg.sh #!/bin/sh exec scala "$0" "[email protected]" ! # // 对第一个参数打招呼 println("Hello, " + args(0) + "!") [[email protected] test]# vim shellarg.sh [[email protected] test]# sh shellarg.sh chmoda Hello, chmoda! [[email protected] test]# cat shellarg.sh #!/bin/sh exec scala "$0" "[email protected]" !# // 对第一个参数打招呼 println("Hello, " + args(0) + "!") [[email protected] test]# [[email protected] test]# pwd /user/local/test [[email protected] test]# sh shellarg.sh chmoda Hello, chmoda! [[email protected] test]# sh shellarg.sh chmoda+x hh Hello, chmoda+x! [[email protected] test]#
时间: 2024-11-06 20:22:09