Scala 函数作为对象与类 [英] Scala Functions as object vs class

查看:53
本文介绍了Scala 函数作为对象与类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

trait MyFunctionTrait extends ((Int, Int) => Double)

class MyFunction1 extends MyFunctionTrait {
   override def apply(a: Int, b: Int) => Double = a/b 
}

object MyFunction2 extends MyFunctionTrait {
   override def apply(a: Int, b: Int) => Double = a/b 
}

我不完全确定应该如何使用哪一个.区别是如何运行它们?

I am not entirely sure which one to use how. Is the differency how to run them?

scala> val f = new MyFunction1
f: MyFunction1 = <function2>

scala> f(1,2)
res50: Double = 0.0

scala> MyFunction2(1,2)
res48: Double = 0.0

我确实知道单例对象和类之间的区别是什么.我想知道在定义函数的特殊情况下的用例.文章中解释了函数,但我已经看到它被编程为一个类和一个对象,所以我想知道最佳实践是什么时候以及为什么.

I do know what the difference between a singleton object and a class is. I want to know the use-cases in the particular case of defining a Function. A Function is explained in articles, but I have seen it programmed as a class and as an object, so I'd like to know what best practise is when and why.

在我的特定情况下,我想以柯里化风格将另一个函数作为函数的参数,然后使用不同的 MyFunction,根据它们获得的函数执行略有不同的事情:如何编写柯里化 Scala 函数特征?

In my particular case I want to give another Function as a parameter to the Function in a currying style and then have different MyFunction which do slightly different stuff depending on what Function they got: How to write a currying Scala Function trait?

请回答1)一般情况,2)特殊情况.

PS:我试图将关于如何咖喱以及是类还是对象的问题分开 - 似乎不太好,就在这里.

PS: I tried to separate the question on how to curry and whether class or object - did not seem to work so well, right here.

推荐答案

这取决于你想做什么!

Scala 中的 apply() 函数只是语法糖 [12],其中允许您使用 MyObject(x) 作为 MyObject.apply(x) 的简写.

The apply() function in scala is just syntactic sugar [1, 2], which allows you to use MyObject(x) as a shorthand for MyObject.apply(x).

Scala 中的对象是单例对象,这意味着该类最多只有一个实例.因此,您可以调用 MyFunction2(1,2).

An object in scala is a singleton, which means there will at most be exactly one instance of the class. Hence, you can call MyFunction2(1,2).

类是具有已知字段和函数的类型.它是您尝试使用代码建模的内容的定义.因此,您不能调用 MyFunction1(1,2),因为您需要类的实例才能访问其字段和函数.

A class is a type with known fields and functions. It is a definition of what you're trying to model with your code. Hence, you cannot call MyFunction1(1,2), as you need an instance of the class to be able to access its fields and functions.

话虽如此,您可能想要重新考虑您正在尝试做什么.从这个意义上说,类和对象不是简单的数学函数,您可以在其中定义对象 x、对象 y、...,然后使用 x(...) 和 y(...).您想要创建的是一个类或一个对象(即单例类),您可以在其中定义函数,例如

That being said, you might want to rethink what you are trying to do. Classes and Objects are not - in that sense - simple mathematical functions, where you define object x, object y, ..., and then use x(...) and y(...). What you would want to create is either a class or an object (i.e. singleton class) where you define your functions, e.g.

object Calculator {

    def add(a: Int, b:Int): Int = {
        return a + b
    }

    def subtract(a: Int, b: Int): Int = {
        return a - b
    }

}

然后你可以用它来调用你的函数,例如Calculator.add(2,3).

which you can then use to call your functions, e.g. Calculator.add(2,3).

class Calculator {

    def add(a: Int, b: Int): Int = {
       return a + b
    }
}

你同样会用到的

val calc = new Calculator
calc.add(3,2)

再次,您会发现无需创建实例即可使用object Calculator.您可以将 Scala 对象中定义的字段和参数与 Java 类中的静态字段进行比较,您也可以在不创建类的实例化对象的情况下访问它们.您应该选择哪一个完全取决于您尝试编程的内容.要找出答案,您可能需要阅读 Scala 中对象和类之间的区别

Again, you will find that you can use object Calculator without creating an instance. You could compare fields and parameters defined in a Scala object with static fields in a Java class, which you can also access without creating an instantiating an object of the class. Which one you should is entirely depends on what you are trying to program. To find out, you may want to read Difference between object and class in Scala

这篇关于Scala 函数作为对象与类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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