Kotlin 中 Java 静态方法的等价物是什么?
在 Java 中,“static”关键字用于高效的内存管理。一旦变量或方法被声明为static,JVM 就会只为这些变量分配一次内存。通常,静态变量用于声明类的公共属性,例如“机构名称”。在下面的示例中,我们将看到如何使用static关键字。
Java 中静态关键字的示例
为了演示 Java 中static 的工作方式,我们将访问我们的在线 Java 编译器并创建一个Test类。在Test类中,我们将尝试创建一个静态变量和一个static方法,并将无需创建任何类对象即可访问它们。
示例
public class Test
{
static int myStaticVariable = 10;
// static method
static void staticMethod()
{
System.out.println("www.tutorialspoint.com");
}
public static void main(String[] args)
{
// accessing static method and variable without creating
// any object of class Test
staticMethod();
System.out.println("Accessing Static variable-"+myStaticVariable);
}
}输出
执行代码后,将生成以下输出:
$javac Test.java $java -Xmx128M -Xms16M Test www.tutorialspoint.com Accessing Static variable-10
Kotlin 中 Java 静态方法的等价物
在 Kotlin 中,我们没有static关键字。在本文中,我们将学习如何使用 Kotlin 库中提供的不同关键字来实现相同的内存管理。目标是实现不同的 Kotlin 库函数,以选择只创建一次内存且其值不能从应用程序的其他部分修改的条件。
在 Kotlin 中使用static有两种方法:
使用伴生对象 (companion object)
使用 object 类和 @JvmStatic 注解
让我们详细了解这些方法。
使用伴生对象
在对象中添加一个companion将帮助开发者在 Kotlin 中实现static的功能。它将对象存储在与类存储在同一个文件中,因此它可以访问类内部的所有私有方法和变量。它与类初始化阶段一起初始化。
示例
我们将尝试实现一个伴生对象,并了解如何在 Kotlin 中高效地处理内存管理。
fun main(args: Array<String>) {
// Accessing class variable and method
//with out creating class object
println("Hello!"+'
' + "This an example of accessing class variable without creating object." + MyClass.staticField+'
')
println("Hello!"+'
' + "This an example of accessing class Method without creating an object." + MyClass.getStaticFunction()+'
');
}
class MyClass{
companion object {
val staticField = "This is a Static Variable."
fun getStaticFunction(): String {
return "This is a static Method."
}
}
}输出
执行代码后,将生成以下输出:
Hello! This an example of accessing class variable without creating object. This is a Static Variable. Hello! This an example of accessing class Method without creating an object. This is a static Method.
在此示例代码中,如果尝试修改任何static变量的值,您将看到 Kotlin 编译器会抛出错误。
示例
fun main(args: Array) {
// Trying to modify the static variable
MyClass.staticField="Hello Students";
println("Hello!"+'
'+"This an example of accessing class variable with out creating object-"+MyClass.staticField+'
')
}
class MyClass{
companion object {
val staticField = "This is an Static Variable"
fun getStaticFunction(): String {
return "This is a static Method"
}
}
}输出
上面的代码将生成以下错误:
$kotlinc -nowarn main.kt -include-runtime -d main.jar main.kt:5:5: error: val cannot be reassigned MyClass.staticField="Hello Students"; ^
使用 object 类和 @JvmStatic 注解
根据 Kotlin 文档,一旦将@JvmStatic注解应用于任何变量或方法,它将作为该类的static功能工作。
示例
在下面的示例中,我们将创建一个object类,并在该object类中,使用@JvmStatic注解声明变量和方法,以便在 Kotlin 环境中实现static功能。
fun main(args: Array) {
// Accessing class variable and method
//with out creating class object
println("Hello!"+'
' + "This an example of accessing a class variable without creating an object." +MyClass.staticField+'
')
println("Hello!"+'
' + "This an example of accessing a class Method without creating an object. " +MyClass.getStaticFunction()+'
');
}
object MyClass{
@JvmStatic
val staticField = "This is a Static Variable."
@JvmStatic
fun getStaticFunction(): String {
return "This is a Static Method."
}
}输出
它将在结果部分生成以下输出:
Hello! This an example of accessing a class variable without creating an object. This is a Static Variable. Hello! This an example of accessing a class Method without creating an object. This is a Static Method.
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP