简体   繁体   中英

Property getter or setter expected in Kotlin

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

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 Sum class:

Property getter or setter expected

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

var a: Int, b: Int;

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

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 not support in Kotlin.

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
}

You are writing unnecessary code in your class.

Write short form for constructor if there is only one.

If there are properties in class, they can be defined in constructor by using val or var .

Use it like following:

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

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

Do read Basic Syntax of Kotlin .

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())
}

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

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.

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 .

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):

  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

This error occurs because

  • Kotlin dont have support if you declare and even initialize 2 field variables on same line

  • There is default setter and getter associated with each field variable in kotlin

so when you declared them on same line, the first variable initialized with default setters and getters but the other one expects that you will give your own setter and getter because syntax was wrong

Error can be resolved via 2 ways

  • Declare setter and getter separately like this

     var a:Int get() { return 0 } set(value) { val b = value }
  • Correct the syntax

     var a: Int var b: Int

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM