让你的代码量减少3倍!使用kotlin开发Android(二) –秘笈!扩展函数

本文承接上一篇文章

 

让你的代码量减少3倍!使用kotlin开发Android(一) 创建Kotlin工程

 

上一节说到,kotlin可以省去getter,setter,和findviewbyid,可以看到已经能去除掉不少代码了。本篇再带给大家一篇秘笈,叫做函数扩展。

函数扩展是什么东东捏,一般来说,我们扩展一个类,需要去继承来实现,不过这样又要写N多代码呀~~ 标题说了,kotlin能让你的代码量减少至少3倍!不信?快来跟我看看!

来让我们先把布局的TextView换为一个Button

假设我们实现这样一个效果,在MainActivity里,点击按钮,弹出Toast。
Java代码的话,大家都很轻车熟路了吧~~

是不是一大坨!!!虽然AS能生成代码,可是还是很臃肿啊。什么?你没觉得臃肿?那你看着我用kotlin给他开一刀。

锵锵锵~你没看错。上面一大坨变成了这一句话。这是如何实现的呢。快来跟我一步一步动手简化吧!

去掉findviewbyid

之前说过,直接写id就可以拿到控件拉~~所以findviewbyid这句话省去了。

加入监听器

我们可以直接用lambda的哈,所以呢,可以写成这样

lambda不熟悉的话,快去谷歌一下用法哈~ 接下来呢,如果lambda左值参数没有用到的话,是可以省略的,所以现在写成了

呃。。看起来还是有点多,是不?别急,秘籍来了!

函数扩展

函数扩展可以让你对任意类进行扩展,而不用继承等等复杂的操作。
举个栗子!拿Toast来说,到处都可以用到,我们新建个kt文件,叫做ext
里面写上如下代码:

对!没错,加了这句话,就可以在所有Context类型中使用toast这个函数了。

具体的语法是 fun + 类型.函数(参数)

这断代码可以写在任意可以编译到的文件中,直的注意的是,如果写在了class内部,则是局部生效。

来看一下第二个参数,可以给默认值,如果参数不传,则自动调用默认值,这方便了什么?大家想想,没错!! 就是重载!!! 总之最终简化版点击按钮相应弹toast,代码就是一行!

简直不能cool更多,你们觉得呢?

本文这就结束了(嫌文字太短?不要急啦,先去动手实践下咯。。系列文章,既然挖了坑,就不会拖更的(吧..))
本项目使用的代码地址

 

下一篇

让你的代码量减少3倍!使用kotlin开发Android(三) 缩短五倍的Java Bean


欢迎加入Kotlin小乐园群:479109923
如果你是Android开发者,那么你还可以来 wing的酒馆:425983695来分享你的开发经验哦

让你的代码量减少3倍!使用kotlin开发Android(二) –秘笈!扩展函数》有80个想法

  1. Thank you for some other informative website. Where else may just I am getting that kind of info written in such an ideal means? I’ve a venture that I am just now working on, and I have been on the glance out for such info. adebdedfbfaa

  2. Hi! Someone in my Myspace group shared this website with us so I came to look it over. I’m definitely loving the information. I’m bookmarking and will be tweeting this to my followers! Great blog and terrific design and style.

  3. Thanks for this article. I might also like to say that it can end up being hard if you find yourself in school and merely starting out to create a long credit rating. There are many learners who are only trying to endure and have long or good credit history are often a difficult issue to have.

  4. Hey are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and set up my own. Do you require any coding knowledge to make your own blog? Any help would be really appreciated!

  5. Heya im for the first time here. I discovered this board and I to uncover It truly helpful &amp it helped me out a whole lot. I hope to supply something back and aid other people such as you helped me.

  6. whoah this blog is fantastic i really like studying your articles. Keep up the good paintings! You understand, many individuals are looking around for this info, you could aid them greatly.

  7. I do not even know how I stopped up here, however I assumed this put up was good. I do not recognise who you are however definitely you’re going to a wellknown blogger for those who are not already

  8. Good blog! I really love how it is easy on my eyes and the data are well written. I am wondering how I could be notified whenever a new post has been made. I have subscribed to your feed which must do the trick! Have a nice day!

  9. Spot lets start off perform on this writeup, I seriously consider this astounding internet site needs much more consideration. Ill apt to become once again to find out to read considerably a lot more, thank you that information.

  10. Nice read, I just passed this onto a friend who was doing some research on that. And he just bought me lunch since I found it for him smile So let me rephrase that Thank you for lunch! Whenever you have an efficient government you have a dictatorship. by Harry S Truman.

  11. Hey very nice site!! Man .. Beautiful .. Amazing .. I will bookmark your blog and take the feeds alsoI am happy to find a lot of useful info here in the post, we need work out more techniques in this regard, thanks for sharing. . . . . .

  12. Im genuinely enjoying the style and layout of one’s website. Its a very uncomplicated on the eyes which makes it a great deal much more enjoyable for me to come here and pay a visit to far more typically. Did you hire out a designer to make your theme? Excellent perform!

  13. Thank you for the sensible critique. Me &amp my neighbor were just preparing to do a little research about this. We got a grab a book from our local library but I think I learned more from this post. I am very glad to see such excellent info being shared freely out there.

  14. I think this is one of the most significant information for me. And i’m glad reading your article. But should remark on some general things, The web site style is wonderful, the articles is really excellent D. Good job, cheers

发表评论

电子邮件地址不会被公开。