将Kotlin转换为JavaFX,在Kotlin中使用Java的getter方法?

huangapple go评论75阅读模式
英文:

Kotlin to JavaFX, using java getters in kotlin?

问题

所以我已经为几个TextField创建了Java的getter方法,现在我正在尝试在一个Kotlin类文件中使用名为projectName的textfield对象,这是我写的代码(在标有projectName的地方,它被标记为红色):

fun createData() {
    val projectData = projectInformation.projectName
}

以下是我用于参考的Java getter:

TextField projectName = new TextField();
public TextField getProjectName() { return projectName; }

当我尝试运行程序时,会出现错误:

Kotlin: 无法解析的引用: projectName

请不要给我发送Java到Kotlin的文档,因为我已经查看过了。现在修复这个问题的方法是将我的变量设为静态的,所以:

static TextField projectName = new TextField();

现在它可以工作了,但是这不是在某种程度上违背了getter的目的吗?我还尝试将变量设为私有,因为有人告诉我可能会因为我的TextFields是包可见的而导致可见性冲突。

只是为了提供更多信息,我正在使用Java和Kotlin开发一个应用程序,我已经使用这些TextFields创建了一个JavaFX GUI,GUI的逻辑/后端使用Kotlin,它还将用于将信息存储到数据库中。我的整个程序要点:https://gist.github.com/Jabz259/bbd7a10ba447c955b4481fff9c0ec58e

英文:

So I have created java getters for several TextFields, now I am trying to use a textfield object called projectName in a kotlin class file, this is what I wrote (where it says projectName, it is highlighted red)

fun createData () {
val projectData = projectInformation.projectName
}

Here is my java getter for reference

TextField projectName = new TextField();
public TextField getProjectName() { return projectName; }

I get an error when I try to run the program

Kotlin: Unresolved reference: projectName

Please do not send me the Java to Kotlin documentation as I have already had a look at it. Now the fix to this is to make my variable static, so

static TextField projectName = new TextField();

It now works, but does this not defeat the whole purpose of getters? I also tried making the variable private because I was told it could have possibly had visibility conflicts since my TextFields are package-visible.

Just for further information, I am using java and Kotlin to develop an application, I have used these TextFields to create a JavaFX GUI, the logic/backend of the GUI is using Kotlin which will also be used to store information into a database. Gist of my entire program : https://gist.github.com/Jabz259/bbd7a10ba447c955b4481fff9c0ec58e

答案1

得分: 2

根据您提供的要求,这是翻译后的内容:

给定您链接的要点:

是错误的,projectInformationprojectInformation 类本身,而不是它的实例;就像写

val x = String.length

而不是

val str = "some string"
val x = str.length

这解释了为什么添加 static 起作用,但这可能不是您真正想要做的。

如果您遵循命名约定,并且对于类/接口使用大驼峰命名法(例如 ProjectInformation 而不是 projectInformation),这将减少出现此问题的机会。

英文:

Given the gist you linked

> Yes projectInformation is an instance of a class where getProjectName() is defined

is wrong. It is in fact the class projectInformation itself, not an instance of it; like writing

val x = String.length

instead of

val str = "some string"
val x = str.length

This would explain why adding static works, but that's probably not what you actually want to do.

If you follow naming conventions and use upper camel case for classes/interfaces (e.g. ProjectInformation instead of projectInformation) this would decrease the chance for this problem.

答案2

得分: 0

你在这里定义了一个局部变量,可能是没有用的:

fun createData() {
    val projectData = projectInformation.projectName
}

projectName 在我看来听起来像是一个 String,但是使用 lateinit 应该能解决问题:

lateinit var projectName: TextView

而一般来说,在 Kotlin 中,getter 和 setter 的工作方式与之前大不相同:

https://kotlinlang.org/docs/reference/properties.html


“Kotlin Extensions with synthetic views”(合成属性)从来都不是推荐的做法,因为它们在跨模块时不起作用。对于您想要实现的内容,推荐使用 DataBinding。请参阅为什么不再推荐使用 kotlinx synthetic

英文:

You define a local variable here, which is probably useless:

fun createData () {
val projectData = projectInformation.projectName
}

projectName sounds alike a String to me, but lateinit should do the job:

lateinit var projectName: TextView

And generally speaking, getters and setter work a whole lot different in Kotlin:

https://kotlinlang.org/docs/reference/properties.html


"Kotlin Extensions with synthetic views" (synthetic properties) was never recommend, as they don't work cross-module. DataBinding would be the better and suggested approach for what you are trying to accomplish there. See Why kotlinx synthetic is no longer a recommended practice.

huangapple
  • 本文由 发表于 2020年10月14日 06:18:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64343948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定