\u57fa\u7840<\/h2> \n \n - Scala \u4e2d\u6570\u636e\u7c7b\u578b\u4e5f\u662f class \n
\n - 7 \u79cd\u6570\u503c\u7c7b\u578b: Byte, Char, Short, Int, Long, Float, and Double<\/li> \n
- Boolean \u7c7b\u578b<\/li> \n <\/ul><\/li> \n
- \u539f\u59cb\u7c7b\u578b\u4e0e class \u7c7b\u578b\u65e0\u533a\u522b\uff0c\u53ef\u5728\u6570\u5b57\u4e0a\u8c03\u7528\u65b9\u6cd5\uff08\u9690\u5f0f\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u65b9\u6cd5\u8c03\u7528\uff0c\u5982\u5bf9 Int \u64cd\u4f5c\u8f6c\u4e3a RichInt \u7684\u65b9\u6cd5\u8c03\u7528\u7b49\uff09 \n
\n 1.toString()<\/code><\/li> \n 1.to(10)<\/code> \/\/ Range(1,2,3,4,5,6,7,8,9,10)<\/li> \n <\/ul><\/li> \n - \u65e0\u5c01\u88c5\u7c7b\u578b\uff0cScala \u81ea\u52a8\u5904\u7406\u5c01\u5305\u62c6\u5305\uff0c\u5982\u5b57\u7b26\u4e32\u5e95\u5c42\u4f7f\u7528\u7684\u662f java<\/a>.lang.String<\/li> \n
- \u9690\u5f0f\u8f6c\u6362\u7684 StringOps \u5bf9 String \u6269\u5c55\uff0c\u5305\u542b\u4e86\u4e0a\u767e\u79cd\u64cd\u4f5c \n
\n "Hello".intersect("World")<\/code> \/\/ "lo"<\/li> \n <\/ul><\/li> \n - \u64cd\u4f5c\u7b26\u91cd\u8f7d\uff0c\u7b97\u6570\u64cd\u4f5c\u7b26\u4e5f\u662f\u65b9\u6cd5 \n
\n 1 + 2<\/code> \u7b49\u4ef7\u4e8e 1.+(b)<\/code><\/li> \n - \u901a\u5e38
a.method(b)<\/code> \u53ef\u7b80\u5199\u4e3a a method b<\/code><\/li> \n <\/ul><\/li> \n - \u9012\u589e\u9012\u51cf\uff0c\u6ca1\u6709
++<\/code> \u548c --<\/code> \u64cd\u4f5c\uff0c\u4f7f\u7528 +=1<\/code> \u548c -=1<\/code> \u4ee3\u66ff<\/li> \n - BigInt \u548c BigDecimal \u4e5f\u53ef\u76f4\u63a5\u4f7f\u7528\u7b97\u6570\u8fd0\u7b97\u7b26 \n
\n val x: BigInt = 1234567890<\/code><\/li> \n x * x * x<\/code> \/\/ Java \u9700\u8981\u8c03\u7528\u65b9\u6cd5 x.multiply(x).multiply(x)<\/code><\/li> \n <\/ul><\/li> \n _<\/code> \u4ee3\u8868\u901a\u914d\u7b26\uff0c\u53ef\u8868\u8fbe\u4efb\u610f\u4e1c\u897f<\/li> \n <\/ul> \n \u51fd\u6570\u4e0e\u65b9\u6cd5<\/h2> \n \n - \u6570\u5b66\u51fd\u6570\u4f7f\u7528\u65b9\u4fbf\u7b80\u5355\uff0c\u5982 min, max \u4e0d\u9700\u8981\u8c03\u7528\u67d0\u4e2a\u7c7b\u7684\u9759\u6001\u65b9\u6cd5 \uff08\u901a\u8fc7
import scala.math._<\/code> \u5bfc\u5165\u6570\u5b66\u51fd\u6570\u5305\uff09<\/li> \n - \u53ef\u7701\u7565\u7684 scala \u5305\u540d\u524d\u7f00\uff0c\u5982
import scala.math._<\/code> \u53ef\u5199\u4e3a import math._<\/code><\/li> \n - \u65e0\u9759\u6001\u51fd\u6570\uff0c\u4e0e\u4e4b\u529f\u80fd\u7c7b\u4f3c\u7684\u662f\u5355\u4f8b\u5bf9\u8c61\uff1b\u901a\u5e38\u4e00\u4e2a class \u90fd\u4f1a\u6709\u4e00\u4e2a\u4f34\u751f\u5bf9\u8c61\uff0c\u5176\u65b9\u6cd5\u5c31\u5982\u540c Java \u7684\u9759\u6001\u65b9\u6cd5<\/li> \n
- \u65e0\u53c2\u65b9\u6cd5\u8c03\u7528\u65f6\u901a\u5e38\u4e0d\u9700\u8981\u5e26\u62ec\u53f7 \n
\n "Hello".distinct<\/code><\/li> \n <\/ul><\/li> \n apply<\/code> \u65b9\u6cd5 \n \n - \u4f34\u751f\u5bf9\u8c61\u4e2d\u5b9a\u4e49<\/li> \n
- \u50cf\u51fd\u6570\u8c03\u7528\u4e00\u6837\u4f7f\u7528\uff0c\u53ef\u770b\u505a\u662f\u91cd\u8f7d\u4e86
()<\/code><\/li> \n "Hello"(4)<\/code> \u7b49\u4ef7\u4e8e "Hello".apply(4)<\/code><\/li> \n <\/ul><\/li> \n <\/ul> \n \u63a7\u5236\u4f53\u7cfb&\u51fd\u6570<\/h2> \n \n - \u6761\u4ef6\u8868\u8fbe\u5f0f \n
\n if\/else<\/code> \u8868\u8fbe\u5f0f\u6709\u8fd4\u56de\u503c \n \n val s = if (a > 0) 1 else -1<\/code> \/\/ \u8fd9\u79cd\u65b9\u5f0f\u4e0b s \u5b9a\u4e49\u4e3a val\uff0c\u5982\u679c\u653e\u5230\u5224\u65ad\u5185\u90e8\u8d4b\u503c\uff0c\u9700\u8981\u5b9a\u4e49\u4e3a\u53d8\u91cf var<\/li> \n - \u7edf\u4e00\u4e86\u4e09\u76ee\u8fd0\u7b97
?:<\/code> \u548c if\/else<\/code>\uff1bScala \u65e0\u4e09\u76ee\u8fd0\u7b97<\/li> \n if (a) 1<\/code> \u7b49\u4ef7\u4e8e if (a) 1 else ()<\/code>\uff1b\u53ef\u4ee5\u5c06 ()<\/code>(\uff0c\u5373 Unit \u7c7b) \u89c6\u4e3a\u65e0\u7528\u503c\u7684\u5360\u4f4d\u7b26\uff0c\u53ef\u770b\u505a Java \u4e2d\u7684 void<\/li> \n <\/ul><\/li> \n - \u65e0
switch<\/code> \u8868\u8fbe\u5f0f\uff0c\u800c\u662f\u4f7f\u7528\u66f4\u4e3a\u5f3a\u5927\u7684\u6a21\u5f0f\u5339\u914d\u6765\u66ff\u4ee3<\/li> \n <\/ul><\/li> \n - \u8bed\u53e5\u7ec8\u7ed3 \n
\n - \u5206\u53f7\u975e\u5fc5\u987b\uff0c\u5355\u884c\u5355\u8868\u8fbe\u5f0f\u53ef\u7701\u7565\uff0c\u5355\u884c\u591a\u8868\u8fbe\u5f0f\u9700\u52a0\u5206\u53f7\u533a\u5206<\/li> \n <\/ul><\/li> \n
\u8bed\u53e5\u5757&\u8d4b\u503c<\/p> \n
\n {...}<\/code> \u5305\u542b\u4e00\u7cfb\u5217\u8868\u8fbe\u5f0f\uff0c\u8bed\u53e5\u5757\u7684\u7ed3\u679c\u4e3a\u6700\u540e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c<\/li> \n - \u53ef\u7528\u4e8e\u521d\u59cb\u5316\u9700\u8981\u591a\u6b65\u64cd\u4f5c\u7684\u503c \n
\n val a = { express1; express2; express3 }<\/code><\/li> \n <\/ul><\/li> \n - \u8d4b\u503c\u8bed\u53e5\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u4e0d\u53ef\u4f7f\u7528\u94fe\u5f0f\u8d4b\u503c
x=y=1<\/code> \/\/ \u4e0e\u9884\u671f\u7ed3\u679c\u4e0d\u4e00\u81f4<\/li> \n <\/ul><\/li> \n IO<\/p> \n
\n - \u6253\u5370\uff0c
print \/ println \/ printf<\/code><\/li> \n - \u8bfb\u53d6 console \u8f93\u5165\uff0c
readLine \/ readInt \/ readDouble...<\/code><\/li> \n <\/ul><\/li> \n \u5faa\u73af<\/p> \n
\n - \u4e0d\u50cf\u5176\u4ed6\u8bed\u8a00\u90a3\u4e48\u5e38\u7528\uff0c\u901a\u5e38\u53ef\u4f7f\u7528\u5355\u4e2a\u65b9\u6cd5\u8c03\u7528\u5b8c\u6210\u6240\u6709\u64cd\u4f5c<\/li> \n
- while, do<\/li> \n
- \u6ca1\u6709\u4e0e Java \u7c7b\u4f3c\u7684 for \u5faa\u73af
for(init; test; update)<\/code>\uff0c\u53ef\u4f7f\u7528 while \u4ee3\u66ff\uff0c\u6216\u8005\u4f7f\u7528 for \u8868\u8fbe\u5f0f \n \n for (i <- 1 to 10) r = r * i<\/code><\/li> \n - \u751f\u6210\u5668
variable <- expression<\/code> \u4f1a\u904d\u5386\u6240\u6709\u5143\u7d20<\/li> \n - for \u5faa\u73af\u53ef\u5305\u542b\u591a\u4e2a\u751f\u6210\u5668\uff0c\u9017\u53f7\u5206\u9694\uff08\u6216\u6362\u884c\u533a\u5206\uff09\uff0c\u53ef\u4f7f\u7528 parttern guard \u6765\u8fdb\u884c\u6761\u4ef6\u8fc7\u6ee4 \n
\n for(v <- exp1; v2 <- exp2 if(condition)) doSome()<\/code> \/\/ if \u4e4b\u524d\u7684\u5206\u53f7\u53ef\u7701\u7565<\/li> \n <\/ul><\/li> \n - for \u8bed\u53e5\u4e2d\u7684\u53d8\u91cf\u4e0d\u9700\u8981\u58f0\u660e val \u6216 var\uff0c\u5176\u7c7b\u578b\u4e0e\u8fed\u4ee3\u7684\u96c6\u5408\u4e2d\u5143\u7d20\u7c7b\u578b\u4e00\u81f4<\/li> \n
1 to n<\/code> \u5305\u542b\u4e0a\u754c\uff0c1 until n<\/code> \u4e0d\u5305\u542b\u4e0a\u754c<\/li> \n <\/ul><\/li> \n \u6ca1\u6709 break\uff0ccontinue \u8868\u8fbe\u5f0f\u6765\u4e2d\u65ad\u5faa\u73af\uff0c\u66ff\u4ee3\u65b9\u6848\uff1a<\/p> \n
\n - Boolean \u53d8\u91cf\u63a7\u5236<\/li> \n
- \u5d4c\u5957\u51fd\u6570<\/li> \n
- \u4f7f\u7528 Breaks \u5bf9\u8c61\u7684 break \u65b9\u6cd5<\/li> \n <\/ul>
import scala.util.control.Breaks._\nbreakable {\n for (...) {\n if (...) break\n }\n}<\/code><\/pre><\/li> \n - yield\uff0c\u5728 for \u5faa\u73af\u4f53\u4ee5 yield \u5f00\u59cb\u7684\u5f62\u5f0f\u6210\u4e3a for \u63a8\u5bfc\u5f0f \n
\n - \u4ea7\u751f\u7684\u7ed3\u679c\u4e3a\u6bcf\u6b21\u8fed\u4ee3\u7684\u503c\u7684\u96c6\u5408 \n
\n for(i <- 1 to 3) yield i % 3<\/code> \/\/ Vector(1, 2, 0)<\/li> \n <\/ul><\/li> \n - \u751f\u6210\u7684\u96c6\u5408\u4e0e\u7b2c\u4e00\u4e2a\u751f\u6210\u5668\u7c7b\u578b\u4e00\u81f4 \n
\n for(c <- "hello"; i <- 0 to 1) yield (c+i).toChar<\/code> \/\/ hieflmlmop<\/li> \n for(i <- 0 to 1; c <- "hello") yield (c+i).toChar<\/code> \/\/ Vector(h, e, l, l, o, i, f, m, m, p)<\/li> \n <\/ul><\/li> \n <\/ul><\/li> \n <\/ul><\/li> \n \u51fd\u6570<\/p> \n
\n - Scala has functions in addition to method<\/li> \n
trait Function...<\/code> \u7684\u5b9e\u4f8b<\/li> \n technically is an object with an apply method<\/code><\/li> \n def abs(x: Double) = if (x >= 0) x else -x<\/code><\/li> \n - \u5fc5\u987b\u6307\u5b9a\u6240\u6709\u53c2\u6570\u7684\u7c7b\u578b\uff1b\u8fd4\u56de\u503c\u4e3a
=<\/code>\u53f3\u8fb9\u7684\u8868\u8fbe\u5f0f\u6216\u8bed\u53e5\u5757\u7684\u6700\u540e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\uff1b\u53ef\u7701\u7565 return<\/code><\/li> \n - \u5982\u679c\u662f\u9012\u5f52\u51fd\u6570\uff0c\u5219\u5fc5\u987b\u6307\u660e\u8fd4\u56de\u7c7b\u578b \n
\n def fac(n: Int): Int = if (n <= 0) 1 else n * fac(n - 1)<\/code><\/li> \n <\/ul><\/li> \n - \u53c2\u6570\u9ed8\u8ba4\u503c\u548c\u547d\u540d\u53c2\u6570 \n
\n def decorate(str: String, left: String = "[", right: String = "]") = left + str + right<\/code><\/li> \n - \u8c03\u7528\u65f6\u53ef\u7ed9\u90e8\u5206\u53c2\u6570\uff0c\u4e5f\u53ef\u7ed9\u5168\u90e8\u53c2\u6570\uff0c\u8fd8\u53ef\u901a\u8fc7\u547d\u540d\u53c2\u6570\u4f20\u503c\u800c\u4e0d\u8003\u8651\u53c2\u6570\u987a\u5e8f \n
\n decorate("a")<\/code> \/\/ [a]<\/li> \n decorate("a", "<<")<\/code> \/\/ <<a]<\/li> \n decorate(left="<", "a")<\/code> \/\/ <a<\/li> \n <\/ul><\/li> \n <\/ul><\/li> \n - \u53ef\u53d8\u53c2\u6570\uff08\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a Seq \u7c7b\u578b\u7684\u53c2\u6570\uff09 \n
\n def sum(args: Int*) ={var result=0; for (a <- args) result += a; result}<\/code><\/li> \n sum(1,2,3)<\/code> \/\/ 6<\/li> \n sum(1 to 5: _*)<\/code> \/\/ 15 \u5f53\u4f20\u9012\u5e8f\u5217\u505a\u4e3a\u53c2\u6570\u65f6\uff0c\u9700\u8981\u6dfb\u52a0 _*<\/code> \u544a\u8bc9\u7f16\u8bd1\u5668\u4f20\u5165\u7684\u4e3a\u53c2\u6570\u5e8f\u5217\uff0c \u800c\u4e0d\u662f Int<\/li> \n <\/ul><\/li> \n <\/ul><\/li> \n \u8fc7\u7a0b Procedures<\/p> \n
\n - \u65e0\u8fd4\u56de\u503c\u7684\u51fd\u6570<\/li> \n
- \u8c03\u7528\u8fc7\u7a0b\u901a\u5e38\u662f\u4e3a\u4e86\u5176\u526f\u4f5c\u7528\uff0c\u5982\u6253\u5370\u7b49<\/li> \n
def box(s: String) { println(s) }<\/code> \/\/ \u65e0\u9700\u8981 =<\/code><\/li> \n <\/ul><\/li> \n lazy<\/code><\/p> \n \n - \u5ef6\u8fdf\u52a0\u8f7d\uff0c\u53d8\u91cf\u5b9a\u4e49\u4e3a lazy \u540e\uff0c\u4f1a\u5728\u7b2c\u4e00\u6b21\u8bbf\u95ee\u65f6\u624d\u88ab\u521d\u59cb\u5316\/\u6267\u884c<\/li> \n
lazy val words = scala.io.Source.fromFile("\/..\/words").mkString<\/code> \/\/ if the program never accesses words<\/code>, the file is never opened<\/li> \n - \u51cf\u5c11\u521d\u59cb\u5316\u6d88\u8017\u3001\u89e3\u51b3\u5faa\u73af\u4f9d\u8d56\u95ee\u9898\u7b49<\/li> \n
- \u4f1a\u6709\u591a\u4f59\u5f00\u9500\uff1a\u6bcf\u6b21\u4f7f\u7528\u5230 lazy","orderid":"0","title":"Scala Basis(\u4e00)","smalltitle":"","mid":"0","fname":"Scala","special_id":"0","bak_id":"0","info":"0","hits":"337","pages":"2","comments":"0","posttime":"2019-09-30 16:47:30","list":"1569833250","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Scala<\/A> Basis<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"14.17.22.32","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Scala Basis","lastview":"1714157571","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}
- \n
- 7 \u79cd\u6570\u503c\u7c7b\u578b: Byte, Char, Short, Int, Long, Float, and Double<\/li> \n
- Boolean \u7c7b\u578b<\/li> \n <\/ul><\/li> \n
- \u539f\u59cb\u7c7b\u578b\u4e0e class \u7c7b\u578b\u65e0\u533a\u522b\uff0c\u53ef\u5728\u6570\u5b57\u4e0a\u8c03\u7528\u65b9\u6cd5\uff08\u9690\u5f0f\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u65b9\u6cd5\u8c03\u7528\uff0c\u5982\u5bf9 Int \u64cd\u4f5c\u8f6c\u4e3a RichInt \u7684\u65b9\u6cd5\u8c03\u7528\u7b49\uff09 \n
- \n
1.toString()<\/code><\/li> \n
1.to(10)<\/code> \/\/ Range(1,2,3,4,5,6,7,8,9,10)<\/li> \n <\/ul><\/li> \n
- \u65e0\u5c01\u88c5\u7c7b\u578b\uff0cScala \u81ea\u52a8\u5904\u7406\u5c01\u5305\u62c6\u5305\uff0c\u5982\u5b57\u7b26\u4e32\u5e95\u5c42\u4f7f\u7528\u7684\u662f java<\/a>.lang.String<\/li> \n
- \u9690\u5f0f\u8f6c\u6362\u7684 StringOps \u5bf9 String \u6269\u5c55\uff0c\u5305\u542b\u4e86\u4e0a\u767e\u79cd\u64cd\u4f5c \n
- \n
"Hello".intersect("World")<\/code> \/\/ "lo"<\/li> \n <\/ul><\/li> \n
- \u64cd\u4f5c\u7b26\u91cd\u8f7d\uff0c\u7b97\u6570\u64cd\u4f5c\u7b26\u4e5f\u662f\u65b9\u6cd5 \n
- \n
1 + 2<\/code> \u7b49\u4ef7\u4e8e
1.+(b)<\/code><\/li> \n
- \u901a\u5e38
a.method(b)<\/code> \u53ef\u7b80\u5199\u4e3a
a method b<\/code><\/li> \n <\/ul><\/li> \n
- \u9012\u589e\u9012\u51cf\uff0c\u6ca1\u6709
++<\/code> \u548c
--<\/code> \u64cd\u4f5c\uff0c\u4f7f\u7528
+=1<\/code> \u548c
-=1<\/code> \u4ee3\u66ff<\/li> \n
- BigInt \u548c BigDecimal \u4e5f\u53ef\u76f4\u63a5\u4f7f\u7528\u7b97\u6570\u8fd0\u7b97\u7b26 \n
- \n
val x: BigInt = 1234567890<\/code><\/li> \n
x * x * x<\/code> \/\/ Java \u9700\u8981\u8c03\u7528\u65b9\u6cd5
x.multiply(x).multiply(x)<\/code><\/li> \n <\/ul><\/li> \n
_<\/code> \u4ee3\u8868\u901a\u914d\u7b26\uff0c\u53ef\u8868\u8fbe\u4efb\u610f\u4e1c\u897f<\/li> \n <\/ul> \n
\u51fd\u6570\u4e0e\u65b9\u6cd5<\/h2> \n
- \n
- \u6570\u5b66\u51fd\u6570\u4f7f\u7528\u65b9\u4fbf\u7b80\u5355\uff0c\u5982 min, max \u4e0d\u9700\u8981\u8c03\u7528\u67d0\u4e2a\u7c7b\u7684\u9759\u6001\u65b9\u6cd5 \uff08\u901a\u8fc7
import scala.math._<\/code> \u5bfc\u5165\u6570\u5b66\u51fd\u6570\u5305\uff09<\/li> \n
- \u53ef\u7701\u7565\u7684 scala \u5305\u540d\u524d\u7f00\uff0c\u5982
import scala.math._<\/code> \u53ef\u5199\u4e3a
import math._<\/code><\/li> \n
- \u65e0\u9759\u6001\u51fd\u6570\uff0c\u4e0e\u4e4b\u529f\u80fd\u7c7b\u4f3c\u7684\u662f\u5355\u4f8b\u5bf9\u8c61\uff1b\u901a\u5e38\u4e00\u4e2a class \u90fd\u4f1a\u6709\u4e00\u4e2a\u4f34\u751f\u5bf9\u8c61\uff0c\u5176\u65b9\u6cd5\u5c31\u5982\u540c Java \u7684\u9759\u6001\u65b9\u6cd5<\/li> \n
- \u65e0\u53c2\u65b9\u6cd5\u8c03\u7528\u65f6\u901a\u5e38\u4e0d\u9700\u8981\u5e26\u62ec\u53f7 \n
- \n
"Hello".distinct<\/code><\/li> \n <\/ul><\/li> \n
apply<\/code> \u65b9\u6cd5 \n
- \n
- \u4f34\u751f\u5bf9\u8c61\u4e2d\u5b9a\u4e49<\/li> \n
- \u50cf\u51fd\u6570\u8c03\u7528\u4e00\u6837\u4f7f\u7528\uff0c\u53ef\u770b\u505a\u662f\u91cd\u8f7d\u4e86
()<\/code><\/li> \n
"Hello"(4)<\/code> \u7b49\u4ef7\u4e8e
"Hello".apply(4)<\/code><\/li> \n <\/ul><\/li> \n <\/ul> \n
\u63a7\u5236\u4f53\u7cfb&\u51fd\u6570<\/h2> \n
- \n
- \u6761\u4ef6\u8868\u8fbe\u5f0f \n
- \n
if\/else<\/code> \u8868\u8fbe\u5f0f\u6709\u8fd4\u56de\u503c \n
- \n
val s = if (a > 0) 1 else -1<\/code> \/\/ \u8fd9\u79cd\u65b9\u5f0f\u4e0b s \u5b9a\u4e49\u4e3a val\uff0c\u5982\u679c\u653e\u5230\u5224\u65ad\u5185\u90e8\u8d4b\u503c\uff0c\u9700\u8981\u5b9a\u4e49\u4e3a\u53d8\u91cf var<\/li> \n
- \u7edf\u4e00\u4e86\u4e09\u76ee\u8fd0\u7b97
?:<\/code> \u548c
if\/else<\/code>\uff1bScala \u65e0\u4e09\u76ee\u8fd0\u7b97<\/li> \n
if (a) 1<\/code> \u7b49\u4ef7\u4e8e
if (a) 1 else ()<\/code>\uff1b\u53ef\u4ee5\u5c06
()<\/code>(\uff0c\u5373 Unit \u7c7b) \u89c6\u4e3a\u65e0\u7528\u503c\u7684\u5360\u4f4d\u7b26\uff0c\u53ef\u770b\u505a Java \u4e2d\u7684 void<\/li> \n <\/ul><\/li> \n
- \u65e0
switch<\/code> \u8868\u8fbe\u5f0f\uff0c\u800c\u662f\u4f7f\u7528\u66f4\u4e3a\u5f3a\u5927\u7684\u6a21\u5f0f\u5339\u914d\u6765\u66ff\u4ee3<\/li> \n <\/ul><\/li> \n
- \u8bed\u53e5\u7ec8\u7ed3 \n
- \n
- \u5206\u53f7\u975e\u5fc5\u987b\uff0c\u5355\u884c\u5355\u8868\u8fbe\u5f0f\u53ef\u7701\u7565\uff0c\u5355\u884c\u591a\u8868\u8fbe\u5f0f\u9700\u52a0\u5206\u53f7\u533a\u5206<\/li> \n <\/ul><\/li> \n
\u8bed\u53e5\u5757&\u8d4b\u503c<\/p> \n
- \n
{...}<\/code> \u5305\u542b\u4e00\u7cfb\u5217\u8868\u8fbe\u5f0f\uff0c\u8bed\u53e5\u5757\u7684\u7ed3\u679c\u4e3a\u6700\u540e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c<\/li> \n
- \u53ef\u7528\u4e8e\u521d\u59cb\u5316\u9700\u8981\u591a\u6b65\u64cd\u4f5c\u7684\u503c \n
- \n
val a = { express1; express2; express3 }<\/code><\/li> \n <\/ul><\/li> \n
- \u8d4b\u503c\u8bed\u53e5\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u4e0d\u53ef\u4f7f\u7528\u94fe\u5f0f\u8d4b\u503c
x=y=1<\/code> \/\/ \u4e0e\u9884\u671f\u7ed3\u679c\u4e0d\u4e00\u81f4<\/li> \n <\/ul><\/li> \n
IO<\/p> \n
- \n
- \u6253\u5370\uff0c
print \/ println \/ printf<\/code><\/li> \n
- \u8bfb\u53d6 console \u8f93\u5165\uff0c
readLine \/ readInt \/ readDouble...<\/code><\/li> \n <\/ul><\/li> \n
\u5faa\u73af<\/p> \n
- \n
- \u4e0d\u50cf\u5176\u4ed6\u8bed\u8a00\u90a3\u4e48\u5e38\u7528\uff0c\u901a\u5e38\u53ef\u4f7f\u7528\u5355\u4e2a\u65b9\u6cd5\u8c03\u7528\u5b8c\u6210\u6240\u6709\u64cd\u4f5c<\/li> \n
- while, do<\/li> \n
- \u6ca1\u6709\u4e0e Java \u7c7b\u4f3c\u7684 for \u5faa\u73af
for(init; test; update)<\/code>\uff0c\u53ef\u4f7f\u7528 while \u4ee3\u66ff\uff0c\u6216\u8005\u4f7f\u7528 for \u8868\u8fbe\u5f0f \n
- \n
for (i <- 1 to 10) r = r * i<\/code><\/li> \n
- \u751f\u6210\u5668
variable <- expression<\/code> \u4f1a\u904d\u5386\u6240\u6709\u5143\u7d20<\/li> \n
- for \u5faa\u73af\u53ef\u5305\u542b\u591a\u4e2a\u751f\u6210\u5668\uff0c\u9017\u53f7\u5206\u9694\uff08\u6216\u6362\u884c\u533a\u5206\uff09\uff0c\u53ef\u4f7f\u7528 parttern guard \u6765\u8fdb\u884c\u6761\u4ef6\u8fc7\u6ee4 \n
- \n
for(v <- exp1; v2 <- exp2 if(condition)) doSome()<\/code> \/\/ if \u4e4b\u524d\u7684\u5206\u53f7\u53ef\u7701\u7565<\/li> \n <\/ul><\/li> \n
- for \u8bed\u53e5\u4e2d\u7684\u53d8\u91cf\u4e0d\u9700\u8981\u58f0\u660e val \u6216 var\uff0c\u5176\u7c7b\u578b\u4e0e\u8fed\u4ee3\u7684\u96c6\u5408\u4e2d\u5143\u7d20\u7c7b\u578b\u4e00\u81f4<\/li> \n
1 to n<\/code> \u5305\u542b\u4e0a\u754c\uff0c
1 until n<\/code> \u4e0d\u5305\u542b\u4e0a\u754c<\/li> \n <\/ul><\/li> \n
\u6ca1\u6709 break\uff0ccontinue \u8868\u8fbe\u5f0f\u6765\u4e2d\u65ad\u5faa\u73af\uff0c\u66ff\u4ee3\u65b9\u6848\uff1a<\/p> \n
- \n
- Boolean \u53d8\u91cf\u63a7\u5236<\/li> \n
- \u5d4c\u5957\u51fd\u6570<\/li> \n
- \u4f7f\u7528 Breaks \u5bf9\u8c61\u7684 break \u65b9\u6cd5<\/li> \n <\/ul>
import scala.util.control.Breaks._\nbreakable {\n for (...) {\n if (...) break\n }\n}<\/code><\/pre><\/li> \n
- yield\uff0c\u5728 for \u5faa\u73af\u4f53\u4ee5 yield \u5f00\u59cb\u7684\u5f62\u5f0f\u6210\u4e3a for \u63a8\u5bfc\u5f0f \n
- \n
- \u4ea7\u751f\u7684\u7ed3\u679c\u4e3a\u6bcf\u6b21\u8fed\u4ee3\u7684\u503c\u7684\u96c6\u5408 \n
- \n
for(i <- 1 to 3) yield i % 3<\/code> \/\/ Vector(1, 2, 0)<\/li> \n <\/ul><\/li> \n
- \u751f\u6210\u7684\u96c6\u5408\u4e0e\u7b2c\u4e00\u4e2a\u751f\u6210\u5668\u7c7b\u578b\u4e00\u81f4 \n
- \n
for(c <- "hello"; i <- 0 to 1) yield (c+i).toChar<\/code> \/\/ hieflmlmop<\/li> \n
for(i <- 0 to 1; c <- "hello") yield (c+i).toChar<\/code> \/\/ Vector(h, e, l, l, o, i, f, m, m, p)<\/li> \n <\/ul><\/li> \n <\/ul><\/li> \n <\/ul><\/li> \n
\u51fd\u6570<\/p> \n
- \n
- Scala has functions in addition to method<\/li> \n
trait Function...<\/code> \u7684\u5b9e\u4f8b<\/li> \n
technically is an object with an apply method<\/code><\/li> \n
def abs(x: Double) = if (x >= 0) x else -x<\/code><\/li> \n
- \u5fc5\u987b\u6307\u5b9a\u6240\u6709\u53c2\u6570\u7684\u7c7b\u578b\uff1b\u8fd4\u56de\u503c\u4e3a
=<\/code>\u53f3\u8fb9\u7684\u8868\u8fbe\u5f0f\u6216\u8bed\u53e5\u5757\u7684\u6700\u540e\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\uff1b\u53ef\u7701\u7565
return<\/code><\/li> \n
- \u5982\u679c\u662f\u9012\u5f52\u51fd\u6570\uff0c\u5219\u5fc5\u987b\u6307\u660e\u8fd4\u56de\u7c7b\u578b \n
- \n
def fac(n: Int): Int = if (n <= 0) 1 else n * fac(n - 1)<\/code><\/li> \n <\/ul><\/li> \n
- \u53c2\u6570\u9ed8\u8ba4\u503c\u548c\u547d\u540d\u53c2\u6570 \n
- \n
def decorate(str: String, left: String = "[", right: String = "]") = left + str + right<\/code><\/li> \n
- \u8c03\u7528\u65f6\u53ef\u7ed9\u90e8\u5206\u53c2\u6570\uff0c\u4e5f\u53ef\u7ed9\u5168\u90e8\u53c2\u6570\uff0c\u8fd8\u53ef\u901a\u8fc7\u547d\u540d\u53c2\u6570\u4f20\u503c\u800c\u4e0d\u8003\u8651\u53c2\u6570\u987a\u5e8f \n
- \n
decorate("a")<\/code> \/\/ [a]<\/li> \n
decorate("a", "<<")<\/code> \/\/ <<a]<\/li> \n
decorate(left="<", "a")<\/code> \/\/ <a<\/li> \n <\/ul><\/li> \n <\/ul><\/li> \n
- \u53ef\u53d8\u53c2\u6570\uff08\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a Seq \u7c7b\u578b\u7684\u53c2\u6570\uff09 \n
- \n
def sum(args: Int*) ={var result=0; for (a <- args) result += a; result}<\/code><\/li> \n
sum(1,2,3)<\/code> \/\/ 6<\/li> \n
sum(1 to 5: _*)<\/code> \/\/ 15 \u5f53\u4f20\u9012\u5e8f\u5217\u505a\u4e3a\u53c2\u6570\u65f6\uff0c\u9700\u8981\u6dfb\u52a0
_*<\/code> \u544a\u8bc9\u7f16\u8bd1\u5668\u4f20\u5165\u7684\u4e3a\u53c2\u6570\u5e8f\u5217\uff0c \u800c\u4e0d\u662f Int<\/li> \n <\/ul><\/li> \n <\/ul><\/li> \n
\u8fc7\u7a0b Procedures<\/p> \n
- \n
- \u65e0\u8fd4\u56de\u503c\u7684\u51fd\u6570<\/li> \n
- \u8c03\u7528\u8fc7\u7a0b\u901a\u5e38\u662f\u4e3a\u4e86\u5176\u526f\u4f5c\u7528\uff0c\u5982\u6253\u5370\u7b49<\/li> \n
def box(s: String) { println(s) }<\/code> \/\/ \u65e0\u9700\u8981
=<\/code><\/li> \n <\/ul><\/li> \n
lazy<\/code><\/p> \n
- \n
- \u5ef6\u8fdf\u52a0\u8f7d\uff0c\u53d8\u91cf\u5b9a\u4e49\u4e3a lazy \u540e\uff0c\u4f1a\u5728\u7b2c\u4e00\u6b21\u8bbf\u95ee\u65f6\u624d\u88ab\u521d\u59cb\u5316\/\u6267\u884c<\/li> \n
lazy val words = scala.io.Source.fromFile("\/..\/words").mkString<\/code> \/\/ if the program never accesses
words<\/code>, the file is never opened<\/li> \n
- \u51cf\u5c11\u521d\u59cb\u5316\u6d88\u8017\u3001\u89e3\u51b3\u5faa\u73af\u4f9d\u8d56\u95ee\u9898\u7b49<\/li> \n
- \u4f1a\u6709\u591a\u4f59\u5f00\u9500\uff1a\u6bcf\u6b21\u4f7f\u7528\u5230 lazy","orderid":"0","title":"Scala Basis(\u4e00)","smalltitle":"","mid":"0","fname":"Scala","special_id":"0","bak_id":"0","info":"0","hits":"337","pages":"2","comments":"0","posttime":"2019-09-30 16:47:30","list":"1569833250","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Scala<\/A> Basis<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"14.17.22.32","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Scala Basis","lastview":"1714157571","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}
- \u8c03\u7528\u65f6\u53ef\u7ed9\u90e8\u5206\u53c2\u6570\uff0c\u4e5f\u53ef\u7ed9\u5168\u90e8\u53c2\u6570\uff0c\u8fd8\u53ef\u901a\u8fc7\u547d\u540d\u53c2\u6570\u4f20\u503c\u800c\u4e0d\u8003\u8651\u53c2\u6570\u987a\u5e8f \n
- \u53c2\u6570\u9ed8\u8ba4\u503c\u548c\u547d\u540d\u53c2\u6570 \n
- \u751f\u6210\u7684\u96c6\u5408\u4e0e\u7b2c\u4e00\u4e2a\u751f\u6210\u5668\u7c7b\u578b\u4e00\u81f4 \n
- yield\uff0c\u5728 for \u5faa\u73af\u4f53\u4ee5 yield \u5f00\u59cb\u7684\u5f62\u5f0f\u6210\u4e3a for \u63a8\u5bfc\u5f0f \n
- \u751f\u6210\u5668
- \u8bfb\u53d6 console \u8f93\u5165\uff0c
- \u8d4b\u503c\u8bed\u53e5\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u4e0d\u53ef\u4f7f\u7528\u94fe\u5f0f\u8d4b\u503c
- \u53ef\u7528\u4e8e\u521d\u59cb\u5316\u9700\u8981\u591a\u6b65\u64cd\u4f5c\u7684\u503c \n
- \u7edf\u4e00\u4e86\u4e09\u76ee\u8fd0\u7b97
- \u53ef\u7701\u7565\u7684 scala \u5305\u540d\u524d\u7f00\uff0c\u5982
- \u901a\u5e38
- \u64cd\u4f5c\u7b26\u91cd\u8f7d\uff0c\u7b97\u6570\u64cd\u4f5c\u7b26\u4e5f\u662f\u65b9\u6cd5 \n