CHAQUOPY - 将 Python 对象转换为 Kotlin class
CHAQUOPY - Convert Python Object to Kotlin class
在 python 文件中:
class Card:
def __init__(self, name, costs):
self.name = name
self.costs = costs
def returnObj(string):
card = Card(string, 2)
return card
在 Kotlin 文件中:
val pyObject = pythonFile.callAttr("returnObj", "string")
如何将 python 对象转换成 kotlin class?我不知道如何使用 .toJava(Class) 方法,我试图将一个与 python 对象具有相同属性的 kotlin class 放在那里,但它给了我一个错误“没有足够的信息来推断类型变量 T”。
Chaquopy 不支持根据相同的属性名称在 class 之间进行转换。但是,它很容易支持直接从 Python 代码创建 Kotlin/Java 对象。
例如,如果您的 Kotlin class 是 com.example.Card
,那么您可以将整个 Python Card
class 替换为行 from com.example import Card
,并且 returnObj
仍然可以正常工作。
那么在Kotlin中,你可以这样写:
val card = pythonFile.callAttr("returnObj", "string").toJava(Card::class.java)
在 python 文件中:
class Card:
def __init__(self, name, costs):
self.name = name
self.costs = costs
def returnObj(string):
card = Card(string, 2)
return card
在 Kotlin 文件中:
val pyObject = pythonFile.callAttr("returnObj", "string")
如何将 python 对象转换成 kotlin class?我不知道如何使用 .toJava(Class) 方法,我试图将一个与 python 对象具有相同属性的 kotlin class 放在那里,但它给了我一个错误“没有足够的信息来推断类型变量 T”。
Chaquopy 不支持根据相同的属性名称在 class 之间进行转换。但是,它很容易支持直接从 Python 代码创建 Kotlin/Java 对象。
例如,如果您的 Kotlin class 是 com.example.Card
,那么您可以将整个 Python Card
class 替换为行 from com.example import Card
,并且 returnObj
仍然可以正常工作。
那么在Kotlin中,你可以这样写:
val card = pythonFile.callAttr("returnObj", "string").toJava(Card::class.java)