Kotlin topic "Sixteen": Nested and inner classes

Foreword: Trying hard to get something, in fact, as long as you are calm and down-to-earth, you can easily achieve your goal without anyone noticing it. And if it is too vigorous, too violent, too childish, and too inexperienced, it will cry, grab, and pull, like a child pulling a tablecloth, but in the end nothing will be achieved. It's just that you pull all the good things on the table to the ground, and you will never get them.

1. Nested classes

Classes can be nested inside other classes:

    class Outer {
    
    
        private val number: Int = 1

        class Nested {
    
    
            fun foo() = 2
            //嵌套类不能访问外部了的私有或公有成员
        }
    }

	//调用
	 val nest = Outer.Nested().foo()//nest == 2

The call format is: external class. nested class (). nested class method/property.

The class Nested is nested inside the class Outer, which is called a nested class; the class Outer is an external class relative to the class Nested. Nested classes can be directly accessed through the outer class, but cannot access the outer private or public members.

Second, the inner class

In Kotlin, only innermarked nested classes are called inner classes, and other classes written inside the class are collectively called nested classes. Inner classes can access members of other outer classes, and inner classes refer to objects of outer classes:

    class Outer {
    
    
        private val number: Int = 0

		//被 `inner` 标记的嵌套类才叫内部类
        inner class Inner {
    
    
            fun foo() = number//内部类可以访问外部类私有成员
        }
    }

    val inner = Outer().Inner().foo()//inner == 0

The inner class can only be accessed through the outer class instance, it can access the outer class members. The calling format is: external class(). internal class(). internal class method/property.

3. Anonymous inner class

We no longer create objects by explicitly defining a subclass of the parent implementation, but directly create an object without a name through the parent implementation. This object is an anonymous inner class, which refers to an interface or an abstract class. Anonymous inner class instances are created using object expressions:

   //抽象类
  abstract class MyAdapter {
    
    
        open fun mouseClicked() {
    
    }//open 修饰,子类可重写
    }

	//调用匿名内部类
	fun test(){
    
    
	    object : MyAdapter() {
    
    //Kotlin 匿名内部类的写法:object + 冒号":" + 类名
            override fun mouseClicked() {
    
    
                println("MyAdapter == mouseClicked")
            }
        }
	}

Kotlin introduces the object keyword for the implementation of anonymous inner classes. This keyword is not the Object class in Java. The related usage will be explained later.

Note: On the JVM, if the object is an instance of a functional Java interface (that is, a Java interface with a single abstract method), you can create it using a lambda expression prefixed with the type of the interface:

val listener = View.OnClickListener {
    
     println("View.OnClickListener") }

View.OnClickListener has a single abstract method.

Four, local class

Partial classes in Kotlin are the same as partial classes in Java, that is, classes defined in methods (functions).

    class Outer {
    
    
        private val number: Int = 0

        fun memberMethod() {
    
    
            class Part {
    
    //局部类
                var numPart = number
                var name: String = "Part"

                fun partMethod() {
    
    
                    println("我是局部类中的方法")
                }
            }

            val part = Part()
            println("name == $name  |  numPart == " + part.numPart)
            part.partMethod()
        }
    }
    
	//调用
	Outer().memberMethod()

The print data is as follows:

name == Kotlin  |  numPart == 0
我是局部类中的方法

A partial class can only be used within the method in which the partial class is defined. Local classes defined in instance methods can access all variables and methods of the outer class, but cannot modify them.

Source address: https://github.com/FollowExcellence/KotlinDemo-master

Pay attention, don't get lost


Well, everyone, the above is the whole content of this article. The people who can see here are all talents .

I am suming, thank you for your support and recognition, your likes are the biggest motivation for my creation, see you in the next article!

If there are any mistakes in this blog, please criticize and advise, thank you very much!

If you want to become an excellent Android developer, here is the knowledge framework you must master , and move towards your dream step by step! Keep Moving!

Guess you like

Origin blog.csdn.net/m0_37796683/article/details/108863997