【问题标题】:Is there any property ordering convention in Kotlin?Kotlin 中是否有任何属性订购约定?
【发布时间】:2019-03-27 01:49:36
【问题描述】:

对于 Kotlin 中的任何类,我通常都有一堆不同的属性。我想知道根据它们的类型对这些属性进行排序是否有任何转换。我使用的基本类型是

  • var fields
  • private var fields
  • lateinit var fields
  • private lateinit var fields
  • val fields 以上所有内容
  • lazy fields
  • private lazy fields

常见的分类是在同一组中声明相同类型的属性。然后我要做的是先声明公共字段,然后再声明私有字段。我想知道怎么样,先说lazylateinit 怎么样。或者var之后会发生什么? private varlateinit varlazy val?

好吧,我知道人们可以随心所欲地写,但我想知道是否有任何约定,根据其特定的未指定级别列出属性?

【问题讨论】:

    标签: android kotlin conventions coding-style


    【解决方案1】:

    来自 Kotlin 文档 (https://kotlinlang.org/docs/reference/coding-conventions.html):

    Do not sort the method declarations alphabetically or by visibility, and do not separate regular methods from extension methods. Instead, put related stuff together, so that someone reading the class from top to bottom would be able to follow the logic of what's happening. Choose an order (either higher-level stuff first, or vice versa) and stick to it.

    我认为这个规则也可以应用于属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 2013-04-13
      • 2010-12-12
      • 2012-01-20
      相关资源
      最近更新 更多