简体   繁体   English

从特征定义的方法中的伴随类中访问常量

[英]Access a constant from the companion class inside a method defined in a trait

I have created a trait called Animal and two classes, Dog and Cat. 我创建了一个名为Animal的特征,并创建了Dog和Cat这两个类。 Both Dog and Cat have companion classes that store the number of lives they have. 狗和猫都有同伴类,可存储他们的生命数量。 My Cat object has 9 lives and my Dog object has 1 life. 我的猫对象有9条生命,我的狗对象有1条生命。 I want to add a function to the Animal trait called isAlive and implement there. 我想向Animal trait中添加一个名为isAlive的函数并在其中实现。 The isAlive function needs access to the number of live the Animal has. isAlive函数需要访问Animal的活动数量。 How would I get access to the lives value in the companion classes? 我如何获得同伴类中的生命价值?

Should I just move the lives value into the Class and remove the companion classes? 我是否应该将生活价值转移到班级中并删除同伴班级?

Here's my code. 这是我的代码。

The trait 特质

package Animal

trait Animal {

  def speak: String

  def getDeaths: Int

  def isAlive: Boolean = {
    getDeaths < 1 // I want to replace 1 with the number of lives from the Dog or Cat
  }
}

The Cat Class and Companion Class 猫班和同伴班

package Animal

class Cat(a: Int) extends Animal {
  private var age: Int = a
  private var deaths: Int = 0

  def this() = this(0)

  override def speak: String = {
    if (this.isAlive) {
      "Meow"
    }
    else {
      "..."
    }
  }

  // I want to do this in the trait
  // override def isAlive: Boolean = this.deaths <= Cat.lives

  def setAge(age: Int): Unit = this.age = age

  def getAge: Int = this.age

  def getDeaths: Int = this.deaths

  def die(): Unit = this.deaths += 1

}

object Cat {
  val lives: Int = 9
}

I'd include lives as an abstract method in Animal trait, similar to getDeaths (btw, Scala doesn't follow Java's naming conventions for getters and setters ). 我将lives作为Animal特征中的抽象方法包括在内,类似于getDeaths (顺便说一句,Scala不遵循Java的getter和setter命名约定 )。

If you're familiar with Java, companion object in scala is similar to Java's static , meaning that type resolution of objects happens in compile time and it's not possible to use polymorphism the same way as you do with methods and fields of the class . 如果你熟悉Java,Scala的同伴对象类似于Java的static ,这意味着该类型分辨率objects在编译时发生,这是不可能的,因为你有方法和字段做使用多态同样的方法class

Here is the code: 这是代码:

trait Animal {
  def speak: String
  def getDeaths: Int
  def lives: Int

  def isAlive: Boolean = {
    getDeaths < lives
  }
}


class Cat(a: Int) extends Animal {
  override val lives: Int = 9

  private var age: Int = a
  private var deaths: Int = 0

  def this() = this(0)

   /* ... */
}

Alternatively, you can defined lives constant in companion object and reference it in concrete class when overriding lives method: 另外,您可以在伴随对象中定义lives常数,并在覆盖lives方法时在具体类中引用它:

class Cat(a: Int) extends Animal {
  override val lives: Int = Cat.lives

  private var age: Int = a
  private var deaths: Int = 0

  def this() = this(0)

   /* ... */
}

object Cat {
  val lives = 9
}

To access lives within a trait it either has to be a part of the trait, as @Alvean has pointed out, or you have to make it a requirement that classes extending the trait need to have it. 要访问trait lives ,就像@Alvean所指出的那样,它必须是特质的一部分,或者您必须要求扩展特质的类必须具有它。

trait Animal { self: {val lives: Int} =>
  def isAlive: Boolean = getDeaths < lives
  . . .
}

class Cat(a: Int) extends Animal {
  val lives = Cat.lives  //required to be an Animal
  . . .
}

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

相关问题 无法从伴随对象访问伴随类的方法 - Can't access method of companion class from companion object 从伴随对象中的特征访问字段 - Access field from trait in companion object 从基本特征访问伴侣对象 - Access companion object from base trait 伴生对象可以扩展一些与伴生类不同的特征吗? - Can companion object extend some trait different from companion class? 使用trait对象中指定的方法的通用内部伴侣对象返回类实例 - Return class instance using generic inside companion object for method specified in trait 需要从案例类的特征中引用同伴对象的特征 - Need to Reference Trait on Companion Object From Trait on Case Class Scala对象扩展抽象类/特征,访问伴随类字段 - Scala object extends abstract class/trait, access companion class fields 在扩展特征的伴侣中定义时,反射看不到子类案例 class 的伴侣 - Reflection does not see companion of subclass case class when defined in companion of extending trait 创建一个伴随对象,该对象混合了一个特性,该特性定义了一个方法,该方法返回对象的伴随类的对象 - Create a companion object that mixes in a trait that defines a method which returns an object of the object's companion class 为什么我的伴随对象无法访问其伴随类中的方法 - Why is my companion object cannot access method in its companion class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM