Kotlin:如何从Java类中调用setter和getter?

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

Kotlin: How to call setters and getters from java class?

问题

以下是我的Java类,我想从我的Kotlin类中获取其中的文本字段,以获取其文本,然后将其存储到数据库中。但是,我已经查阅了多个网站并观看了许多视频,但没有一个明确说明如何获取变量。

以下是我的Java类,我应该如何从Kotlin类中访问并获取输入到这些字段中的信息?

TextField projectName = new TextField();
TextField teamName = new TextField();
TextField mainTaskName = new TextField();
TextField additionalTask = new TextField();
ComboBox teamSelection = new ComboBox();
ComboBox supportSelection = new ComboBox();

public TextField getProjectName() {
    return projectName;
}

public TextField getTeamName() {
    return teamName;
}

public ComboBox getTeamSelection() {
    return teamSelection;
}

public ComboBox getSupportSelection() {
    return supportSelection;
}

public TextField getMainTaskName() {
    return mainTaskName;
}

public TextField getAdditionalTask() {
    return additionalTask;
}
英文:

So I have multiple textFields in my java class which I want to be able to get from my kotlin class to get its text and then store into a database. However, I have checked multiple sites and watched many videos, but none explicitly mention how to get a variable.

Below is my java class, how can I access and get the information inputted into these fields from a Kotlin class?

TextField projectName = new TextField();
TextField teamName = new TextField();
TextField mainTaskName = new TextField();
TextField additionalTask = new TextField();
ComboBox teamSelection = new ComboBox();
ComboBox supportSelection = new ComboBox();


public TextField getProjectName() {
    return projectName;
}

public TextField getTeamName() {
    return teamName;
}

public ComboBox getTeamSelection() {
    return teamSelection;
}

public ComboBox getSupportSelection() {
    return supportSelection;
}

public TextField getMainTaskName() {
    return mainTaskName;
}

public TextField getAdditionalTask() {
    return additionalTask;
}

答案1

得分: 2

遵循Java约定的获取器和设置器方法(无参数方法名称以get开头,单参数方法名称以set开头)在Kotlin中被表示为属性。

例如,调用yourClassInstance.projectName

来源:从Kotlin调用Java

英文:

Methods that follow the Java conventions for getters and setters (no-argument methods with names starting with get and single-argument methods with names starting with set) are represented as properties in Kotlin.

Call yourClassInstance.projectName for example.

Source: Calling Java from Kotlin

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

发表评论

匿名网友

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

确定