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

 

回顾一下

哈,没想到你已经坚持不懈看到第三篇了,不错哈~坚持就是胜利。

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

我们介绍了扩展函数,这里对上一篇进行一点小小的补充。

还记得textview是如何setText()的吗

不知道你有没有想过,这个.text是如何实现的呢。 哈哈,其实你们大概已经猜到了。就是通过扩展函数实现的

开始今天的内容

今天介绍的是一种叫做数据类的类,他没有什么功能,只负责储存数据,也就是我们常说的Java Bean,通常,在Java语言里,我们定义一个实体,是这样的

可以看到,仅仅两个属性,加上getter setter有这么多代码。那么kotlin是什么样子呢?

你不要说我了个擦,这确实是全部代码。。没有少东西了。可以看到语法,括号里跟着参数,也是一种构造器。并且不用getter setter。

使用的时候正常使用就可以了。

这里提到一点,var 和val的区别。var一般是变量,val一般是常亮,即不可变量,也多用于只是用一次的变量。

这里你可能有疑问了,如果构造器是单参或者空参的呢? 语法如下:

举个栗子

接下来以一个点击按钮,传递数据类向下一个Activity为栗子,把之前的知识应用一下。

首先,新建一个SecondActivity,提供一个静态方法,用于Activity的转跳。想必大家都知道这样的好处,就是让调用者不必看源码就知道需要什么参数。如果你按照java写,就会发现没有static这个关键字!不要慌,这里可以使用 伴生对象 来实现。伴生对象是伴随这个类声明周期的对象~下面看详细用法。

在第一个Activity中,给按钮设置监听器,同时转跳到第二个Activity并且传递User对象。

大功告成了!接下来运行下apk,看看第二个Activity成功接收到了传递的User(对了别忘了把User序列化).

对比一下,嗯哼?比Java少了多少? 有没有很爽啊哈。

 

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

本项目使用的代码地址
欢迎加入Kotlin小乐园群:479109923

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

  1. 因为是弱引用,如果要获取对象的属性值,还要强转一下,var user = user as User
    ==||,没有看到这里,自己一直试,发现怎么都点不出来,囧~~~

  2. 非常赞,从一看到现在感觉浑身都有劲了。就是某些地方有些不好理解,demo中有两个问题点归纳如下:
    1、Entity要实现Serializable接口
    class User : Serializable {}
    2、SecondActivity->onCreate方法代码错误
    intent.getStringExtra(USER) -> val user = intent.getSerializableExtra(USER) as User
    toast(…) -> toast(user.id + ” : ” + user.name)
    接着看,想要马上用到下个项目,Come on!

  3. I just want to tell you that I am just newbie to blogging and actually enjoyed this page. More than likely I’m planning to bookmark your blog post . You surely come with fabulous article content. Thanks a bunch for revealing your website page.

  4. I have to get across my affection for your kindness supporting women who absolutely need help on in this subject. Your special commitment to getting the solution up and down appeared to be amazingly valuable and have usually encouraged guys and women just like me to arrive at their objectives. Your new valuable hints and tips means a great deal to me and much more to my peers. Warm regards; from everyone of us.

  5. I actually wanted to write down a brief comment in order to express gratitude to you for all the stunning tips and tricks you are showing on this website. My time consuming internet search has at the end of the day been paid with reputable tips to exchange with my family members. I would admit that many of us readers actually are undoubtedly endowed to dwell in a remarkable network with so many lovely professionals with very beneficial tips. I feel very much happy to have discovered your website and look forward to really more awesome minutes reading here. Thank you once more for everything.

  6. I precisely wished to say thanks again. I do not know the things I could possibly have handled without those information contributed by you relating to such subject. This has been a real fearsome circumstance in my position, however , viewing the very expert strategy you resolved it took me to weep with contentment. I am thankful for your assistance and in addition hope that you find out what a powerful job you’re doing educating the mediocre ones via your webblog. I am certain you haven’t encountered any of us.

  7. I wish to convey my affection for your generosity giving support to all those that should have help with the subject matter. Your real commitment to passing the message all through appeared to be especially effective and has always made men and women like me to reach their dreams. Your personal interesting information denotes a whole lot a person like me and substantially more to my mates. Thank you; from each one of us.

  8. A powerful share, I simply given this onto a colleague who was doing just a little analysis on this. And he in reality purchased me breakfast as a result of I discovered it for him.. smile. So let me reword that: Thnx for the deal with! However yeah Thnkx for spending the time to debate this, I really feel strongly about it and love reading more on this topic. If potential, as you change into experience, would you thoughts updating your weblog with more details? It is extremely helpful for me. Large thumb up for this weblog publish!

  9. I’m commenting to make you understand of the impressive discovery my child enjoyed using yuor web blog. She learned such a lot of details, most notably what it’s like to have an amazing giving nature to have others without difficulty learn about a number of grueling matters. You truly did more than her desires. Thanks for rendering the interesting, dependable, informative and also easy thoughts on that topic to Tanya.

  10. My wife and i were absolutely fulfilled Edward could deal with his researching from your ideas he had through the web page. It’s not at all simplistic to just choose to be handing out tips which often men and women might have been selling. We really fully grasp we have got the writer to give thanks to because of that. The type of illustrations you have made, the straightforward site navigation, the relationships you will make it possible to engender – it is all fantastic, and it’s leading our son and us reason why that theme is excellent, and that is seriously fundamental. Thank you for the whole lot!

  11. I precisely needed to thank you very much again. I do not know what I would have worked on without the type of tricks provided by you directly on such a situation. It had become a frightful dilemma in my view, but spending time with your specialised style you dealt with the issue made me to jump for fulfillment. I’m happier for this support and even hope that you know what a great job that you are doing instructing the rest with the aid of your web page. Probably you haven’t got to know all of us.

  12. I wish to voice my gratitude for your generosity supporting people who actually need guidance on this one matter. Your real commitment to passing the message up and down came to be quite valuable and has frequently made professionals like me to get to their aims. Your amazing invaluable guidelines implies a great deal a person like me and further more to my office colleagues. Best wishes; from everyone of us.

  13. Hello there! Quick question that’s totally off topic. Do you know how to make your site mobile friendly? My web site looks weird when browsing from my iphone4. I’m trying to find a theme or plugin that might be able to fix this problem. If you have any suggestions, please share. Cheers!

  14. I precisely needed to thank you so much again. I am not sure the things I might have implemented without the strategies revealed by you regarding that concern. It absolutely was a real depressing setting in my opinion, but viewing the skilled way you dealt with the issue forced me to jump over joy. Extremely thankful for the advice and even expect you know what an amazing job you have been accomplishing training most people through the use of your web site. I am sure you have never met any of us.

  15. I merely want to tell you that I am new to writing and clearly loved your write-up. Likely I am likely to bookmark your blog post . You indeed have excellent article content. Truly Appreciate it for expressing with us your main domain webpage

  16. My wife and i got quite contented Edward could complete his survey through the entire ideas he gained in your web page. It’s not at all simplistic to simply continually be handing out guidelines which usually many people have been trying to sell. We recognize we have the writer to appreciate for that. All of the illustrations you’ve made, the easy web site menu, the relationships you can help to engender – it’s many astonishing, and it’s helping our son and us know that this subject is fun, and that’s quite fundamental. Thank you for everything!

  17. I want to voice my appreciation for your generosity giving support to those people who should have assistance with this subject. Your personal commitment to passing the solution all over has been surprisingly functional and have always permitted professionals like me to reach their dreams. Your own interesting tutorial means much a person like me and further more to my colleagues. Thanks a ton; from each one of us.

  18. On Pixabay you will find both, terrific photos and cliparts. I would imagine several of these images are copyrighted and even though I probably bought some type of license once I bought the digital content I am not going to be sharing those files. It’s possible to even download whole size images without registration.

  19. My husband and i got very contented Louis managed to finish up his researching via the ideas he discovered through your site. It is now and again perplexing just to be giving out instructions which often some others could have been making money from. Therefore we grasp we now have the website owner to thank for this. All the illustrations you have made, the simple website navigation, the friendships you will help to foster – it is all incredible, and it’s really aiding our son and us reason why this matter is awesome, which is certainly very fundamental. Thank you for the whole lot!

  20. I wish to show my admiration for your kind-heartedness giving support to persons who really need help with this important question. Your personal commitment to getting the message up and down has been quite interesting and have surely encouraged people just like me to arrive at their targets. Your warm and friendly key points implies this much to me and especially to my peers. Best wishes; from all of us.

  21. I really need to advise you that I am new to posting and pretty much loved your site. More than likely I am going to save your blog post . You truly have superb article content. Be Grateful For it for giving out with us your domain article

  22. Definitely believe that which you stated. Your favorite justification seemed to be on the web the easiest thing to be aware of. I say to you, I certainly get irked while people consider worries that they just do not know about. You managed to hit the nail upon the top and also defined out the whole thing without having side-effects , people could take a signal. Will likely be back to get more. Thanks

  23. I am not sure where you are getting your info, but great topic. I needs to spend some time learning more or understanding more. Thanks for great information I was looking for this information for my mission.

  24. fantastic post, very informative. I ponder why the other specialists of this sector don’t notice this. You must continue your writing. I’m confident, you’ve a huge readers’ base already!

  25. fantastic post, very informative. I’m wondering why the opposite specialists of this sector don’t understand this. You must continue your writing. I’m confident, you have a huge readers’ base already!

  26. I enjoy you because of your entire efforts on this website. My niece enjoys engaging in internet research and it is easy to see why. My partner and i know all relating to the compelling way you make sensible ideas through this blog and therefore attract participation from other people about this concept while our own daughter is always becoming educated so much. Enjoy the rest of the new year. You’re doing a tremendous job.

  27. I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored subject matter stylish. nonetheless, you command get bought an shakiness over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly a lot often inside case you shield this increase.

  28. I must express my thanks to you just for bailing me out of such a problem. Because of researching through the internet and getting methods which were not beneficial, I figured my entire life was over. Existing without the solutions to the problems you have sorted out as a result of your main review is a critical case, and the kind that would have badly affected my entire career if I hadn’t come across your blog. Your skills and kindness in taking care of the whole thing was invaluable. I don’t know what I would have done if I hadn’t encountered such a subject like this. It’s possible to now relish my future. Thanks for your time very much for this skilled and amazing guide. I won’t be reluctant to endorse your web blog to any individual who needs to have counselling about this subject matter.

  29. A lot of thanks for each of your hard work on this blog. Debby really likes doing research and it’s easy to see why. A lot of people notice all concerning the lively means you give efficient tips via your web blog and even recommend contribution from some others about this idea then our own girl is undoubtedly learning a whole lot. Take advantage of the remaining portion of the new year. You’re the one carrying out a remarkable job.

  30. I really have to reveal to you that I am new to writing and absolutely liked your page. Very likely I am probably to store your blog post . You seriously have magnificent article content. Appreciate it for telling with us your favorite website document

  31. Thanks so much for giving everyone an extraordinarily wonderful possiblity to read in detail from here. It’s always so pleasant and also stuffed with a great time for me personally and my office friends to search your web site more than 3 times per week to study the newest stuff you have got. And definitely, I’m just certainly satisfied for the brilliant knowledge you give. Some two areas in this post are undeniably the most beneficial we’ve ever had.

  32. Needed to draft you one very small remark to help thank you very much yet again for all the wonderful thoughts you have shown in this case. It was simply generous with people like you to offer unhampered what exactly a number of people could possibly have supplied for an e book to generate some cash for their own end, and in particular seeing that you might have done it if you ever wanted. Those creative ideas additionally worked as the fantastic way to understand that someone else have a similar fervor similar to mine to find out a great deal more when considering this matter. I think there are several more pleasant times up front for folks who browse through your blog post.

  33. I don’t even know how I ended up here, but I thought this post was good. I do not know who you are but definitely you’re going to a famous blogger if you aren’t already 😉 Cheers!

  34. I simply hope to show you that I am new to blog posting and incredibly valued your post. More than likely I am inclined to remember your blog post . You truly have lovely article blog posts. Get Pleasure From it for swapping with us your website webpage

  35. hey there and thank you for your information – I have certainly picked up something new from right here. I did however expertise some technical issues using this website, since I experienced to reload the site many times previous to I could get it to load properly. I had been wondering if your hosting is OK? Not that I’m complaining, but sluggish loading instances times will very frequently affect your placement in google and can damage your high quality score if ads and marketing with Adwords. Well I’m adding this RSS to my e-mail and can look out for a lot more of your respective interesting content. Ensure that you update this again soon..

  36. I wish to convey my appreciation for your kindness in support of visitors who must have assistance with that study. Your real dedication to getting the message across appears to be certainly insightful and has constantly empowered ladies much like me to achieve their aims. Your new warm and helpful recommendations signifies so much to me and far more to my fellow workers. Thank you; from each one of us.

  37. I in addition to my friends appeared to be looking through the excellent guidelines from the website while before long I had a terrible suspicion I had not expressed respect to the web site owner for those strategies. Most of the people were definitely consequently passionate to read through them and now have surely been enjoying these things. Appreciate your truly being quite thoughtful and then for deciding on certain great areas most people are really desirous to understand about. Our sincere regret for not expressing gratitude to you earlier.

  38. Thanks for the sensible critique. Me & my neighbor were just preparing to do some 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 fantastic information being shared freely out there.

  39. Hey very nice website!! Man .. Excellent .. Superb .. I will bookmark your website and take the feeds also¡KI am glad to seek out so many useful information right here in the post, we’d like develop extra strategies on this regard, thanks for sharing. . . . . .

  40. I’m commenting to make you know of the exceptional discovery our daughter gained reading through your blog. She came to find plenty of things, including how it is like to possess an ideal teaching mindset to get the mediocre ones effortlessly thoroughly grasp several extremely tough subject areas. You undoubtedly exceeded my expectations. Thanks for giving those effective, healthy, edifying not to mention easy guidance on that topic to Janet.

  41. Very nice article and right to the point. I don’t know if this is truly the best place to ask but do you guys have any thoughts on where to hire some professional writers? Thx 🙂

发表评论

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