typedef定义如下:

  typedef 给某一种特定的函数类型起了一个名字,可以认为是一个类型的别名。或者这样理解:

    自己定义了一种数据类型,不过这种数据类型是函数类型,按照这种类型实例化后的对象,就会具备类型检查;

  栗子举起,先来一个没有typedef定义:

class SortedCollection {
Function compare; SortedCollection(int f(Object a, Object b)) {
compare = f;
}
} int sort(Object a, Object b) => 0; void main() {
SortedCollection coll = new SortedCollection(sort);
assert(coll.compare is Function)
}

  可以看到coll.compare已经丢失了函数的具体信息;

  再来一个通过typedef保存别的栗子:

typedef int Compare(Object a, Object b);

class SortedCollection {
Compare compare; SortedCollection(this.compare);
} int sort(Object a, Object b) => 0; void main() {
SortedCollection coll = new SortedCollection(sort);
assert(coll.compare is Function);
assert(coll.compare is Compare);
}

  可以看到,我们通过typedef,定义了Compare类型,即使在int sort 传入后,compare仍旧保持Compare的类型;

  基本上typedef就是这样,最后说个高级一丢的玩法,泛型函数别名:

typedef int Compare<T>(T a, T b); 

int sort(int a, int b) => a - b;

void main() {
assert(sort is Compare<int>);
}

最新文章

  1. Python:认识模块
  2. 卷积神经网络(CNN)学习算法之----基于LeNet网络的中文验证码识别
  3. mysql字符乱码
  4. Java dynamical proxy demo
  5. javascript工具--控制台详解(转自 阮一峰博客)
  6. solr官方文档翻译系列之schema.xml配置介绍
  7. android4.4组件分析--service组件
  8. iOS基础 - 类扩展
  9. 远程线程注入方法CreateRemoteThread
  10. 百度OCR文字识别-身份证识别
  11. 滑稽的下午--angularjs 2.0管道的使用
  12. React的组件模式
  13. Android 常驻广播和非常驻广播
  14. 还原是不可能还原的,这辈子都不可能还原(手动笑cry)
  15. iOS UI进阶-4.0 地图与定位
  16. SQLdeveloper换成windows主题后不显示的情况
  17. 树莓派无法挂载exfat格式硬盘
  18. Shell学习---Shell脚本的静态检查工具shellcheck
  19. win8 中如何删除 共享文件夹 用户名和密码
  20. UART,SPI,IIC的一点理解

热门文章

  1. Flask理论基础(一)视图函数和URL反转函数(url_for)
  2. Ubuntu 奇怪踩坑记录
  3. 加载ubuntu的时候卡在‘SMBus Host Controller not enabled&#39;错误
  4. 读书笔记---《Docker 技术入门与实践》---其一
  5. windows server 常用功能(一)
  6. Vuejs input 和 textarea 元素中使用 v-model 实现双向数据绑定
  7. List&lt;Map&lt;String,Object&gt;&gt; 中文排序
  8. javascript追加节点
  9. 最大字段和--GSS1 MUSHROOM ORZ
  10. bzoj1024题解