[英]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.