gpt4 book ai didi

android - 从数据类的伴随对象扩展函数时没有可见性

转载 作者:行者123 更新时间:2023-12-02 12:44:08 25 4
gpt4 key购买 nike

给定以下数据类:

data class Coords(val x: Int, val y: Int) {
companion object CoordsOps {
private val shiftTable: Map<Direction, Pair<Int, Int>> = mapOf(
Direction.North to Pair(0, 1),
Direction.East to Pair(1, 0),
Direction.South to Pair(0, -1),
Direction.West to Pair(-1, 0)
)

private operator fun Coords.plus(increment: Pair<Int, Int>): Coords =
Coords(x + increment.first, y + increment.second)

fun Coords.shift(direction: Direction): Coords = this + shiftTable.getValue(direction)
}
}
我无法使用扩展功能 shift()从那个对象之外。我想用 Coords.shift(direction) .
我究竟做错了什么?

最佳答案

你应该能够。我完全复制了您的代码,并且能够从另一个文件中调用扩展功能转移。
也许你错过了进口?还是叫错了?尝试这个

import <your_package>.Coords.CoordsOps.shift

val y = Coords(3, 4).shift(Direction.East)

关于android - 从数据类的伴随对象扩展函数时没有可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63984683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com