Kotlin的内部阶级的目的是什么? [英] What is the purpose of inner class in kotlin?

查看:53
本文介绍了Kotlin的内部阶级的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是科特林的新手.我正在学习Kotlin中的嵌套类和内部类概念. 以下是嵌套类的示例.

I am new to kotlin. i am going through Nested class and inner class concepts in kotlin. Below is a example of nested class.

 fun main(args: Array<String>) {
   val demo = Outer.Nested().foo() // calling nested class method
   print(demo)
}
class Outer {
   class Nested {
      fun foo() = "Hello world"
   }
}

将输出显示为

Hello world

这是内部类的示例.

fun main(args: Array<String>) {
   val demo = Outer().Nested().foo() // calling inner class method
   print(demo)
}
class Outer {
   private val welcomeMessage: String = "Hello world"
   inner class Nested {
      fun foo() = welcomeMessage
   }
}

将输出表示为

Hello world

我的问题是,嵌套类何时可以执行与内部类相同的操作. 内部类的目的是什么?为什么我们需要实时的内部类?

my question is when nested class can perform the same operation as that of inner class . what is the purpose of inner class ?why we need inner class in real time?

推荐答案

嵌套类本身是一个相当简单的想法,一个概念在逻辑上依赖于另一个更广泛的概念.每当您发现自己描述了一个如此广泛的概念的构建基块时,您都想声明一个新的嵌套类,而您不希望它们凭空出现在您的业务逻辑中.因此,可以通过嵌套在该类中的该类的定义来访问此类的实例.

Nested class itself is a rather straightforward idea of one concept being logically dependent on another, wider concept. You would like to declare a new nested class whenever you find yourself describing a building block of such a wider concept you do not expect to ever show up in your business logic on its own out of thin air. Therefore, an instance of such a class is accessed via the definition of the class it is nested in.

以以下结构为例:

class Parrot(val name: String): Animal {

    //vals and vars useful for a parrot

    class HealthyConditions {
        val featherColour: String // Does not really apply to Dogs, Cats, Fish, etc.
        val cageSize: Size // Does not really apply to Dogs, Cats, Fish, etc.
        val weightInGrammes: Int
        val dailySleepInHours: Int

        fun isUnderweight(currentWeightInGrammes: Int): Boolean {
            return currentWeightInGrammes < weightInGrammes
        }
    }
}

现在,健康生活条件的概念对于该系统可能描述的任何Animal都是普遍且重要的,但是从一个物种到另一个物种,要考虑多个完全不同的因素.试图一次为所有它们准备一个通用的HealthyConditions<Animal>类将可能导致充满ifelse的代码难以理解且难以维护.因此,对于开发人员来说,为每个Animal自己定义简洁,整洁的嵌套类可能更有意义.稍后,将通过Parrot.HealthyConditions()Cat.HealthyConditions()等(而不是HealthyConditions.getForSpecies(animal))访问这些文件.

Now, the concept of healthy living conditions is common and important for any Animal this system may describe, but from one species to another there are multiple completely different factors to take into consideration. Trying to prepare a single common HealthyConditions<Animal> class for all of them at once will likely lead to unreadable and hard to maintain code, full of ifs and elses. Therefore, it may make more sense for the developer to define concise, tidy nested classes for each Animal on its own. Later on, these will be accessed through Parrot.HealthyConditions(), Cat.HealthyConditions(), etc. instead of HealthyConditions.getForSpecies(animal).

内部类是在嵌套类的基础上构建的Kotlin概念.尽管某些详细信息对于给定的概念非常特定,因此被描述为嵌套类,但是如果这些详细信息根据更广泛的概念的实例而进一步不同,该怎么办?然后,使用类定义访问它可能并不是正常运行所需要的全部.因此,可以通过调用作为内部类的类的实例来访问内部类.

Inner class is a Kotlin concept built on top of the idea of a nested class. While some details are very specific for a given concept and therefore are being described as nested classes, what if those details vary even further depending on the wider concept's instance? Then accessing it using the class definition may not be all it takes to operate properly. Inner classes are therefore accessed through the call to an instance of the class they are an inner class of.

让我们回到我们的Parrot吧?

class Parrot(val name: String, var age: Int): Animal {

    //vals and vars useful for a parrot

    inner class HealthyConditions {
        val featherColour: String // Does not really apply to Dogs, Cats, Fish, etc.
        val cageSize: Size // Does not really apply to Dogs, Cats, Fish, etc.
        val weightInGrammesByAge: Map<Int, Int>
        val dailySleepInHoursByAge: Int<Int, Int>
        
        /** variable 'age' from the Parrot class can be accessed only because the HealthyConditions class is marked inner!  */
        fun isUnderweight(currentWeightInGrammes: Int): Boolean {
            return currentWeightInGrammes[age] < weightInGrammes 
        }
    }
}

健康的Parrot体重应在所述鹦鹉的整个生命过程中发生变化.孵化几百克的幼鱼是可以的,但一个成年的个体体重在2公斤以下可能需要引起注意.因此,Parrot是否体重不足X克这个问题很难回答,但是如果我们问鹦鹉鹦鹉鲍勃(Bob)是否体重有所增加,我们可以利用我们对鲍勃(Bob)的了解来确定答案.为此,将通过Parrot("Bob", 5).HealthyConditions()访问HealthyConditions类.

How much a healthy Parrot should weigh changes over the course of the said parrot's life. A hatchling weighing a couple hundreds of grams is fine but a grown individual under 2 kilograms might need some attention. Therefore, the question of whether or not a Parrot is underweight at X grams cannot be answered easily, but should we ask if Bob the Parrot should gain some weight, we could use what we know about Bob to determine the answer. In order to do that, the HealthyConditions class will be accessed through Parrot("Bob", 5).HealthyConditions().

现在,最重要的是,您可能仍然想知道访问父类属性对于内部类是否真的有用.毕竟,您可以在调用isUnderweight()函数的任何地方简单地提供age值,对吗?当然是对的,但是在讨论嵌套类时,我们决定每个Animal都应该拥有自己的HealthyConditions实现.对于Dog类,在确定其正确体重和年龄方面,狗的品种同样重要.对于其他物种,性别也很重要.这意味着,如果不能直接访问内部类对其父代属性的访问,Healthcheck接口的函数checkIfUnderweight()必须不得不接受很多不同的,可能为空的变量,才能适用于系统中的所有不同种类的动物.

Now, to top it off, you might still wonder if accessing parent class properties is really useful for the inner class. After all, you could simply provide the age value wherever you call the isUnderweight() function, right? That is true, of course, but when discussing nested classes we decided each Animal deserves their own HealthyConditions implementation. For a Dog class, the breed of the dog is just as important in determining its correct weight as is its age. For yet other species, the gender will also be of importance. Meaning that, without direct access of the inner class to its parent's properties, Healthcheck interface's function checkIfUnderweight() would have to accept awfully lots of different, probably nullable, variables in order to be applicable to all different kinds of animals in the system.

我花了很多时间讨论这个概念,没有很多代码示例,但是据我了解,您并不为实现而苦恼,而是有理由甚至一开始就实现代码.另外,如果动物类别"无效,请原谅我.例如,我想出的可能显得有些愚蠢-事实是这是我第一次想到. ;)

I took a lot of time talking over the concept without many code samples but from what I understood you do not struggle with the implementation, but with the justification for even implementing the code in the first place. Also, forgive me if the "Animal classes" example I came up with may appear dumbed down - the truth is it was the first I came up with. ;)

无论如何,我希望这段简短的阅读可以帮助您更好地理解该概念及其用例.安全的编码,可能使错误远离您!

Anyways, I hope this short read may help you understand the concept and its use cases better. Safe coding and may the bugs stay away from you!

这篇关于Kotlin的内部阶级的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆