Kotlin之对象表达式、声明、类代理
来源:互联网 发布:闪光灯软件哪个好 编辑:程序博客网 时间:2024/06/02 00:31
package net.edaibu.kotlintest.ClassAndExtends/** * @author geqipeng * * @date 2017/5/26 * * @time 18:35 *///对象表达式 和声明//对象表达式open class Test1(x: Int) { public open val x: Int = x}interface Interface1 { fun MethodTest() { println("测试接口") }}//类继承Test1val testMethod = object : Test1(10) { override val x = 10}//对象声明 和java类似object Animals { fun eat() { println("猫吃鱼") } fun play() { println("爬树") }}//伴随对象 companion关键字class CompanionTest { companion object InnerObject { fun MethodCompanion(): CompanionTest = MethodCompanion() }}//伴随对象成员 通过类名做限定词直接使用val companionMember = CompanionTest.MethodCompanion()//使用companion关键字对象名可以省略class CompanionTest2 { companion object { }}//伴随对象可以实现接口interface InterfaceCompanion<T> { fun CompanionTest3()}class CompanionTest4 { companion object : InterfaceCompanion<CompanionTest> { override fun CompanionTest3() { } }}/** * 对象表达式和声明的区别 * 他俩之间只有一个特别重要的区别: *对象表达式在我们使用的地方立即初始化并执行的 *对象声明是懒加载的,是在我们第一次访问时初始化的。 *伴随对象是在对应的类加载时初始化的,和 Java 的静态初始是对应的。 *///类代理 interface mInterface{ fun Method1()}class TestClass(val test:Int):mInterface{ override fun Method1() { println(test) }}//通过对象i代理TestClass中的公共方法class Derived(i:mInterface) :mInterface by ifun main(args: Array<String>) { println(testMethod) //输出对象值:net.edaibu.kotlintest.ClassAndExtends.KotlinTest3Kt$testMethod$1@7ea987ac//无继承类 val testMethod2 = object { val test1: Int = 20 val test2: Int = 30 } println(testMethod2.test1 + testMethod2.test2) //50 println("对象声明————————————————————————————————————————") println(Animals.eat()) //猫吃鱼 println(Animals.play()) //爬树 println("类代理________________________") val x=TestClass(10) println(x) // net.edaibu.kotlintest.ClassAndExtends.TestClass@5cad8086 Derived(x).Method1() // 10}
阅读全文
0 0
- Kotlin之对象表达式、声明、类代理
- Kotlin-对象表达式,对象声明
- Kotlin编程之对象的表达式和声明
- Kotlin学习之-5.11 对象表达式和声明
- Kotlin -- 对象表达式和对象声明
- android Kotlin 对象表达式和对象声明
- Kotlin基础教程-对象表达式和声明
- Kotlin学习之object表达式和声明
- Kotlin语法(十五)-对象表达式和声明
- Kotlin之代理
- Kotlin代理之属性代理
- Kotlin学习(八): 对象表达式和声明(Object Expressions and Declarations)
- Kotlin汇总5-内部类,枚举类,对象,代理
- Kotlin编程之代理模式
- Kotlin从入门到放弃之基础篇(六)——泛型、嵌套类、枚举类、对象表达式
- Kotlin Reference (十三) object表达式和object声明
- Kotlin 类和对象
- Kotlin 类、对象、接口
- 创建一个定时作业进行数据库备份
- Linux下mysql配置慢日志查询,把查询慢的sql记录下来
- 跳台阶
- 用GDB调试程序(二)
- redis批量删除指定的key
- Kotlin之对象表达式、声明、类代理
- Search Insert Position
- JAVA字符串格式化-String.format()的使用
- js判断用户的浏览器设备是移动端还是pc端
- C#泛型List< >集合:创建、与数组转换,记录运行时间、装箱与拆箱、dictionary
- bool类型
- OpenMAX flow
- Android之沉浸模式设置
- ReactNative开发——自定义组件