网站首页 > 知识剖析 正文
函数是程序执行的最基本单位 ,是每种编程语言共有的概念,对于Dart来说,也不例外,今天学习这些重要的内容。
一 例程代码
void main()
{
//函数定义
getCurrentDateTime();
getCurrentDateTime01(7);
//带参数的函数
prinBeijingPosion(personName: 'sgd01');
prinBeijingPosion(personName: '02',clientId: 999);
DateTime timeNow = getCurrentDateTime03(0);
DateTime timediff = getCurrentDateTime03(-7);
print('the time now is: $timeNow');
print('the time now is: $timediff');
int value = 5;
//匿名函数
int ex1Squared(num1) => num1 * num1;
int ex1Cubed(num1) => num1 * num1 *num1;
int ex2Squared(num1){return num1 * num1; }
int ex2Cubed(num1){return num1 * num1*num1; }
print('EX1:$value squqred is ${ex1Squared(value)}');
print('EX1:$value cubed is ${ex1Cubed(value)}');
print('EX2:$value squqred is ${ex2Squared(value)}');
print('EX2:$value cubed is ${ex2Cubed(value)}');
}
void getCurrentDateTime()
{
var timeBeijing = DateTime.now();
print('Beijing: $timeBeijing');
}
void getCurrentDateTime01(int hourdi)
{
var timeNow = DateTime.now();
var timediff = timeNow.add(Duration(hours: hourdi));
print('The time now is:$timeNow');
print('The time minus 7 hours is:$timediff');
}
void prinBeijingPosion({String personName = 'sgd',int clientId = 1000})
{
if(personName.contains('sgd'))
{
print('$clientId is right');
}
else
{
print('ERRO');
}
}
DateTime getCurrentDateTime03( int hourdiff)
{
DateTime timeNow = DateTime.now();
DateTime timeDiff = timeNow.add(Duration(hours:hourdiff));
return timeDiff;
}
二 总结
在 Dart 语言中,函数(Functions)是程序执行的基本单元,用于封装可重用的逻辑代码块。Dart 的函数系统非常灵活,支持多种参数类型、函数式编程特性和灵活的调用方式。
Dart 函数的强大功能
基础能力:多种参数传递方式(必需/可选/命名)
函数式特性:一级公民、闭包、高阶函数
异步处理:async/await 简化异步编程
代码生成:同步/异步生成器函数
类型安全:typedef 明确函数签名
- 上一篇: 人事管理中经常要用到的5个函数公式,总有一个你会用到
- 下一篇: 如何实现动态钟表转动?
猜你喜欢
- 2025-08-01 技术栈:SQL server数据库教程第八集
- 2025-08-01 威纶通触摸屏宏指令基本应用二
- 2025-08-01 如何实现动态钟表转动?
- 2025-08-01 人事管理中经常要用到的5个函数公式,总有一个你会用到
- 2025-08-01 Is系列判断函数都不掌握,那就真的Out了
- 2025-08-01 数据库 SQL 约束之 DEFAULT
- 2025-08-01 支付宝新功能?手把手教你调整花呗还款日
- 2025-08-01 碎片时间学编程「335]:获取指定日期为一年中的第几周
- 2025-08-01 Excel 函数讲解:DATEDIF 函数,精准计算日期间隔难题
- 2025-08-01 Excel 函数讲解:EDATE 函数,轻松搞定日期批量推算!
- 08-01技术栈:SQL server数据库教程第八集
- 08-01威纶通触摸屏宏指令基本应用二
- 08-01如何实现动态钟表转动?
- 08-01Dart编程学习笔记03-函数
- 08-01人事管理中经常要用到的5个函数公式,总有一个你会用到
- 08-01Is系列判断函数都不掌握,那就真的Out了
- 08-01数据库 SQL 约束之 DEFAULT
- 08-01支付宝新功能?手把手教你调整花呗还款日
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)