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