java Kotlin 中预期的属性 getter 或 setter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44277667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:02:57  来源:igfitidea点击:

Property getter or setter expected in Kotlin

javaandroidkotlin

提问by N Sharma

I am learning Kotlinfrom official docs, I am trying to create a class to do arithmetic operations.

我正在Kotlin从官方文档中学习,我正在尝试创建一个类来进行算术运算。

class Sum {

    var a: Int, b: Int;

    constructor(a: Int, b: Int) {
        this.a = a
        this.b = b
    }

    fun add(): Int {
        return a + b
    }
}

I have this class, now I want to create an object of this class like

我有这个类,现在我想创建一个这个类的对象,比如

val sum = Sum(1,2)
Log.d("Sum", sum.add())

I am getting this error in Sumclass:

我在Sum课堂上遇到这个错误:

Property getter or setter expected

预期属性 getter 或 setter

on b: int;within the line var a: Int, b: Int;

b: int;行内var a: Int, b: Int;

回答by GhostCat

var a: Int, b: Int;

Kotlin doesn't allow to have multiple declarations in one line. You have to go for:

Kotlin 不允许在一行中有多个声明。你必须去:

var a: Int
var b: Int

instead. The Kotlin folks simply found the C/java practice of "int a, b, .." to be something they wish to notsupport in Kotlin.

反而。在科特林人只是发现的“INT A,B,..”的C / Java的做法是,他们想的东西没有在科特林支持。

回答by chandil03

You are writing unnecessary code in your class.

您正在类中编写不必要的代码。

Write short form for constructorif there is only one.

constructor如果只有一个,请写下简短的表格。

If there are properties in class, they can be defined in constructorby using valor var.

如果类中有属性,可以constructor使用val或定义它们var

Use it like following:

像下面这样使用它:

class Sum (var a: Int,var b: Int){

    fun add(): Int {
        return a + b
    }
}

Do read Basic Syntaxof Kotlin.

请务必阅读基本语法Kotlin

回答by Mario

The error you have would be solved simply declaring the variables in two lines:

只需在两行中声明变量即可解决您遇到的错误:

var a: Int
var b: Int

However, the recommended way to integrate those variables in constructors (if you only want to have a single constructor with arguments):

但是,将这些变量集成到构造函数中的推荐方法(如果您只想拥有一个带参数的构造函数):

class Sum(var a: Int, var b: Int) {
    fun add(): Int = a + b
}

回答by Abhishek Luthra

Firstly , you must put property declarations on separate lines . You can refer this threadhere , where JetBrains' Engineer yole states that:

首先,您必须将属性声明放在​​不同的行中。您可以在此处参考此线程,其中 JetBrains 的工程师 yole 指出:

Declaring multiple properties on the same line is frowned upon by many Java style guides, so we did not implement support for that in Kotlin.

许多 Java 风格指南不赞成在同一行上声明多个属性,因此我们没有在 Kotlin 中实现对它的支持。

Once property declaration on separate lines is done , you should have no problem as kotlin genrates default getter and setter internally with same visibility as your property . If you do not specify any visibility modifier, public is used by default, which means that your declarations will be visible everywhere .

完成单独行上的属性声明后,您应该没有问题,因为 kotlin 会在内部生成与您的属性具有相同可见性的默认 getter 和 setter。如果您没有指定任何可见性修饰符,则默认使用 public,这意味着您的声明将在任何地方可见。

In fact, Kotlin has a concise syntax, for declaring properties and initializing them from the primary constructor(In case you have only one constructor which will be always primary):

事实上,Kotlin 有一种简洁的语法,用于声明属性并从主构造函数初始化它们(如果您只有一个始终是主构造函数的构造函数):

  class Sum(var a:Int,var b:Int) {

    fun add(): Int {
        return a + b
    }
}

Refer constructors column in official documentation in link below https://kotlinlang.org/docs/reference/classes.html

https://kotlinlang.org/docs/reference/classes.html下面的链接中参考官方文档中的构造函数列

回答by Angel Koh

the following code shows you how to assign and manipulate variables in your class

以下代码向您展示了如何在类中分配和操作变量

class Sum (var a: Int,var b: Int){

    fun add(): Int= a + b //you can return the results directly.
}

you can test your code using the main below.

您可以使用下面的主要测试您的代码。

fun main(args: Array<String>) {
   var s= Sum(1,2)

    print(s.add())
}