kotlin核心编程
Kotlin介绍 (Kotlin Introduction)
Kotlin a modern programming language that makes the developer's life easier and happier. Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference.
Kotlin是一种现代编程语言,使开发人员的生活更轻松,更快乐。 Kotlin是一种具有类型推断功能的跨平台 ,静态类型的通用编程语言。
Kotlin was introduced by the company Jetbrains in the year 2010 and was open source since 2012. The team has decided to name Kotlin language after a Russian island, in the Baltic Sea named Kotlin Island.
Kotlin由Jetbrains公司于2010年推出,自2012年开始开源。团队决定以波罗的海的一个名为Kotlin Island的俄罗斯岛屿来命名Kotlin语言。
Kotlin is growing its popularity day-by-day. Kotlin is a pack of many awesome features that drastically reduce the amount of boilerplate code we write in other languages.
Kotlin的知名度每天都在增长。 Kotlin包含了许多很棒的功能,这些功能极大地减少了我们用其他语言编写的样板代码的数量。
As per the report from Snyk, it has revealed that Kotlin overtakes Scala and Clojure, to become the 2nd most popular language on the JVM. Kotlin is one of the awesome and easier languages to start coding with. Kotlin is good for
根据Snyk的报告,它显示Kotlin超过了Scala和Clojure,成为JVM上第二流行的语言。 Kotlin是一种很棒的语言,可以轻松地开始编码。 Kotlin对
Kotlin is the first preferred language for the development of Android since Google I/O held in 2019. Many top apps already have started using Kotlin in their app development.
自2019年Google I / O以来,Kotlin是Android开发的首选语言。许多顶级应用程序已经开始在其应用程序开发中使用Kotlin。
Particularly if you are working on an Android Development or starter who is seeking to learn Android my suggestion is to start with Kotlin rather than Java.
尤其是如果您正在研究Android的开发人员或初学者,我的建议是从Kotlin而不是Java开始。
“According to Google, over 60% of the top 1000 apps on the Play Store use Kotlin” — Kotlin docs
“据Google称,Play商店前1000个应用中有60%以上使用Kotlin” – Kotlin docs
Kotlin的功能 (Features of Kotlin)
Let’s have a look at some of the awesome features at a basic level in Kotlin
让我们在Kotlin的基础上看一些很棒的功能
NULL SAFETY: Kotlin provides null safety. NullPointer exceptions are quite common in any programming language. But in Kotlin with the help of “?” we can simply avoid NullPointer exceptions in most cases.
空安全: Kotlin提供空安全。 NullPointer异常在任何编程语言中都很常见。 但是在Kotlin的帮助下? 在大多数情况下,我们可以简单地避免NullPointer异常。
var a: String = "Satya"
a = null // Compilation Error Null can not be a value of a non-null type String
var b: String? = "Pavan"
b = null // works
val c: String = null // Null can not be a value of a non-null type String
var d: String? = null // Allowed usage
TYPE INFERENCE: In Kotlin, we do not have to specify the type of a variable explicitly if the value is assigned at the time of declaration
类型推断:在Kotlin中,如果在声明时分配了值,则不必显式指定变量的类型
val name = "Satya"val a = 30
Data Classes: We generally create a model or POJO(Plain Old Java Objects) classes while dealing with business logic. In this class, we generally declare variables and write getter/setter methods to access/modify the values. In java, it would be something like following
数据类:我们通常在处理业务逻辑时创建模型或POJO(普通旧Java对象)类。 在此类中,我们通常声明变量并编写getter / setter方法以访问/修改值。 在Java中,将类似于以下内容
public class Student {
private int id;
private String name;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
But in Kotlin we have a better solution to maintain model or POJO classes. It’s nothing but Data Classes. A Data Class is similar to a regular class but with some additional functionalities. In Kotlin a class can be marked as data class:
但是在Kotlin中,我们有一个更好的解决方案来维护模型或POJO类。 就是数据类 。 数据类类似于常规类,但具有一些附加功能。 在Kotlin中,可以将一个类标记为数据类:
data class Student(val id: Int, val name: String, val email: String)
The compiler automatically derives the following members from all properties declared in the primary constructor:
编译器自动从主要构造函数中声明的所有属性派生以下成员:
equals()
/hashCode()
pairequals()
/hashCode()
对toString()
of the form"Student(id=1,name=Satya, email=pavan@gmail.com)"
toString()
的形式为"Student(id=1,name=Satya, email=pavan@gmail.com)"
componentN()
functions corresponding to the properties in their order of declaration按其声明顺序与属性相对应的
componentN()
函数copy()
function.copy()
函数。
There are many more features like extension functions, functional programming, scope functions, inline functions, default and named arguments, etc explained in below mentioned posts.
还有许多功能,例如扩展功能,功能编程,作用域功能,内联函数,默认和命名参数等,将在下面提到的帖子中进行介绍。
Kotlin指南 (Kotlin Guide)
I thought of sharing my knowledge so that it would help someone out there hence I started writing these series of posts on Kotlin's features. Check out my posts on Kotlin:
我想分享我的知识,以便对外面的人有所帮助,因此我开始撰写有关Kotlin功能的系列文章。 查看我在Kotlin上的帖子:
Kotlin Guide for Beginners — Explains the basics of variable declarations & Why to learn Kotlin
Kotlin初学者指南 -解释变量声明的基础知识以及学习Kotlin的原因
Kotlin Advanced Programming — This post is regarding basics related to functions & types of functions in Kotlin
Kotlin高级编程 -这篇文章是关于Kotlin中与函数和函数类型相关的基础知识的
Kotlin Advanced Programming Part 2 — This post is regarding Functional Programming in Kotlin
Kotlin高级编程第2部分 -这篇文章是关于Kotlin中的函数式编程的
Kotlin Advanced Programming Part 3 — This post is regarding scope functions in Kotlin
Kotlin高级编程第3部分 -这篇文章是关于Kotlin中的作用域函数的
Kotlin Advanced Programming Part 4 — This post is regarding inline functions & reified types in Kotlin
Kotlin高级编程第4部分 —这篇文章是关于Kotlin中的内联函数和修饰类型的
Kotlin Delegates — This post explains about inline functions & reified types in Kotlin
Kotlin代表 —这篇文章解释了Kotlin中的内联函数和修饰类型
Kotlin Sealed Classes — This post is regarding sealed classes which are an extension of enums.
Kotlin密封类 -这篇文章是关于密封类的,它们是枚举的扩展。
摘要 (Summary)
I will keep posting the articles on Kotlin and update the content here. My goal is to share the knowledge that helps others and myself. Always share the knowledge that we have it will benefit us too.
我将继续在Kotlin上发布文章,并在此处更新内容。 我的目标是分享有助于他人和自己的知识。 始终分享我们拥有的知识也将使我们受益。
Kotlin one of the awesome and easier language to start coding with. Comment about the topic you would like to explore in Kotlin. Don’t delay start exploring Kotlin…
Kotlin是一种很棒的语言,可以轻松地开始编码。 评论您想要在Kotlin中探索的主题。 不要延迟开始探索Kotlin…
翻译自: https://medium.com/android-dev-hacks/kolin-advanced-programming-series-8ac51f69f9c9
kotlin核心编程