如何从Kotlin的数据类访问Val?
因此,我想访问iSinbounds
navigation.kt:
data class Coordinate(val x:Int , val y:Int){
val isInBounds = x >= 0 && y >= 0
operator fun plus(other:Coordinate) = Coordinate(x + other.x, y + other.y)
}
但是当我尝试访问它时,我会得到未解决的参考。
game.kt
private fun move(directionInput: String) = try {
val direction = Direction.valueOf(directionInput.uppercase())
val newPosition = direction.updateCoordinate(player.currentPosition)
if (!newPosition.isInBounds){
}
else{
}
} catch (e: Exception){
}
enum class Direction(private val coordinate: Coordinate){
NORTH(Coordinate(0 , -1)),
EAST(Coordinate(1 , 0)),
SOUTH(Coordinate(0 , 1)),
WEST(Coordinate(-1 , 0));
fun updateCoordinate(playerCoordinate:Coordinate) {
coordinate + playerCoordinate
}
}
So I want to access isInBounds
Navigation.kt:
data class Coordinate(val x:Int , val y:Int){
val isInBounds = x >= 0 && y >= 0
operator fun plus(other:Coordinate) = Coordinate(x + other.x, y + other.y)
}
But when I try to access it I get an unresolved reference.
Game.kt
private fun move(directionInput: String) = try {
val direction = Direction.valueOf(directionInput.uppercase())
val newPosition = direction.updateCoordinate(player.currentPosition)
if (!newPosition.isInBounds){
}
else{
}
} catch (e: Exception){
}
enum class Direction(private val coordinate: Coordinate){
NORTH(Coordinate(0 , -1)),
EAST(Coordinate(1 , 0)),
SOUTH(Coordinate(0 , 1)),
WEST(Coordinate(-1 , 0));
fun updateCoordinate(playerCoordinate:Coordinate) {
coordinate + playerCoordinate
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误是在
方向
。请注意,您没有为updateCoortion
,和指定返回类型,而是为其使用了块体。这意味着它隐式返回单位
。因此,updateCoortion
计算坐标 + playerCorecrion
,丢弃结果,然后返回单位
。这使newPosition
为单位
,这显然不会具有iSinbounds
属性。显然,这不是您想要的。相反,您应该用表达式声明
updateCoortion
:或者如果您喜欢块体:
这只是我的意见,但我认为将
updateCoortion
作为一种方法更可读性在坐标
上,称为MOVETTOWARDS
:The mistake is in
Direction
. Note that you did not specify a return type forupdateCoordinate
, and you used a block body for it. This means that it implicitly returnsUnit
. SoupdateCoordinate
calculatescoordinate + playerCoordinate
, discards the result, and returnsUnit
. This causesnewPosition
to beUnit
, and that is obviously not going to be have anisInBounds
property.Clearly that is not what you want. You should instead declare
updateCoordinate
with an expression body:or if you like block bodies:
This is just my opinion, but I think it is more readable to have
updateCoordinate
as a method onCoordinate
, calledmovedTowards
:您的
updateCoortion
函数实际上没有完成任何事情,因为它可以计算出某些内容而无需对结果做任何事情,例如返回它。因此,它隐式返回单元,因此newPosition
只是对单元的引用。更改函数以返回新的坐标:
我建议给此功能一个更好的名称。 “更新”一词意味着它正在突变传递给它的实例,而不是计算新的实例。
从逻辑上讲,由于此功能是在坐标中添加的东西,因此可以倒置代码的可读性和简单的推理将其倒置更有意义(交换接收器和参数)。因此,我将此功能从枚举类中删除,并使其成为坐标扩展功能。
但是您必须将方向的
坐标
属性公开或内部
。Your
updateCoordinate
function doesn't actually accomplish anything because it calculates something without doing anything with the result, like returning it. Therefore, it implicitly returns Unit, sonewPosition
is just a reference to Unit.Change the function to return the new coordinate:
I recommend giving this function a better name. The word "update" implies that it is mutating the instance that is passed to it instead of calculating a new one.
Logically, since this function is something that adds something to a coordinate, it would make more sense for legibility and easy reasoning of your code to invert it (swap the receiver and parameter). So I would pull this function out of the enum class and make it a Coordinate extension function.
but you'll have to make the Direction's
coordinate
property public orinternal
.