void main(List args){ print(args); print(getPerson('wwk', 32)); } /*String getPerson(String name,int age){ return 'name = $name,age = $age'; }*/ //返回值的類型 參數的類型均可以省略 /*getPerson(name,age){ return 'name = $name,age = $age'; }*/ //箭頭語法 getPerson(name,age)=>'name = $name,age = $age';
打印結果:閉包
[]
name = wwk,age = 32
void main(){ printPerson('張三'); printPerson('張三',age: 18); printPerson('張三',age: 18,gender: 'Male'); print('--------------'); printPerson2('李四'); printPerson2('李四', 30); printPerson2('李四', 30, 'Female'); } void printPerson(String name,{int age,String gender}){ print('name=$name,age=$age,gender=$gender'); } void printPerson2( name,[int age,String gender]){ print('name=$name,age=$age,gender=$gender'); }
打印結果:spa
name=張三,age=null,gender=null name=張三,age=18,gender=null name=張三,age=18,gender=Male -------------- name=李四,age=null,gender=null name=李四,age=30,gender=null name=李四,age=30,gender=Female
另外,正常參數必須在可選參數以前。3d
void main(){ printPerson('張三'); printPerson('張三',age: 18); printPerson('張三',age: 18,gender: 'Male'); } void printPerson(String name,{int age = 30,String gender = 'Female'}){ print('name=$name,age=$age,gender=$gender'); }
打印結果:code
name=張三,age=30,gender=Female name=張三,age=18,gender=Female name=張三,age=18,gender=Male
void main(){ // var fun = printHello; //方法能夠做爲個對象傳遞給其餘變量 Function fun = printHello; fun(); //方法能夠做爲參數傳遞給其餘方法 var list = [1,2,3,4]; list.forEach(print); } void printHello(){ print('Hello'); }
打印結果:對象
Hello
1
2
3
4