惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Heimdal Security Blog
S
Security @ Cisco Blogs
N
News | PayPal Newsroom
J
Java Code Geeks
罗磊的独立博客
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic
V
V2EX
WordPress大学
WordPress大学
Google Online Security Blog
Google Online Security Blog
N
News and Events Feed by Topic
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
AI
AI
小众软件
小众软件
The GitHub Blog
The GitHub Blog
MongoDB | Blog
MongoDB | Blog
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
美团技术团队
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tailwind CSS Blog
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
F
Full Disclosure
B
Blog RSS Feed
Forbes - Security
Forbes - Security
S
SegmentFault 最新的问题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
Jina AI
Jina AI
Cisco Talos Blog
Cisco Talos Blog
U
Unit 42
Project Zero
Project Zero
H
Hacker News: Front Page
Y
Y Combinator Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
S
Secure Thoughts
The Hacker News
The Hacker News
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 麦机长

读《左手程序右手诗》 Flutter的强制自我审查 玩转二叉树 N皇后问题 如何减小微信小程序代码包大小 我为什么瞧不上996 如何做好Code Review Dart单例模式最佳实践 面试那点事儿 ThinkPad T43续命记 - 麦机长 - 博客园 从程序员的“劣根性”发散开去 没什么技术含量的Remove Before Flight Padrino 生成器指南 Padrino 博客开发示例 谋职中的战术与战略 以人为鉴,可以明得失——《程序员职场第一课》书评 时间管理系列文章 下载版 时间管理——寻找精力与效率的平衡点 时间管理——专注与放下
mixin很难吗?
麦机长 · 2020-02-17 · via 博客园 - 麦机长

What is mixin?

实践类工作经常会遇到这样的问题,有些概念会用不会说,最近在学习Dart,遇到mixin便是如此。读了几篇网文总觉得良莠不齐,一群码友也说越读越懵,索性静下心来重新出发,一点一滴地弄明白。记录下来分享给大家,咱不说别人文笔不好、也不多举例子以至忘了初心,咱就看能不能以最直观的示例和最直白的讲解达到快速理解mixin的目的。

Mix in: (Phrasal Verb) Mix something in/with something. To add one substance to others, especially in cooking.
-- Oxford Advanced American Dictionary

Mix in本意主要是指烹饪时将一种东西混进另一种东西,比如打个鸡蛋,放点儿盐,一起搅。把这个概念搬到Dart这门单继承的面向对象语言中来又是几个意思呢?

根子就在单继承上,这可以说是现代编程语言的“政治正确”,但它在带来好处的同时也限制了想象空间。废话少说,以码服人:

abstract class DeveloperBase {
  void code();
}

class FrontEndDeveloper extends DeveloperBase {
  void code() => print('我在帮组里小姐姐写代码');
}

mixin Zhuangbility on FrontEndDeveloper {
  @override
  void code() => print('写代码讲究的是情怀');

  void operateDatabase() => print('大不了老子删库跑路');
}

class FullStackDeveloper extends FrontEndDeveloper with Zhuangbility {}

void main() {
  FullStackDeveloper fullStackDeveloper = FullStackDeveloper();
  fullStackDeveloper.code();
  fullStackDeveloper.operateDatabase();
}

首先我们笼统地定义了一个程序员基类DeveloperBase,程序员写程序,天经地义,没啥好说的。然后我们基于DeveloperBase定义了只要有机会就喜欢帮小姐姐干活儿的前端程序员类FrontEndDeveloper

现在,我们实际上就可以用FrontEndDeveloper来干活儿了,但是单就一个前端程序员,刷子毕竟不够,假如我们需要一个能够主持全面工作的全栈程序员,咋整?定义一个全栈程序员类FullStackDeveloper,不管它继承自DeveloperBase还是FrontEndDeveloper,似乎都不够;定义一个后端程序员类BackEndDeveloperFullStackDeveloper来继承,又是鱼和熊掌不可兼得的问题,毕竟单继承机制不给你这个想象空间。

所以,这种时候就得换个思路了。用mixin来声明一种混入,为了避免造成困扰,咱不叫它BackEndDeveloper而命名为高阶能力Zhuangbility,将一组新的技能混入FrontEndDeveloper,然后就可以声明前端程序员出身、带有高阶能力,既能写代码又会玩弄数据库的FullStackDeveloper了。

> dart main.dart ↙
写代码讲究的是情怀
大不了老子删库跑路