标签:ima png span list 返回值 info 匿名 art 闭包
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‘); }
打印结果:
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
另外,正常参数必须在可选参数之前。
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‘); }
打印结果:
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
标签:ima png span list 返回值 info 匿名 art 闭包
原文地址:https://www.cnblogs.com/wangwenkai/p/11629990.html