
Kotlin 自带的Builder建造者模式
���������������������������������������������������
发布日期:2021-05-10 09:30:24
浏览次数:16
分类:精选文章
本文共 2150 字,大约阅读时间需要 7 分钟。
Kotlin Builder������������
Kotlin������Builder���������������������Java������������������������������������������������������������
Method 1: ���������������������
class Person { var name = "" var age = 0 var married = false override fun toString(): String { return "Person(name='$name', age=$age, married=$married)" }}
- ������������������������������:
- ������������������:
- ������������������������������������:
- ������������:
val person = Person().apply { name = "zhk" age = 18 married = false}Log.i("Test", person.toString())
I/Test: Person(name='zhk', age=18, married=false)
Method 2: ��� ��lindi��res���������
class MyDialog { private var tvTitle: TextView? = null private var tvContent: TextView? = null init { val layoutInflater = LayoutInflater.from(Global.getContext()) val rootView = layoutInflater.inflate(R.layout.dialog_avatar, null) tvTitle = rootView.findViewById(R.id.tv_title) tvContent = rootView.findViewById(R.id.tv_content) } fun title(@StringRes res: Int? = null, text: CharSequence? = null): MyDialog { if (res != null) { this.tvTitle?.text = Global.getContext().getString(res) } else if (text != null) { this.tvTitle?.text = text } return this } fun content(@StringRes res: Int? = null, text: CharSequence? = null): MyDialog { if (res != null) { this.tvContent?.text = Global.getContext().getString(res) } else if (text != null) { this.tvContent?.text = text } return this } inline fun show(func: MyDialog.() -> Unit) { this.func() this.show() } fun show() { // ... }}
MyDialog().show { title(res = R.string.login_title) content(text = "������XXXX")}
Method 3:DSL������
��������������� nltki-craftsmen���������������Kotlin������������������������������������
������
���������������������������Kotlin Builder������������������������������������������������������������������������������������������������������
发表评论
最新留言
关注你微信了!
[***.104.42.241]2025年04月23日 23时22分23秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
3分钟搞懂js的冒泡和捕获?
2021-05-10
Mac电脑调用自带的命令行窗口
2021-05-10
终端查看本机ip地址
2021-05-10
vue前端导出多级表头的excel表
2021-05-10
初学微信小程序开发
2021-05-10
通过Python openpyxl库对excel进行操作
2021-05-10
eclipse自动补全代码(Auto activation只能输3个字符)
2021-05-10
svn commit failed:is scheduled for addition
2021-05-10
初学java:斐波那契数列的java实现
2021-05-10
Java多线程
2021-05-10
Unity监听日记
2021-05-10
AndroidStudio跳到错误位置
2021-05-10
unity 四元数和欧拉角相互转换
2021-05-10
ARFoundation系列讲解-03申请苹果开发者账号
2021-05-10
直接点不玩虚的--启明云端千元现金红包、50pcs开发板直播现场拿走不谢
2021-05-10
木马开发的基本理论基础(五)
2021-05-10
openssl服务器证书操作
2021-05-10
expect 模拟交互 ftp 上传文件到指定目录下
2021-05-10
Matplotlib绘制分组聚合流程图
2021-05-10