一起来学Kotlin:概念:4. Kotlin 函数注解:Suppress,Volatile, Synchronized, Bindable, RequiresApi,SerializedName
这篇博客我们解释 Kotlin 函数注解:Suppress,Volatile, Synchronized, Bindable, RequiresApi,SerializedName 等。
文章目录
1. Deprecated
如果需要废弃一个方法,只需要在方法钱加上 @Deprecated 即可。
Kotlin 对于 @Deprecated 的定义:
/**
* Marks the annotated declaration as deprecated.
*
* A deprecated API element is not recommended to use, typically because it's being phased out or a better alternative exists.
*
* To help removing deprecated API gradually, the property [level] could be used.
* Usually a gradual phase-out goes through the "warning", then "error", then "hidden" or "removed" stages:
* - First and by default, [DeprecationLevel.WARNING] is used to notify API consumers, but not to break their compilation or runtime usages.
* - Then, some time later the deprecation level is raised to [DeprecationLevel.ERROR], so that no new Kotlin code can be compiled
* using the deprecated API.
* - Finally, the API is either removed entirely, or hidden ([DeprecationLevel.HIDDEN]) from code,
* so its usages look like unresolved references, while the API remains in the compiled code
* preserving binary compatibility with previously compiled code.
*
* @property message The message explaining the deprecation and recommending an alternative API to use.
* @property replaceWith If present, specifies a code fragment which should be used as a replacement for
* the deprecated API usage.
* @property level Specifies how the deprecated element usages are reported in code.
* See the [DeprecationLevel] enum for the possible values.
*/
@Target(CLASS, FUNCTION, PROPERTY, ANNOTATION_CLASS, CONSTRUCTOR, PROPERTY_SETTER, PROPERTY_GETTER, TYPEALIAS)
@MustBeDocumented
public annotation class Deprecated(
val message: String,
val replaceWith: ReplaceWith = ReplaceWith(""),
val level: DeprecationLevel = DeprecationLevel.WARNING
)
所以说,Deprecated的输入有三个,一个是message,解释弃用并建议使用替代 API 的信息;第二个是 replaceWith,指定可用于替换已弃用的函数,属性或类的代码片段;而 level 则指定如何在代码中报告已弃用的元素用法(WARNING, ERROR,HIDDEN三个选项)。后两个参数是有预设值的,第一个参数没有。所以,如果需要使用Deprecated函数注解的话,例如下面代码:
@Deprecated("xxx")
fun testKt(){
}
replaceWith的一个例子:

2. Suppress
如果需要消除一些编译时的警告,可以使用 @Suppress("xxx") 。
比如:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@Suppress("UNUSED VARIABLE")
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this,R.layout.activity_main)
}
}
又比如:
fun unChecked(){
val list: List<Any> = emptyList()
@Suppress("UNCHECKED_CAST")
list as List<String>
}
3. Volatile
为了强制变量中的更改立即对其他线程可见,我们可以使用注解 @Volatile,在以下示例中为:
@Volatile var shutdownRequested = false
这将保证在值更改后其他线程的更改可见性。 这意味着如果线程 X 修改了 shutdownRequested 的值,线程 Y 将能够立即看到更改。
@Volatile 的官方解释是:
Marks the JVM backing field of the annotated property as volatile, meaning that writes to this field are immediately made visible to other threads.
4. Synchronized
总结:Java 有 synchronized 关键字,可以将其应用于方法以确保一次只有一个线程可以访问它们。进入同步方法的线程获得锁(被锁定的对象是包含类的实例),并且在释放锁之前没有其他线程可以进入该方法。Kotlin通过 @Synchronized 注解提供了相同的功能。
在多线程的世界中,我们需要跨线程访问共享对象,如果我们不同步我们的工作,就会发生不希望的情况。比如下面例子:
import kotlinx.coroutines.*
fun main() = runBlocking {
var sharedCounter = 0
val scope = CoroutineScope(
Kotlin函数注解

本文介绍了Kotlin中的函数注解,包括@Deprecated、@Suppress、@Volatile、@Synchronized、@Bindable、@RequiresApi和@SerializedName等。这些注解在代码中扮演重要角色,帮助开发者实现从代码提示到并发控制等多种功能。
最低0.47元/天 解锁文章
11万+

被折叠的 条评论
为什么被折叠?



