传送门
个人认为Kotlin语言非常优雅,与Java相比,增加了很多特性和语法糖,在使用过程中也有了一定的思考,并做了一些简单的记录。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| public class People {
private String name; private int age;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
@Override public String toString() { return "People{" + "name='" + name + '\'' + ", age=" + age + '}'; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; People people = (People) o; return age == people.age && Objects.equals(name, people.name); } }
|
1
| data class People(val name: String, val age: Int)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| public final class Student { @NotNull private final String name; private final int age; public boolean equals(Object paramObject) { if (this != paramObject) { if ((paramObject instanceof Student)) { Student localStudent = (Student)paramObject; if (Intrinsics.areEqual(this.name, localStudent.name)) { if ((this.age == localStudent.age ? 1 : 0) == 0) {} } } } else { return true; } return false; } public int hashCode() { } public String toString() { return "Student(name=" + this.name + ", age=" + this.age + ")"; } @NotNull public final Student copy(@NotNull String name, int age) { Intrinsics.checkParameterIsNotNull(name, "name"); return new Student(name, age); } public final int component2() { return this.age; } @NotNull public final String component1() { return this.name; } public Student(@NotNull String name, int age) { this.name = name;this.age = age; } public final int getAge() { return this.age; } @NotNull public final String getName() { return this.name; } }
|
1 2 3
| val xu = User("Xu", 24) val (name, age) = xu println("$name, $age")
|
1 2 3 4 5
| fun View.getViewLocationArr(): IntArray { val viewLoc = intArrayOf(0, 0) getLocationOnScreen(viewLoc) return viewLoc }
|
1 2 3 4 5
| var viewLoc = view.getViewLocationArr() if (viewLoc[0] <= popupContentView.measuredWidth) { return false } `
|
1 2 3 4 5 6 7 8
| @NotNull public static final int[] getViewLocationArr(@NotNull View $receiver) { Intrinsics.checkParameterIsNotNull($receiver, "$receiver"); int[] viewLoc = { 0, 0 }; $receiver.getLocationOnScreen(viewLoc); return viewLoc; }
|
1 2 3 4 5 6 7 8 9
| animator?.addListener(object : AnimatorListenerAdapter() { override fun onAnimationStart(animation: Animator?) {
}
override fun onAnimationEnd(animation: Animator?) {
} })
|
1 2 3 4 5 6 7
| object InputMethodUtil { fun showInputMethod(view: View?) { val imm: InputMethodManager = view?.context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT) } }
|
官方文档(PS:吐槽一下,Kotlin的中文文档翻译到有点生涩,有些地方不是很理解),后续本人也会发表一些关于Kotlin的文章,更多从知其所以然的角度(例如反编译代码)去认识它,学习它。