简体   繁体   English

Scala 伴生对象和应用方法

[英]Scala Companion Objects and Apply method

I'm trying to make this statement work in my Scala code:我试图让这个语句在我的 Scala 代码中工作:

val dvd1 = Item(new Description("The Matrix DVD", 15.50, "DVD World"))

I have the following classes and companion object:我有以下类和伴侣对象:

class Item(){
    private var id = 0
    def getId(): Int = this.id
}

object Item{
    def apply(description: String, price: Double, supplier: String): Description = {
        new Description(description, price, supplier)
    }

    def nextId: Int = {
        this.id += 1
    }
}

class Description(description: String, price: Double, supplier: String){
    def getDescription(): String = description
    def getPrice(): Double = price
    def getSupplier(): String = supplier
}

And I get the following error with my apply function and nextId:我的应用函数和 nextId 出现以下错误:

error: not enough arguments for method apply: (description: String, price: Double, supplier: String)Description in object Item.
Unspecified value parameters price, supplier.
    val dvd1 = Item(new Description("The Matrix DVD", 15.50, "DVD World"))
                   ^
indus.scala:16: error: value id is not a member of object Item
        this.id += 1
             ^

It's not apparent to me what I'm doing wrong.我做错了什么对我来说并不明显。

Question: What do I need to change with my apply function so that dvd1 will work as expected.问题:我需要对我的应用函数进行哪些更改,以便dvd1能够按预期工作。 Also, nextId should increment the Item's id when Item.nextId is called, what's wrong there?此外,当Item.nextId被调用时,nextId 应该增加 Item 的 id,有什么问题吗?

1) you are trying to access class data from companion object which you can't. 1) 您正在尝试从无法访问的伴侣对象访问类数据。

scala> case class Order(id: String)
defined class Order

scala> object Order { println(id) }
<console>:11: error: not found: value id
       object Order { println(id) }
                              ^

The reverse works, once you import your companion object inside class.一旦你在类中导入你的伴生对象,反之亦然。

2) you when define apply inside your companion, you you have two apply functions now, one with empty args and one with three args as you defined which you want to call. 2)当你在你的同伴中定义apply时,你现在有两个apply函数,一个是空的args ,一个是你定义的三个args ,你想调用它们。 Your args is wrong.你的参数是错误的。

Based on your comments below, you want to have Item has Description datastructure which can be done as below using immutable classes in scala called case class根据您在下面的评论,您希望Item具有Description结构,可以按如下方式使用 Scala 中称为case class不可变类来完成

import scala.util.Random

case class Description(description: String, price: Double, supplier: String)
case class Item(id: Int, description: Description)

object Item {
  def apply(description: String, price: Double, supplier: String): Item = {
    val itemId = Random.nextInt(100)
    new Item(itemId, Description(description, price, supplier))
  }
}


//client code
val dvd1 = Item("The Matrix DVD", 15.50, "DVD World")

assert(dvd1.isInstanceOf[Item])
assert(dvd1.description.description == "The Matrix DVD")
assert(dvd1.description.price == 15.50)
assert(dvd1.description.supplier == "DVD World")

see code here in online scala editor - https://scastie.scala-lang.org/prayagupd/P3eKKPLnQYqDU2faESWtfA/4在在线 Scala 编辑器中查看代码 - https://scastie.scala-lang.org/prayagupd/P3eKKPLnQYqDU2faESWtfA/4

Also, read case class explanation另外,阅读案例类解释

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM