Java – expected property getter or setter in kotlin

I'm learning kotlin from the official documentation. I'm trying to create a class to perform 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 the object of this class

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

I received this error in the sum class:

In B: int; In line VaR, a: int, B: int;

resolvent:

You are writing unnecessary code in class

If there is only one, write a short form for the constructor

If there are properties in the class, you can define them in the constructor using Val or var

Use the following:

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

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

Read kotlin's basic syntax

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>