內(nèi)聯(lián)函數(shù)let with run apply also
//閉包形式返回
//let定義一下作用域趟咆,判空處理
haha.let {
haha.run()
}
//類多個方法或?qū)傩栽遥乐诡惗鄬?护桦,多用于onBinderViewHolder中
with(HaHa("vv",30)){
println("$name$age")
}
//let+with屬性,返回最后一句話
HaHa("vv",30).run {
println("$name$age")
}
//返回this
//let+with屬性侮穿,返回對象自身域醇,用于實(shí)例初始化或 view = View.inflate().apply{初始化}
val haHa = HaHa("vv",30).apply {
println("$name$age")
}
//also 與let一樣,不過返回的是對象本身
val haHa2 = HaHa("vv",30).also {
println("$it.name$it.age")
}
靜態(tài)常量
companion object{
val foo:String = "aa"
const val foo2:String = "bb"
@JvmField
val foo3 = "cc"
}
頂級常量:const val FOO = "foo"
集合數(shù)組常用操作
循環(huán)跳出
類型強(qiáng)轉(zhuǎn)
類基礎(chǔ)
//構(gòu)造函數(shù)
//無(注釋袖扛,可見性修飾符)constructor可省略
class SuperClass @JvmOverloads private constructor(name:String, age:Int = 0){}
class SuperClass1(name:String){
// var j = SuperClass("jj")//JvmOverloads自動生成2個構(gòu)造函數(shù)
}
//定義成員變量拦耐,演變1
class SuperClass2 (name:String,age:Int){
private var name:String
private var age:Int
init {
this.name = name
this.age = age
}
}
//演變2
class SuperClass3 (name:String,age:Int){
private var name:String = name
private var age:Int = age
}
//演變3
open class SuperClass4 (private var name: String, private var age: Int){
constructor(name: String,age: Int,url:String):this(name,age){
}
constructor(name: String,age: Int,url:String,sex:Boolean):this(name,age,url){
}
//默認(rèn)類中所有是final的,想覆寫或繼承必須open
open fun haHa(){
this.name = "你好"http://this關(guān)鍵字用于訪問對象的其他成員
this.age = 90
}
fun hh(){}
}
class ChildClass constructor(private var name: String,private var age: Int,private var url: String) : SuperClass4(name,age,url,true){
override fun haHa() {
super.haHa()
super.hh()
this.url = "http://www.baidu.com"
}
}
//抽象類的abstract修飾符默認(rèn)是open的
abstract class A{
abstract fun jj()
fun ii(i:Int){}
}
class B(private var age:Int) : A(){
override fun jj() {
super.ii(age)
}
}
//接口
interface C{
fun c()
}
interface D{
fun d()
}
class E(food:String):C,D{
override fun c() {}
override fun d() {}
}
//嵌套類溉愁,內(nèi)部類
class Outer{
var name = "sdfa"
var jj = Nested().exe()
class Nested{
fun exe(){
//訪問不了外部類的成員
}
}
}
class Outer2{
var name = "saf"
inner class Inner{
fun exe(){
println(name)
}
}
}
fun uu(){
Outer2().Inner().exe()
}
//枚舉,枚舉類實(shí)例可數(shù)处铛,密封類子類可數(shù)
enum class Week{
星期一,星期二,星期三,星期四,星期五,星期六,星期日
}
enum class Week2(var arg1:String,var arg2:String){
YI("22","ee"),
ER("44","gg")
}
fun dd(){
Week2.YI.arg1 = "555"
}
var jj = Week2.YI.arg1
//密封類構(gòu)造函數(shù)是私有的,因此密封類的子類必須在內(nèi)部或同一文件內(nèi)
sealed class HHH{
object AAA : HHH() {}
class BBB: HHH(){}
}
class CCC:HHH(){
fun hh(){}
}
//數(shù)據(jù)類,必須val或var修飾拐揭,主構(gòu)造至少一個參數(shù)
//此類編譯器可以自動生成equals撤蟆,hashcode等方法
data class Bean(val hhh: HHH){}
//單例
object Singleton{
var name = "單例"
fun hj(){
}
}
fun call(){
Singleton.name = "111"
Singleton.hj()
}
//伴生對象,kotlin中的靜態(tài)變量,類加載時初始化堂污,和類生命周期一樣
class ddd{
companion object 伴生對象名稱{//(也可以不寫名稱)
fun ooo(){}
}
}
fun ttt(){
ddd.伴生對象名稱.ooo()
}
//委托代理模式 AA委托給JJJ
interface AA{
fun aa()
}
class JJJ:AA{
override fun aa() {
}
}
//一
class P:AA by JJJ(){}
class PP(jjj: JJJ):AA by jjj
fun uuu(){
//一
var p:P = P()
p.aa()
//
var jjj =JJJ()
PP(jjj).aa()
}
//屬性委托,child委托給parent,也就是家肯,委托類監(jiān)聽被委托類
class Parent{
var name:Int = 0
//setValue() 和 getValue() 方法前必須用 operator 關(guān)鍵字修飾
//
//getValue()方法返回類型必須與委托屬性相同或是其子類
//
//如果委托屬性是 val 類型,被委托方法只需要實(shí)現(xiàn) getValue()盟猖, 如果是 var 類型讨衣,則需要實(shí)現(xiàn)
//getValue() 和 setValue()
operator fun getValue(child: Child, property: KProperty<*>): Int {
println("getValue")
return name
}
operator fun setValue(child: Child, property: KProperty<*>, i: Int) {
println("setValue")
}
}
class Child{
var name:Int by Parent()
fun ooo(){
println("被委托類的方法")
}
}
fun LLL(){
val child:Child = Child()
child.name=100
}
//延遲加載,變量被訪問時才會初始化,val ** by lazy
//執(zhí)行第一句話
//執(zhí)行中間一句話
//執(zhí)行最后一句//初始化完
//safsdfsf//開始執(zhí)行式镐,只會執(zhí)行最后一句
//safsdfsf
//safsdfsf
fun kkk(){
val name by lazy {
println("執(zhí)行第一句話")
println("執(zhí)行中間一句話")
println("執(zhí)行最后一句")
"safsdfsf"
}
println(name)
println(name)
println(name)
}