简体   繁体   English

为什么可以在 Kotlin 的类之外编写函数?

[英]Why is possible to write a function outside a class in Kotlin?

I don't understand why is possible to write a function outside a class in Kotlin?我不明白为什么可以在 Kotlin 的类之外编写函数? Is that a good practice?这是一个好习惯吗?

For example, it's possible in Kotlin to write a function outside my MainActivity class:例如,在 Kotlin 中可以在我的MainActivity类之外编写一个函数:

fun hello(){}

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        hello()
    }
}

In Java, this is impossible, That's not how an object-oriented language works normally?在 Java 中,这是不可能的,这不是面向对象语言的正常工作方式吗? right?正确的?

In the documentation, they talk of Local Functions for the classic function and Member Functions for the function defined inside a class or object but they don't explain when it's better to use one or the other.在文档中,他们讨论了经典函数的局部函数和类或对象内部定义的函数的成员函数,但他们没有解释何时使用其中一个或另一个更好。

In Java, this is impossible, That's not how an object-oriented language works normally?在 Java 中,这是不可能的,这不是面向对象语言的正常工作方式吗? right?正确的?

Just stop for a second and reconsider the nature of java's static method.稍等片刻,重新考虑 java 静态方法的性质。 A class is supposed to be a blueprint for objects, describe their behavior and state.类应该是对象的蓝图,描述它们的行为和状态。 But you can call a static method without creating any instances.但是您可以在不创建任何实例的情况下调用静态方法。

How does that fit into the object-oriented picture?这如何适应面向对象的画面? How does a static method "belong" to the class it's declared in?静态方法如何“属于”它在其中声明的类?

Actually, static methods are top-level functions and the class only provides a namespace for them.实际上,静态方法是顶级函数,类只是为它们提供一个命名空间。 Speaking strictly from the OOP perspective you bring up, they are a hack.严格来说,从您提出的 OOP 角度来看,他们是黑客。 But you got used to them over the years so you don't feel that anymore.但多年来你已经习惯了它们,所以你不再有那种感觉了。

In contrast to that, Kotlin allows you to declare top-level functions without forcing you to couple their name to a class.与此相反,Kotlin 允许您声明顶级函数,而无需强制您将它们的名称与类耦合。 Sometimes you expressly want to couple a top-level function to a class, and that's what companion objects are for in Kotlin.有时您明确希望将顶级函数耦合到类,这就是 Kotlin 中伴随对象的用途。

Yes, this is good practice.是的,这是很好的做法。 Kotlin is not a purely object-oriented language, so it's not obligated to follow how "an object-oriented language works normally" (even though other object-oriented languages, such as C++, Ruby and Python, also allow top-level functions). Kotlin 不是纯粹的面向对象语言,因此它没有义务遵循“面向对象语言正常工作”的方式(尽管其他面向对象语言,如 C++、Ruby 和 Python 也允许顶层函数) .

It's better to use a top-level function when the logic of this function does not clearly belong to any class.当这个函数的逻辑不明确属于任何类时,最好使用顶层函数。

Yes, it is a good practice to create package-level functions if the function logic is independent of properties and lifecycle of a class.是的,如果函数逻辑独立于类的属性和生命周期,那么创建包级函数是一个很好的做法 Example:例子:

  • a function to convert miles per gallon to kilometers per litre is independent of any object and fits well as package-level.将每加仑英里数转换为每升公里数的函数独立于任何对象,并且适合包装级别。
  • otoh, a function to cancel reservation would naturally be associated with a specifc reservation object and fits well inside such a class. otoh,取消预订的功能自然会与特定的预订对象相关联,并且非常适合此类。

The main benefit of a package-level function is simplicity (ergo better maintainability): callers of your function don't need to declare and create an object to call the function.包级函数的主要好处是简单(因此可维护性更好):函数的调用者不需要声明和创建对象来调用函数。 (If your package-level function needs to be called from Java code, this benefit is lost because the Java calling code has to use a class name that is generated by Kotlin.) (如果你的包级函数需要从 Java 代码中调用,这个好处就失去了,因为 Java 调用代码必须使用 Kotlin 生成的类名。)

IMPORTANT: Although you don't have a class lexical scope for your function, the Single-Responsibility Principle (SRP) still applies .重要提示:虽然您的函数没有类词法作用域,但单一职责原则 (SRP) 仍然适用 Do not create a Kotlin source file, say Util.kt, and bloat it up with functions that lack cohesion, that is, functions that do unrelated things.不要创建一个 Kotlin 源文件,比如 Util.kt,并用缺乏内聚性的函数使其膨胀,即做不相关事情的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我们编写NextActivity :: class.java,尽管这是一个kotlin类? - Why we write NextActivity::class.java although this is a kotlin class? 带有 Kotlin 协程的函数是否可以等待完成并返回 Java 类? - Is it possible for a function with a Kotlin coroutine to wait for completion and return to a Java class? 为什么不能在 class 之外使用 lambda 表达式实现功能接口? - Why is it not possible to implement a functional interface using a lambda expression outside a class? 对于Kotlin中的高阶函数,为什么lambda显示在其他函数参数之外? - With higher-order functions in Kotlin, why are lambdas shown outside of other function parameters? 是否可以在工作区之外运行类? - Is it possible to run a class outside of the workspace? 为什么Kotlin类的财产不公开? - Why is this Kotlin class property not public? 如何将 class 传递给 Kotlin 中的 function - How to pass a class to a function in Kotlin 可以在Kotlin中(即在顶层)编写函数。 我也可以对顶级人员进行单元测试吗? - It is possible to write function in Kotlin (ie. at top level). Can I unit-test those at top-level too? Netty IO从类外部写入服务器 - Netty IO write to server from outside the class Java在类外调用函数 - Java call a function outside a class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM