1、入门 ,google整理的快速入门,核心内容都点了一下,看完后有个大概认识
2、官方:kotlin学习官网 (详细)
(1)基础
(2)类
3、知识点
---------------------- google入门文档和kotlin官网的笔记 -------------------------------
kotlin 结合java和C++,而且有点偏向于C++
val 用于值从不更改的变量。您不能为使用 val 声明的变量重新赋值。
var 用于值可以更改的变量
基本类型大字 Int、 Byte、Short、Long、Float 和 Double
val languageName = "Kotlin"
languageName 赋予初始值时,Kotlin 编译器可根据所赋值的类型来推断类型。且之后不改变。
在某些语言中,可以声明引用类型变量而不明确提供初始值。
在这些情况下,变量通常包含 null 值。默认情况下,Kotlin 变量不能持有 null 值。这意味着以下代码段无效:
val languageName: String = null
条件语句基本用法 和java 一样
扩展用法,有返回值的if
val answerString: String = if (count == 42) {
"I have the answer."
} else if (count > 35) {
"The answer is close."
} else {
"The answer eludes me."
}
println(answerString)
每个条件分支都隐式地返回其最后一行的表达式的结果
用when语句代替if ,效果一样 ,一旦有符合的条件(true),就不执行后面的分支
“->” 也是隐式地返回其最后一行的表达式的结果
val answerString = when {
count == 42 -> "I have the answer."
count > 35 -> "The answer is close."
else -> "The answer eludes me."
}
println(answerString)
匿名函数
像C++,可以定义指向函数的指针,可以理解为函数的别名,和java8的新特性Lambda相似
说是匿名,但还是有一个变量对其进行引用
并非每个函数都需要一个名称。某些函数由其输入和输出更直接地进行标识。这些函数称为“匿名函数”。您可以保留对某个匿名函数的引用,以便日后使用此引用来调用该匿名函数。与其他引用类型一样,您也可以在应用中传递引用。
val stringLengthFunc: (String) -> Int = { input ->
input.length
}
高阶函数
一个函数可以将另一个函数当作参数。将其他函数用作参数的函数称为“高阶函数”。此模式对组件之间的通信(其方式与在 Java 中使用回调接口相同)很有用。
fun stringMapper(str: String, mapper: (String) -> Int): Int {
// Invoke function
return mapper(str)
}
调用例子
val testFun3:(String,Int)->Int={
str,tempInt->
var myValue=str.length + tempInt
myValue+=2
myValue
}
如果匿名函数是在某个函数上定义的最后一个参数,则您可以在用于调用该函数的圆括号之外传递它,如以下示例所示:
stringMapper("Android") { input ->
input.length
}
类,没有new
属性 默认public
##############################
基本语法
Function returning no meaningful value:
Unit 表示返回void
fun printSum(a: Int, b: Int): Unit {
println("sum of $a and $b is ${a + b}")
}
字符串代替符
var a = 1
// 普通替换
val s1 = "a is $a"
a = 2
// 任意表达式 用${}
val s2 = "${s1.replace("is", "was")}, but now is $a"
判断对象是什么类型,用关键字is ,任意类型用Any
fun getStringLength(obj: Any): Int? {
if (obj is String) {
return obj.length
}
// `obj` is still of type `Any` outside of the type-checked branch
return null
}
条件 循环 详细例子
Control Flow: if, when, for, while
https://kotlinlang.org/docs/reference/control-flow.html#if-expression
in 关键字
when (x) {
in 1..10 -> print("x is in the range")
in validNumbers -> print("x is valid")
!in 10..20 -> print("x is outside the range")
else -> print("none of the above")
}
for (item: Int in ints) {
// ...
}
fun main() {
val array = arrayOf("a", "b", "c")
for (i in array.indices) {
println(array[i])
}
}
while (x > 0) {
x--
}
do {
val y = retrieveData()
} while (y != null) // y is visible here!
这里可以设置break/return的返回点,就好像以前的goto
这种模式以前是不建议使用的
loop@ for (i in 1..100) {
for (j in 1..100) {
if (...) break@loop
}
}
类中的属性默认为public
A class in Kotlin can have a primary constructor and one or more secondary constructors
构造方法Person(var firstName:String) 中变量默认是val ,但可以声明为var
其他构造方法必须继承主构造方法
class Person(val name: String) {
var children: MutableList<Person> = mutableListOf<>()
constructor(name: String, parent: Person) : this(name) {
parent.children.add(this)
}
}
私有化默认无参数构造方法
class DontCreateMe private constructor ()
类中的
每new 一个类,都会执行一次
init {
Log.w("tan","Car init")
}
All classes in Kotlin have a common superclass Any
Any 相当java的object,所有类都实现这个类
By default, Kotlin classes are final: they can’t be inherited. To make a class inheritable, mark it with the open keyword
Kotlin 默认所有是final 不可以继承,通过关键字 open来开放 (类,方法默认都是不可以继承,要open)
open class Base(p: Int)
class Derived(p: Int) : Base(p)
继承 :
以前public ,private等等决定类是否可以继承,属性和方法是否可见、继承。
但kotlin细分了,可见由public、private等等控制,但继承由 open ,final决定
class MyView : View {
constructor(ctx: Context) : super(ctx)
constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs)
}
Kotlin 类中的方法默认是不可以继承的,要open,但一旦open后,如果子类没有关闭,就会默认一直open,直到有一个final关闭
open class Shape {
var myName="123"
open fun draw() { /*...*/ }
fun fill() { /*...*/ }
}
open class Circle() : Shape() {
override fun draw() { myName="xddsds"+myName }
//父类没有open,所以报错 override fun fill() { /*...*/ }
}
class Circle2() : Circle() {
// Kotlin 类中的方法默认是不可以继承的,要open,但一旦open后,如果子类没有关闭,就会默认一直open,直到有一个final关闭
override fun draw() { myName="xddsds"+myName }
}
隐藏(关闭)默认无参数构造方法
class DontCreateMe private constructor ()
主构造方法 ,在继承时就要调用,其他构造方法,在类中调用
主 class Derived(p: Int) : Base(p)
副 class MyView : View {
constructor(ctx: Context) : super(ctx)
constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs)
}
类中重写属性 ,有什么用?重写静态变量?
Overriding properties
open class Shape {
open val vertexCount: Int = 0
}
class Rectangle : Shape() {
override val vertexCount = 4
}
可通过super调用父类的方法、属性
super.draw()
内部类,调用父类的方法 super@Outer (Outer这个是可变的)
class FilledRectangle: Rectangle() {
fun draw() { /* ... */ }
val borderColor: String get() = "black"
inner class Filler {
fun fill() { /* ... */ }
fun drawAndFill() {
super@FilledRectangle.draw() // Calls Rectangle's implementation of draw()
fill()
println("Drawn a filled rectangle with color ${super@FilledRectangle.borderColor}") // Uses Rectangle's implementation of borderColor's get()
}
}
}
注意 “inner”的用法 inner class
Derived class initialization order
父类子类初始化顺序
先初始化父类,再子类,先有父亲才有儿子,和java一样
重写规则(翻译重点,好像有点不准)
如果继承中有多个相同的名字的方法,必须重写这个方法,通过super<Base>调用父类方法
open class Rectangle {
open fun draw() { /* ... */ }
}
interface Polygon {
fun draw() { /* ... */ } // interface members are 'open' by default
}
class Square() : Rectangle(), Polygon {
// The compiler requires draw() to be overridden:
override fun draw() {
super<Rectangle>.draw() // call to Rectangle.draw()
super<Polygon>.draw() // call to Polygon.draw()
}
}
接口成员默认为 open 。interface members are 'open' by default
抽象类,抽象方法 默认为open。
open class Polygon {
open fun draw() {}
}
abstract class Rectangle : Polygon() {
abstract override fun draw()
}
----------------------------------------------------------------
Kotlin从入门到进阶
https://www.jianshu.com/p/f98dcd2da323
Kotlin介绍