资源描述:
《Functions and Methods》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、FunctionsandMethodsFunctionsandMethods函数和方法☺agentzh(章亦春)☺2006.10☆Consistentinterface,Simpleinterface,richinterface一致的接口,简单的接口,丰富的接口C#seemstohavehundredsofdifferentcollectionclasses,usedinconsistentlyinthe.NETlibraries.--NedBatchelderC#似乎拥有成百上千个不同的集合类,它们在.NET类库中的使用是如此的不一致。Perl6'sarray➥Simp
2、leinterfacewithrichfunctionalitiesPerl6数组:简单的接口,丰富的功能。#useitasanordinaryarray:@item='dog','cat','mouse','tiger';@item[0].say;#prints'dog'say~@item[1..-1];#prints'catmousetiger'@item[2]='human';@item.push('camel','moose');#appendelements@item=(@item,'camel','moose');#justthesame#useitasas
3、tack(FILO):@item.push('moose');push@item,'elk';#justanotherway$top=@item[-1];$top=@item.pop();$top=@item.pop;#ditto#useitasaqueue(FIFO):@item.unshift(256);unshift@item,128,'hello';$elem=@item.shift();$elem=@item.shift;$elem=shift@item;say"lengthofthequeue:",@elem.elems;Makesimplethingsea
4、syandhardthingspossible.--LarryWall让简单的事情很容易办到,让困难的事情有可能办到。Writingastringtoafileshouldbeasimpletask,however...向一个文件写入一个字符串本该是一件很简单的任务,可是……#TheJavaway:importjava.io.*;classWriteFile{publicstaticvoidmain(Stringargs[]){FileOutputStreamfoStream;PrintStreampStream;try{foStream=newFileOutputStr
5、eam("somefile.txt");pStream=newPrintStream(foStream);pStream.println("Thisiswrittentoafile");pStream.close();}catch(Exceptione){System.err.println("Errorwritingtofile"+e);}}}/*TheCway:*/#include#includeintmain(){FILE*fh;if((fh=fopen("somefile.txt","w")==NULL){fprintf(stder
6、r,"Can'topenfile:%s",strerror(errno));return1;}fprintf(fh,"Thisiswrittentoafile");fclose(fh);return0;}#ThePerl5way:openmy$fh,">somefile.txt"ordie"Can'topenfile:$!";print$fh"Thisiswrittentoafile"ordie"Can'tprinttofile:$!";close$fh;☆outputarguments<=>returnvalues输出参数<=>返回值/*passresultsviaret
7、urnvalues*/intadd(inta,intb){returna+b;}...if(add(1,2)==3){printf("ok");}printf("%d",add(5,6));/*passresultsviaoutputarguments*/voidadd(inta,intb,int&c){c=a+b;}...inttemp;add(1,2,temp);if(temp==3){printf("ok");}add(5,6,temp);printf("%d",temp);☆Multiplereturnvalues多重返回值#Th