本周五轮到技术分享,趁着 Google I/O 刚宣布 Kotlin 加入官方支持,正好借这个时机向同事们安利 Kotlin。选了几点内容,发现和这篇官方博客重合度蛮高的,膨胀一波不过分吧兄弟。不过这位老哥也没介绍 Delegation,Dsl,Coroutines …难道和我一样想偷懒
Kotlin
Statically typed programming language for modern multiplatform applications 100% interoperable with Java™ and Android™
Kotlin has been around for quite a while; it was announced back in 2011 and the first preview was released in 2012. Kotlin 1.0 was released in 2016, at which point JetBrains committed to maintaining backwards compatibility for stable features from 1.0 forward.
@Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + (email != null ? email.hashCode() : 0); result = 31 * result + (company != null ? company.hashCode() : 0); return result; }
@Override public String toString() { return"Customer{" + "name='" + name + '\'' + ", email='" + email + '\'' + ", company='" + company + '\'' + '}'; } }
1 2
dataclassCustomer(var name: String, var email: String = "", var company: String = "")
Smart casts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
interfaceCpu
classIntel : Cpu { funintelCool() {} } classAMD : Cpu { funamdBoom() {} }
val cpu: Cpu = AMD()
if (cpu is Intel) { cpu.intelCool() } elseif (cpu is AMD) { cpu.amdBoom() }