Skip to content

Commit

Permalink
获得class的实例Java、Kotlin对比实现
Browse files Browse the repository at this point in the history
  • Loading branch information
BetterZhang committed Jun 29, 2017
1 parent c701a74 commit 1ade651
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/com/betterzhang/learnkotlin/java/ClassZZ.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.betterzhang.learnkotlin.java;

/**
* Created by IntelliJ IDEA.
* Author : Andrew Zhang
* Email : betterzhang.dev@gmail.com
* Time : 2017/06/29 下午 4:57
* Desc : 获得class的实例
*/
public class ClassZZ {

public static void main(String[] args) {
Class<?> clazz = ClassZZ.class;
System.out.println(clazz);

ClassZZ c = new ClassZZ();
Class<?> clazz2 = c.getClass();
System.out.println(clazz2);
}

}
25 changes: 25 additions & 0 deletions src/com/betterzhang/learnkotlin/kotlin/ClassZZ.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.betterzhang.learnkotlin.kotlin

/**
* Created by IntelliJ IDEA.
* Author : Andrew Zhang
* Email : betterzhang.dev@gmail.com
* Time : 2017/06/29 下午 5:03
* Desc : 获得class的实例
*/
class ClassZZ {

companion object {
@JvmStatic
fun main(args: Array<String>) {
// ClassZZ::class拿到的是Kotlin的KClass,再转换成Java的class实例
val clazz = ClassZZ::class.java
println(clazz)

val c = ClassZZ()
val clazz2 = c.javaClass
println(clazz2)
}
}

}

0 comments on commit 1ade651

Please sign in to comment.