如何从Kotlin的数据类访问Val?

发布于 2025-01-21 08:56:37 字数 963 浏览 0 评论 0原文

因此,我想访问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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み青杉依旧 2025-01-28 08:56:37

错误是在方向。请注意,您没有为updateCoortion指定返回类型,而是为其使用了块体。这意味着它隐式返回单位。因此,updateCoortion计算坐标 + playerCorecrion,丢弃结果,然后返回单位。这使newPosition单位,这显然不会具有iSinbounds属性。

显然,这不是您想要的。相反,您应该用表达式声明updateCoortion

fun updateCoordinate(playerCoordinate:Coordinate) =
    coordinate + playerCoordinate

或者如果您喜欢块体:

fun updateCoordinate(playerCoordinate:Coordinate): Coordinate {
    return coordinate + playerCoordinate
}

这只是我的意见,但我认为将updateCoortion作为一种方法更可读性在坐标上,称为MOVETTOWARDS

data class Coordinate(val x: Int, val y: Int) {
    operator fun plus(coord: Coordinate) = ...

    fun movedTowards(direction: Direction) =
        this + direction.coordinate // you'd have to make direction.coordinate public
}

The mistake is in Direction. Note that you did not specify a return type for updateCoordinate, and you used a block body for it. This means that it implicitly returns Unit. So updateCoordinate calculates coordinate + playerCoordinate, discards the result, and returns Unit. This causes newPosition to be Unit, and that is obviously not going to be have an isInBounds property.

Clearly that is not what you want. You should instead declare updateCoordinate with an expression body:

fun updateCoordinate(playerCoordinate:Coordinate) =
    coordinate + playerCoordinate

or if you like block bodies:

fun updateCoordinate(playerCoordinate:Coordinate): Coordinate {
    return coordinate + playerCoordinate
}

This is just my opinion, but I think it is more readable to have updateCoordinate as a method on Coordinate, called movedTowards:

data class Coordinate(val x: Int, val y: Int) {
    operator fun plus(coord: Coordinate) = ...

    fun movedTowards(direction: Direction) =
        this + direction.coordinate // you'd have to make direction.coordinate public
}
烟花易冷人易散 2025-01-28 08:56:37

您的updateCoortion函数实际上没有完成任何事情,因为它可以计算出某些内容而无需对结果做任何事情,例如返回它。因此,它隐式返回单元,因此newPosition只是对单元的引用。

更改函数以返回新的坐标:

fun updateCoordinate(playerCoordinate: Coordinate): Coordinate {         
    return coordinate + playerCoordinate     
} 

我建议给此功能一个更好的名称。 “更新”一词意味着它正在突变传递给它的实例,而不是计算新的实例。

从逻辑上讲,由于此功能是在坐标中添加的东西,因此可以倒置代码的可读性和简单的推理将其倒置更有意义(交换接收器和参数)。因此,我将此功能从枚举类中删除,并使其成为坐标扩展功能。

fun Coordinate.moved(direction: Direction): Coordinate {
    return this + direction.coordinate
}

但是您必须将方向的坐标属性公开或内部

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, so newPosition is just a reference to Unit.

Change the function to return the new coordinate:

fun updateCoordinate(playerCoordinate: Coordinate): Coordinate {         
    return coordinate + playerCoordinate     
} 

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.

fun Coordinate.moved(direction: Direction): Coordinate {
    return this + direction.coordinate
}

but you'll have to make the Direction's coordinate property public or internal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文