实例字段

我们使用 @JvmField 注解对Kotlin中的属性字段标注,表示这是一个实例字段(Instance Fields),Kotlin编译器在处理的时候,将不会给这个字段生成getters/setters方法。

  1. class Department {
  2. var id: Long = -1L
  3. var name: String = "Dept"
  4. var isOpen: Boolean = true
  5. var isBig: String = "Y"
  6. @JvmField var NO = 0
  7. }

映射成Java的代码就是:

  1. public final class Department {
  2. private long id = -1L;
  3. @NotNull
  4. private String name = "Dept";
  5. private boolean isOpen = true;
  6. @NotNull
  7. private String isBig = "Y";
  8. @JvmField
  9. public int NO;
  10. public final long getId() {
  11. return this.id;
  12. }
  13. public final void setId(long var1) {
  14. this.id = var1;
  15. }
  16. @NotNull
  17. public final String getName() {
  18. return this.name;
  19. }
  20. public final void setName(@NotNull String var1) {
  21. Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
  22. this.name = var1;
  23. }
  24. public final boolean isOpen() {
  25. return this.isOpen;
  26. }
  27. public final void setOpen(boolean var1) {
  28. this.isOpen = var1;
  29. }
  30. @NotNull
  31. public final String isBig() {
  32. return this.isBig;
  33. }
  34. public final void setBig(@NotNull String var1) {
  35. Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
  36. this.isBig = var1;
  37. }
  38. }

我们在Java中调用的时候,就直接使用这个属性实例字段NO

  1. System.out.println(d.NO = 10);