Kotlin学习记录
- 1. 权限修饰符
- 1.1 internal 修饰符
- 1.2 open 修饰符
- 2 其它修饰符
- 2.1 lateinit 修饰符
- 2.2 inline 修饰符
- 2.3 var 修饰符 (变量)
- 2.4 val 修饰符 (常量)
- 2.5 typealias
- 3 申明对象
- 3.1 申明对象不赋值
- 3.2 申明对象并实例化
- 3.3 申明常量并用 by lazy 懒加载
- 4 类、接口、object、fun、const
- 4.1 class
- 4.2 interface
- 4.3 object
- 4.4 fun
- 4.5 const
- 5 suspend挂起函数,必须在Coroutine协程中使用
1. 权限修饰符
除了java的 public
, protected
, default
, private
外,又有独特的 internal
, open
修饰符。
1.1 internal 修饰符
直译为内部的,访问范围是当前模块可见。使用示例:
// 可以对类修饰
internal class MapboxNavigationAppDelegate { }// 可以对常量修饰,val 为申明常量,var 为申明变量
internal val carAppLifecycleObserver = object : DefaultLifecycleObserver { }// 可以对构造方法和变量修饰
class EtcGateApi internal constructor(internal var experimental: com.mapbox.navigator.Experimental
) { }
1.2 open 修饰符
默认情况下 class 和成员都是具备 final 修饰符的,即无法被继承和复写。
如果可以被继承或复写,需要添加 open 修饰。使用示例:
open class AutoArrivalController : ArrivalController { }open fun nextChain(chain: AudioTypeResolver) { }
更多kotlin权限修饰符介绍参考
2 其它修饰符
2.1 lateinit 修饰符
- 延迟初始化,但又相当于有java中的
@NotNull
注解,所以在使用之前必须已经实例化,否则会报错。 - lateinit 不能用来修饰基本数据类型,因为基本类型的属性在类加载后的准备阶段都会被初始化为默认值。
- lateinit不能修饰val常量,只能修饰可变的属性。
2.2 inline 修饰符
inline——内联函数修饰符
inline fun logI(category: String? = null, lazyMsg: () -> String) {if (logLevel().accepts(LoggingLevel.INFO)) {logI(lazyMsg(), category)}
}
2.3 var 修饰符 (变量)
2.4 val 修饰符 (常量)
2.5 typealias
它的作用十分简单,给已有类型取一个别名,可以像使用原类型一样使用这个 “类型别名” 。
private typealias SDKRoadGraphVersionInfo = com.mapbox.navigation.core.RoadGraphVersionInfo
3 申明对象
3.1 申明对象不赋值
private lateinit var navigationCamera: NavigationCamera
3.2 申明对象并实例化
// 基本类型
private var clearingPlayedEvents = false// Class对象
private var historyTimeOffset: Double = 0.0
private val navigationLocationProvider = NavigationLocationProvider()
private var currentJob: Job? = null
3.3 申明常量并用 by lazy 懒加载
类似 lateinit 修饰符
,但这个懒加载用来申明常量,而 lateinit 修饰符
只能用来申明变量。
private val overviewPadding: EdgeInsets by lazy {EdgeInsets(140.0 * pixelDensity,40.0 * pixelDensity,120.0 * pixelDensity,40.0 * pixelDensity)
}
4 类、接口、object、fun、const
这五种都可以单独定义在一个.kt文件中,也可以多种类型组合。
4.1 class
// 实现了LoggerFrontend接口的 MapboxCommonLoggerFrontend 类
internal class MapboxCommonLoggerFrontend : LoggerFrontend { }// 继承某个类需要后面加(),还有实现的其它接口用逗号隔开添加
class MapboxNavigationActivity : AppCompatActivity(), OnClickListener { }// 带构造参数的类定义,有些参数是常量,被传入后不再能修改,有些参数被直接定义,并且是用其它的参数来构造
class NavigationCamera(mapboxMap: MapboxMap,private val cameraPlugin: CameraAnimationsPlugin,private val viewportDataSource: ViewportDataSource,private val stateTransition: NavigationCameraStateTransition =MapboxNavigationCameraStateTransition(mapboxMap, cameraPlugin)
) { }// 带构造参数的类定义,用 constructor 的方式
class RoadGraphVersionInfo internal constructor(val dataset: String, val version: String) { }// constructor 定义其它的构造函数
class MapboxNavigation @VisibleForTesting internal constructor(val navigationOptions: NavigationOptions,private val threadController: ThreadController,
) {constructor(navigationOptions: NavigationOptions) : this(navigationOptions, ThreadController())}
4.2 interface
将接口作为一个常量实现,如果接口只有一个方法,可以 = 接口名{ 实现方法 }
;如果有多个方法要 = object : 接口名{ 实现方法 }
。
interface Time { }
4.3 object
object InternalJobControlFactory { // 内部可以定义fun和constfun createDefaultScopeJobControl(): JobControl {val parentJob = SupervisorJob()return JobControl(parentJob, CoroutineScope(parentJob + Dispatchers.Default))}
}
4.4 fun
package com.mapbox.navigation.utils.internalimport android.location.Location
import com.mapbox.geojson.Pointfun Location.toPoint(): Point {return Point.fromLngLat(this.longitude, this.latitude)
}
4.5 const
package com.mapbox.navigation.utils.internalconst val NOTIFICATION_CHANNEL = "Navigation Notifications"
const val NAVIGATION_NOTIFICATION_CHANNEL = "NAVIGATION_NOTIFICATION_CHANNEL"
const val END_NAVIGATION_ACTION = "com.mapbox.intent.action.END_NAVIGATION"
const val SET_BACKGROUND_COLOR = "setBackgroundColor"
const val NOTIFICATION_ID = 7654
5 suspend挂起函数,必须在Coroutine协程中使用
private suspend fun simulateEvents(replayEventsCallback: (List<ReplayEventBase>) -> Unit) {val loopStart = timeSeconds()val replayEvents = movePivot(loopStart)if (replayEvents.isNotEmpty()) {replayEventsCallback(replayEvents)}val loopElapsedSeconds = timeSeconds() - loopStartval loopElapsedMillis = (loopElapsedSeconds * MILLIS_PER_SECOND).roundToLong()val delayMillis = max(0L, REPLAY_UPDATE_SPEED_MILLIS - loopElapsedMillis)delay(delayMillis)
}fun launchSimulator(replayEventsCallback: (List<ReplayEventBase>) -> Unit): Job {resetSimulatorClock()return jobControl.scope.launch { // 协程中使用while (isActive) {if (isDonePlayingEvents()) {delay(IS_DONE_PLAYING_EVENTS_DELAY_MILLIS)} else {simulateEvents(replayEventsCallback) // 上面的挂起函数调用}}}.also {currentJob = it}
}// InternalJobControlFactory是object,提供了main、default、io线程的协程JobControl获取
private val jobControl = InternalJobControlFactory.createMainScopeJobControl()