{"rsdb":{"rid":"351962","subhead":"","postdate":"0","aid":"249824","fid":"90","uid":"1","topic":"1","content":"
\n

\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf<\/span><\/h3> \n

\/\/ Scala\u4e2d\u7684\u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11\uff0c\u53ef\u4ee5\u72ec\u7acb\u5b9a\u4e49\uff0c\u72ec\u7acb\u5b58\u5728\uff0c\u800c\u4e14\u53ef\u4ee5\u76f4\u63a5\u5c06\u51fd\u6570\u4f5c\u4e3a\u503c\u8d4b\u503c\u7ed9\u53d8\u91cf<\/span><\/p> \n

\/\/ Scala\u7684\u8bed\u6cd5\u89c4\u5b9a\uff0c\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf\u65f6\uff0c\u5fc5\u987b\u5728\u51fd\u6570\u540e\u9762\u52a0\u4e0a\u7a7a\u683c\u548c\u4e0b\u5212\u7ebf<\/span><\/p> \n

 <\/p> \n

def sayHello(name: String) { println("Hello, " + name) }<\/p> \n

val sayHelloFunc = sayHello _<\/p> \n

sayHelloFunc("leo")<\/p> \n

 <\/p> \n

 <\/p> \n

 \u533f\u540d\u51fd\u6570<\/span><\/strong><\/h3> \n

\/\/ Scala\u4e2d\uff0c\u51fd\u6570\u4e5f\u53ef\u4ee5\u4e0d\u9700\u8981\u547d\u540d\uff0c\u6b64\u65f6\u51fd\u6570\u88ab\u79f0\u4e3a\u533f\u540d\u51fd\u6570\u3002<\/span>\/\/ <\/span>\u53ef\u4ee5\u76f4\u63a5\u5b9a\u4e49\u51fd\u6570\u4e4b\u540e\uff0c\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u67d0\u4e2a\u53d8\u91cf\uff1b\u4e5f\u53ef\u4ee5\u5c06\u76f4\u63a5\u5b9a\u4e49\u7684\u533f\u540d\u51fd\u6570\u4f20\u5165\u5176\u4ed6\u51fd\u6570\u4e4b\u4e2d<\/span><\/p> \n

\/\/ Scala\u5b9a\u4e49\u533f\u540d\u51fd\u6570\u7684\u8bed\u6cd5\u89c4\u5219\u5c31\u662f\uff0c<\/span>(<\/span>\u53c2\u6570\u540d<\/span>: <\/span>\u53c2\u6570\u7c7b\u578b<\/span>) => <\/span>\u51fd\u6570\u4f53<\/span><\/p> \n

\/\/ \u8fd9\u79cd\u533f\u540d\u51fd\u6570\u7684\u8bed\u6cd5\u5fc5\u987b\u6df1\u523b\u7406\u89e3\u548c\u638c\u63e1\uff0c\u5728<\/span>spark<\/span>\u7684\u4e2d\u6709\u5927\u91cf\u8fd9\u6837\u7684\u8bed\u6cd5\uff0c\u5982\u679c\u6ca1\u6709\u638c\u63e1\uff0c\u662f\u770b\u4e0d\u61c2<\/span>spark<\/span>\u6e90\u7801\u7684<\/span><\/p> \n

 <\/p> \n

val sayHelloFunc = (name: String) => println("Hello, " + name)<\/p> \n

 <\/p> \n

 <\/p> \n

\u9ad8\u9636\u51fd\u6570<\/strong><\/h3> \n

\/\/ Scala\u4e2d\uff0c\u7531\u4e8e\u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u5c06\u67d0\u4e2a\u51fd\u6570\u4f20\u5165\u5176\u4ed6\u51fd\u6570\uff0c\u4f5c\u4e3a\u53c2\u6570\u3002\u8fd9\u4e2a\u529f\u80fd\u662f\u6781\u5176\u5f3a\u5927\u7684\uff0c\u4e5f\u662f<\/span>Java<\/span>\u8fd9\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u8bed\u8a00\u6240\u4e0d\u5177\u5907\u7684\u3002<\/span><\/p> \n

\/\/ \u63a5\u6536\u5176\u4ed6\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u7684\u51fd\u6570\uff0c\u4e5f\u88ab\u79f0\u4f5c\u9ad8\u9636\u51fd\u6570\uff08<\/span>higher-order function<\/span>\uff09<\/span><\/p> \n

val sayHelloFunc = (name: String) => println("Hello, " + name)<\/p> \n

def greeting(func: (String) => Unit, name: String) { func(name) }<\/p> \n

greeting(sayHelloFunc, "leo")<\/p> \n

 <\/p> \n

Array(1, 2, 3, 4, 5).map((num: Int) => num * num)<\/p> \n

 <\/p> \n

\/\/ \u9ad8\u9636\u51fd\u6570\u7684\u53e6\u5916\u4e00\u4e2a\u529f\u80fd\u662f\u5c06\u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c<\/span><\/p> \n

def getGreetingFunc(msg: String) = (name: String) => println(msg + ", " + name)<\/p> \n

val greetingFunc = getGreetingFunc("hello")<\/p> \n

greetingFunc("leo")<\/p> \n

 <\/p> \n

\u9ad8\u9636\u51fd\u6570\u7684\u7c7b\u578b\u63a8\u65ad<\/strong><\/h3> \n

 <\/p> \n

\/\/ \u9ad8\u9636\u51fd\u6570\u53ef\u4ee5\u81ea\u52a8\u63a8\u65ad\u51fa\u53c2\u6570\u7c7b\u578b\uff0c\u800c\u4e0d\u9700\u8981\u5199\u660e\u7c7b\u578b\uff1b\u800c\u4e14\u5bf9\u4e8e\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u7684\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u7701\u53bb\u5176\u5c0f\u62ec\u53f7\uff1b\u5982\u679c\u4ec5\u6709\u7684\u4e00\u4e2a\u53c2\u6570\u5728\u53f3\u4fa7\u7684\u51fd\u6570\u4f53\u5185\u53ea\u4f7f\u7528\u4e00\u6b21\uff0c\u5219\u8fd8\u53ef\u4ee5\u5c06\u63a5\u6536\u53c2\u6570\u7701\u7565\uff0c\u5e76\u4e14\u5c06\u53c2\u6570\u7528<\/span>_<\/span>\u6765\u66ff\u4ee3<\/span><\/p> \n

\/\/ \u8bf8\u5982<\/span>3 * _<\/span>\u7684\u8fd9\u79cd\u8bed\u6cd5\uff0c\u5fc5\u987b\u638c\u63e1\uff01\uff01<\/span>spark<\/span>\u6e90\u7801\u4e2d\u5927\u91cf\u4f7f\u7528\u4e86\u8fd9\u79cd\u8bed\u6cd5\uff01<\/span><\/span><\/p> \n

 <\/p> \n

def greeting(func: (String) => Unit, name: String) { func(name) }<\/p> \n

greeting((name: String) => println("Hello, " + name), "leo")<\/p> \n

greeting((name) => println("Hello, " + name), "leo")<\/p> \n

greeting(name => println("Hello, " + name), "leo")<\/p> \n

 <\/p> \n

def triple(func: (Int) => Int) = { func(3) }<\/p> \n

triple(3 * _)<\/p> \n

 <\/p> \n

Scala\u7684\u5e38\u7528\u9ad8\u9636\u51fd\u6570<\/span><\/strong><\/h3> \n

\/\/ map: \u5bf9\u4f20\u5165\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8fdb\u884c\u6620\u5c04\uff0c\u8fd4\u56de\u4e00\u4e2a\u5904\u7406\u540e\u7684\u5143\u7d20<\/span><\/p> \n

Array(1, 2, 3, 4, 5).map(2 * _)<\/p> \n

 <\/p> \n

\/\/ foreach: \u5bf9\u4f20\u5165\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8fdb\u884c\u5904\u7406\uff0c\u4f46\u662f\u6ca1\u6709\u8fd4\u56de\u503c<\/span><\/p> \n

(1 to 9).map("*" * _).foreach(println _)<\/p> \n

 <\/p> \n

\/\/ filter: \u5bf9\u4f20\u5165\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\uff0c\u5982\u679c\u5bf9\u5143\u7d20\u8fd4\u56de<\/span>true<\/span>\uff0c\u5219\u4fdd\u7559\u8be5\u5143\u7d20\uff0c\u5426\u5219\u8fc7\u6ee4\u6389\u8be5\u5143\u7d20<\/span><\/p> \n

(1 to 20).filter(_ % 2 == 0)<\/p> \n

 <\/p> \n

\/\/ reduceLeft: \u4ece\u5de6\u4fa7\u5143\u7d20\u5f00\u59cb\uff0c\u8fdb\u884c<\/span>reduce<\/span>\u64cd\u4f5c\uff0c\u5373\u5148\u5bf9\u5143\u7d20<\/span>1<\/span>\u548c\u5143\u7d20<\/span>2<\/span>\u8fdb\u884c\u5904\u7406\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u4e0e\u5143\u7d20<\/span>3<\/span>\u5904\u7406\uff0c\u518d\u5c06\u7ed3\u679c\u4e0e\u5143\u7d20<\/span>4<\/span>\u5904\u7406\uff0c\u4f9d\u6b21\u7c7b\u63a8\uff0c\u5373\u4e3a<\/span>reduce<\/span>\uff1b<\/span>reduce<\/span>\u64cd\u4f5c\u5fc5\u987b\u638c\u63e1\uff01<\/span>spark<\/span>\u7f16\u7a0b\u7684\u91cd\u70b9\uff01\uff01\uff01<\/span><\/span><\/p> \n

\/\/ \u4e0b\u9762\u8fd9\u4e2a\u64cd\u4f5c\u5c31\u76f8\u5f53\u4e8e<\/span>1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9<\/span><\/p> \n

(1 to 9).reduceLeft( _ * _)<\/span><\/p> \n

 <\/p> \n

\/\/ sortWith: \u5bf9\u5143\u7d20\u8fdb\u884c\u4e24\u4e24\u76f8\u6bd4\uff0c\u8fdb\u884c\u6392\u5e8f<\/span><\/p> \n

Array(3, 2, 5, 4, 10, 1).sortWith(_ < _)<\/p> \n

 <\/p> \n

\u95ed\u5305<\/strong><\/h3> \n

\/\/ \u95ed\u5305\u6700\u7b80\u6d01\u7684\u89e3\u91ca\uff1a\u51fd\u6570\u5728\u53d8\u91cf\u4e0d\u5904\u4e8e\u5176\u6709\u6548\u4f5c\u7528\u57df\u65f6\uff0c\u8fd8\u80fd\u591f\u5bf9\u53d8\u91cf\u8fdb\u884c\u8bbf\u95ee\uff0c\u5373\u4e3a\u95ed\u5305<\/span><\/p> \n

 <\/p> \n

def getGreetingFunc(msg: String) = (name: String) => println(msg + ", " + name)<\/p> \n

val greetingFuncHello = getGreetingFunc("hello")<\/p> \n

val greetingFuncHi = getGreetingFunc("hi")<\/p> \n

 <\/p> \n

\/\/ \u4e24\u6b21\u8c03\u7528<\/span>getGreetingFunc<\/span>\u51fd\u6570\uff0c\u4f20\u5165\u4e0d\u540c\u7684<\/span>msg<\/span>\uff0c\u5e76\u521b\u5efa\u4e0d\u540c\u7684\u51fd\u6570\u8fd4\u56de<\/span><\/p> \n

\/\/ \u7136\u800c\uff0c<\/span>msg<\/span>\u53ea\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u5374\u5728<\/span>getGreetingFunc<\/span>\u6267\u884c\u5b8c\u4e4b\u540e\uff0c\u8fd8\u53ef\u4ee5\u7ee7\u7eed\u5b58\u5728\u521b\u5efa\u7684\u51fd\u6570\u4e4b\u4e2d\uff1b<\/span>greetingFuncHello("leo")<\/span>\uff0c\u8c03\u7528\u65f6\uff0c\u503c\u4e3a<\/span>"hello"<\/span>\u7684<\/span>msg<\/span>\u88ab\u4fdd\u7559\u5728\u4e86\u51fd\u6570\u4f53\u5185\u90e8\uff0c\u53ef\u4ee5\u53cd\u590d\u7684\u4f7f\u7528<\/span><\/p> \n

\/\/ \u8fd9\u79cd\u53d8\u91cf\u8d85\u51fa\u4e86\u5176\u4f5c\u7528\u57df\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7684\u60c5\u51b5\uff0c\u5373\u4e3a\u95ed\u5305<\/span><\/p> \n

 <\/p> \n

\/\/ Scala\u901a\u8fc7\u4e3a\u6bcf\u4e2a\u51fd\u6570\u521b\u5efa\u5bf9\u8c61\u6765\u5b9e\u73b0\u95ed\u5305\uff0c\u5b9e\u9645\u4e0a\u5bf9\u4e8e<\/span>getGreetingFunc<\/span>\u51fd\u6570\u521b\u5efa\u7684\u51fd\u6570\uff0c<\/span>msg<\/span>\u662f\u4f5c\u4e3a\u51fd\u6570\u5bf9\u8c61\u7684\u53d8\u91cf\u5b58\u5728\u7684\uff0c\u56e0\u6b64\u6bcf\u4e2a\u51fd\u6570\u624d\u53ef\u4ee5\u62e5\u6709\u4e0d\u540c\u7684<\/span>msg<\/span><\/p> \n

 <\/p> \n

\/\/ Scala\u7f16\u8bd1\u5668\u4f1a\u786e\u4fdd\u4e0a\u8ff0\u95ed\u5305\u673a\u5236<\/span><\/p> \n

 <\/p> \n

SAM\u8f6c\u6362<\/span><\/strong><\/h3> \n

\/\/ \u5728<\/span>Java<\/span>\u4e2d\uff0c\u4e0d\u652f\u6301\u76f4\u63a5\u5c06\u51fd\u6570\u4f20\u5165\u4e00\u4e2a\u65b9\u6cd5\u4f5c\u4e3a\u53c2\u6570\uff0c\u901a\u5e38\u6765\u8bf4\uff0c\u552f\u4e00\u7684\u529e\u6cd5\u5c31\u662f\u5b9a\u4e49\u4e00\u4e2a\u5b9e\u73b0\u4e86\u67d0\u4e2a\u63a5\u53e3\u7684\u7c7b\u7684\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u53ea\u6709\u4e00\u4e2a\u65b9\u6cd5\uff1b\u800c\u8fd9\u4e9b\u63a5\u53e3\u90fd\u53ea\u6709\u5355\u4e2a\u7684\u62bd\u8c61\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f<\/span>single abstract method<\/span>\uff0c\u7b80\u79f0\u4e3a<\/span>SAM<\/span><\/p> \n

 <\/p> \n

\/\/ \u7531\u4e8e<\/span>Scala<\/span>\u662f\u53ef\u4ee5\u8c03\u7528<\/span>Java<\/span>\u7684\u4ee3\u7801\u7684\uff0c\u56e0\u6b64\u5f53\u6211\u4eec\u8c03\u7528<\/span>Java<\/span>\u7684\u67d0\u4e2a\u65b9\u6cd5\u65f6\uff0c\u53ef\u80fd\u5c31\u4e0d\u5f97\u4e0d\u521b\u5efa<\/span>SAM<\/span>\u4f20\u9012\u7ed9\u65b9\u6cd5\uff0c\u975e\u5e38\u9ebb\u70e6\uff1b\u4f46\u662f<\/span>Scala<\/span>\u53c8\u662f\u652f\u6301\u76f4\u63a5\u4f20\u9012\u51fd\u6570\u7684\u3002\u6b64\u65f6\u5c31\u53ef\u4ee5\u4f7f\u7528<\/span>Scala<\/span>\u63d0\u4f9b\u7684\uff0c\u5728\u8c03\u7528<\/span>Java<\/span>\u65b9\u6cd5\u65f6\uff0c\u4f7f\u7528\u7684\u529f\u80fd\uff0c<\/span>SAM<\/span>\u8f6c\u6362\uff0c\u5373\u5c06<\/span>SAM<\/span>\u8f6c\u6362\u4e3a<\/span>Scala<\/span>\u51fd\u6570<\/span><\/p> \n

 <\/p> \n

\/\/ \u8981\u4f7f\u7528<\/span>SAM<\/span>\u8f6c\u6362\uff0c\u9700\u8981\u4f7f\u7528<\/span>Scala<\/span>\u63d0\u4f9b\u7684\u7279\u6027\uff0c\u9690\u5f0f\u8f6c\u6362<\/span><\/p> \n

 <\/p> \n

import java<\/a>x.swing._<\/p> \n

import java<\/a>.awt.event._<\/p> \n

 <\/p> \n

val button = new JButt","orderid":"0","title":"Scala \u51fd\u6570\u5f0f\u7f16\u7a0b(\u4e00)","smalltitle":"","mid":"0","fname":"Scala","special_id":"0","bak_id":"0","info":"0","hits":"181","pages":"2","comments":"0","posttime":"2019-09-03 03:46:48","list":"1567453608","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> \u51fd\u6570<\/A> \u7f16\u7a0b<\/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":"120.229.33.54","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Scala \u51fd\u6570\u5f0f\u7f16\u7a0b","lastview":"1713487517","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}