📜  Kotlin 嵌套类和内部类

📅  最后修改于: 2022-05-13 01:55:15.714000             🧑  作者: Mango

Kotlin 嵌套类和内部类

嵌套类

一个类在另一个类中声明,然后它被称为嵌套类。默认情况下,嵌套类是静态的,因此我们可以使用点 (.) 表示法访问嵌套类属性或变量,而无需创建类的对象。
声明语法:

class outerClass {
       ............
      // outer class properties or member function
      
      class nestedClass { 
            ..........
            // inner class properties or member function
      }
}

访问嵌套类属性的 Kotlin 程序:

Kotlin
// outer class declaration
class outerClass {
    var str = "Outer class"
    // nested class declaration
    class nestedClass {
        val firstName  = "Praveen"
        val lastName = "Ruhil"
    }
}
fun main(args: Array) {
    // accessing member of Nested class
    print(outerClass.nestedClass().firstName)
    print(" ")
    println(outerClass.nestedClass().lastName)
}


Kotlin
// outer class declaration
class outerClass {
    var str = "Outer class"
    // nested class declaration
    class nestedClass {
        var s1 = "Nested class"
        // nested class member function
        fun nestfunc(str2: String): String {
            var s2 = s1.plus(str2)
            return s2
        }
    }
}
fun main(args: Array) {
    // creating object of Nested class
    val nested = outerClass.nestedClass()
    // invoking the nested member function by passing string
    var result = nested.nestfunc(" member function call successful")
    println(result)
}


Kotlin
// outer class declaration
class outerClass {
    var str = "Outer class"
    // innerClass declaration without using inner keyword
    class innerClass {
        var s1 = "Inner class"
        fun nestfunc(): String {
            // can not access the outer class property str
            var s2 = str
            return s2
        }
    }
}
// main function
fun main(args: Array) {
    // creating object for inner class
    val inner= outerClass().innerClass()
    // inner function call using object
    println(inner.nestfunc())
}


Kotlin
// outer class declaration
class outerClass {
    var str = "Outer class"
    // innerClass declaration with using inner keyword
    inner class innerClass {
        var s1 = "Inner class"
        fun nestfunc(): String {
            // can access the outer class property str
            var s2 = str
            return s2
        }
    }
}
// main function
fun main(args: Array) {
    // creating object for inner class
    val inner= outerClass().innerClass()
    // inner function call using object
    println(inner.nestfunc()+" property accessed successfully from inner class ")
}


输出:

Praveen Ruhil

在 Kotlin 中,要访问嵌套类的成员函数,我们需要为嵌套类创建对象并使用它调用成员函数。

访问嵌套类成员函数的 Kotlin 程序:

科特林

// outer class declaration
class outerClass {
    var str = "Outer class"
    // nested class declaration
    class nestedClass {
        var s1 = "Nested class"
        // nested class member function
        fun nestfunc(str2: String): String {
            var s2 = s1.plus(str2)
            return s2
        }
    }
}
fun main(args: Array) {
    // creating object of Nested class
    val nested = outerClass.nestedClass()
    // invoking the nested member function by passing string
    var result = nested.nestfunc(" member function call successful")
    println(result)
}

输出:

Nested class member function call successful

与Java的比较

当我们考虑功能和用例时,Kotlin 类与Java类非常相似,但并不完全相同。 Kotlin 中的嵌套类似于Java中的静态嵌套类,而 Inner 类类似于Java中的非静态嵌套类。

Kotlin 内部类

当我们可以使用关键字inner在另一个类中声明一个类时,它被称为内部类。借助内部类,我们可以访问内部类内部的外部类属性。

class outerClass {
       ............
      // outer class properties or member function
      
      inner class innerClass { 
            ..........
            // inner class properties or member function
      }
}

在下面的程序中,我们试图从内部类成员函数访问str 。但它不起作用并给出编译时错误。
内部类的 Kotlin 程序:

科特林

// outer class declaration
class outerClass {
    var str = "Outer class"
    // innerClass declaration without using inner keyword
    class innerClass {
        var s1 = "Inner class"
        fun nestfunc(): String {
            // can not access the outer class property str
            var s2 = str
            return s2
        }
    }
}
// main function
fun main(args: Array) {
    // creating object for inner class
    val inner= outerClass().innerClass()
    // inner function call using object
    println(inner.nestfunc())
}

输出:

Error:(9, 22) Kotlin: Unresolved reference: str

首先,在内部类前面使用inner关键字。然后,创建外部类的实例,否则我们不能使用内部类。

科特林

// outer class declaration
class outerClass {
    var str = "Outer class"
    // innerClass declaration with using inner keyword
    inner class innerClass {
        var s1 = "Inner class"
        fun nestfunc(): String {
            // can access the outer class property str
            var s2 = str
            return s2
        }
    }
}
// main function
fun main(args: Array) {
    // creating object for inner class
    val inner= outerClass().innerClass()
    // inner function call using object
    println(inner.nestfunc()+" property accessed successfully from inner class ")
}

输出:

Outer class property accessed successfully from inner class