kotlin的包级别函数
与 Java 不同,Kotlin 允许函数独立存在,而不必依赖于某个类,这类函数我们称之为包级别函数(Package-Level Functions)。
为了兼容 Java,Kotlin 默认会将所有的包级别函数放在一个自动生成的叫ExampleKt的类中, 在 Java 中想要调用包级别函数时,需要通过这个类来调用。
当然,也是可以自定义的,你只需要通过注解@file:JvmName(“Example”)即可将当前文件中的所有包级别函数放到一个自动生成的名为 Example 的类中。
强转与智能转换
在 Kotlin 中,用 is 来判断一个对象是否是某个类的实例,用 as 来做强转。
Kotlin 有一个很好的特性,叫 智能转换(smart cast)。就是当已经确定一个对象的类型后,可以自动识别为这个类的对象,而不用再手动强转。
fun main(args: Array<String>) {var animal: Animal? = xxxif (animal is Dog) {//在这里 animal 被当做 Dog 的对象来处理animal.bark()}}
Kotlin中Unit代表什么
kotlin.Unit为无类型,类似java中的void,可见无返回。
Kotlin如何实现静态函数
Java 的静态方法或者变量只需要加一个 static 即可:
public class Singleton{private static Singleton instance = ...;public static Singleton getInstance(){...return instance;}}
用 Kotlin 直译过来就是:
class KotlinSingleton{
companion object{
private val kotlinSingleton = KotlinSingleton()
@JvmStatic
fun getInstance() = kotlinSingleton
}
}
注意 getInstance 的写法。 JvmStatic 这个注解会将 getInstance 这个方法编译成与 Java 的静态方法一样的签名,如果不加这个注解,Java 当中无法像调用 Java 静态方法那样调用这个方法。
另外,对于静态方法、变量的场景,在 Kotlin 当中建议使用包级函数。
val和var的区别
Kotlin中有两个关键字定义变量,这两个关键字外形看着差别很小就只差了一个字母,但实际差别很大的。
- var是一个可变变量,这是一个可以通过重新分配来更改为另一个值的变量。这种声明变量的方式和Java中声明变量的方式一样。
- val是一个只读变量,这种声明变量的方式相当于java中的final变量。一个val创建的时候必须初始化,因为以后不能被改变。
kotlin const关键字
kotlin lateinit关键字
kotlin 获取class实例
Java 当中:
public class Hello{
...
}
...
Class<?> clazz = Hello.class;
Hello hello = new Hello();
Class<?> clazz2 = hello.getClass();
前面我们展示了两种获得 class 的途径,一种直接用类名,一种通过类实例。刚刚接触 Kotlin 的时候,获取 Java Class 的方法却是容易让人困惑。
class Hello
val clazz = Hello::class.java
val hello = Hello()
val clazz2 = hello.javaClass
同样效果的 Kotlin 代码看上去确实很奇怪,实际上 Hello::class 拿到的是 Kotlin 的 KClass,这个是 Kotlin 的类型,如果想要拿到 Java 的 Class 实例,那么就需要前面的办法了。
定义POJO类
data class BindStartupInfo(val startup:ClassName) {
}
前面为什么加上data关键字?
用于只持有数据的java Pojo类
:: 操作符代表什么?
三目运算符
字符串连接
val name: String = "grass"
val age: Int = 30;
println("i am " + name + age)
kotlin中class和file的区别
在新建kotlin文件的时候,有一个kind选项,其中的file和class有什么区别呢?
- 新建class:文件内部会自动添加class xxxx{}
- 新建file:除了package之外,什么也没有
实际上两者没有任何区别,随便用
**
The only difference is that creating a file creates a file with no classes, and creating a class creates a file with one class. You can then add more classes to the file, or delete classes, or make any other changes - the end result doesn’t depend on how the file was created.
实现单例
object PlusFontChecker2 {
const val TAG = "PlusFontChecker";
}
单例中是无法使用下面的代码实现static final常量的,可以使用const关键字
实现static final 常量
java
class StaticDemoActivity {
public static final String LOAN_TYPE = "loanType";
public static final String LOAN_TITLE = "loanTitle";
}
kotlin
class StaticDemoActivity {
companion object {
val LOAN_TYPE = "loanType"
val LOAN_TITLE = "loanTitle"
}
}
或者
class StaticDemoActivity {
companion object StaticParams{
val LOAN_TYPE = "loanType"
val LOAN_TITLE = "loanTitle"
}
}
或者
class StaticDemoActivity {
companion object {
const val LOAN_TYPE = "loanType"
const val LOAN_TITLE = "loanTitle"
}
}
注:const 关键字用来修饰常量,且只能修饰 val,不能修饰var, companion object 的名字可以省略,可以使用 Companion来指代
静态方法
Java代码:
class StaticDemoActivity {
public static void test(){
、、、
}
}
Kotlin中:
class StaticDemoActivity {
companion object {
fun test(){
、、、
}
}
}
或者
class StaticDemoActivity {
companion object StaticParams{
fun test() {
、、、
}
}
}
引用静态方法(这里的引用只针对于java引用kotlin代码)
TestEntity类引用StaticDemoActivity中的静态方法
class TestEntity {
public TestEntity () {
StaticDemoActivity.Companion.test();
}
}
或者
class TestEntity {
public TestEntity () {
StaticDemoActivity.StaticParams.test();
}
}
companion object {}中用来修饰静态常量,或者静态方法,单例等等作者:会撒娇的犀犀利 链接:https://www.jianshu.com/p/e8752c880088 来源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
为什么KClass不是KType的子类
KType is more than just a class. In Kotlin, a type can be nullable or not, it can have generic type arguments, and it can be annotated with type annotations. Additionally, a type’s classifier (what the type is “based” on) can be not only a particular class, but a type parameter of some declaration. For example, here:
fun <K : Any, V : Any> get(key: K): V?
K is a type, and V? is a type, and their classifiers are the corresponding type parameters, but the first type is marked as non-null and the second type is nullable.
Generally, KType is something you discover in a function signature and that denotes the set of values that function can take or return. KClass is a concrete class, with a declaration and source somewhere and a fixed fully qualified name, and you can get the (single) class of every instance at runtime.
For your problem, it might be helpful to think what value do the type’s constituents have: should the behavior differ if the type in the function signature is nullable or not? Should generic type arguments be taken into account? Do you have to handle the type annotations in any way? If all answers are no and you also don’t care about types whose classifiers are type parameters, not classes (example above), then you can safely call type.jvmErasure, or type.classifier as KClass<*> to get the KClassinstance.
