简体   繁体   English

Kotlin 中预期的属性 getter 或 setter

[英]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.我正在从官方文档中学习Kotlin ,我正在尝试创建一个 class 来进行算术运算。

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我有这个 class,现在我想创建一个 class 的 object

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

I am getting this error in Sum class:我在Sum class 中收到此错误:

Property getter or setter expected期望属性 getter 或 setter

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

var a: Int, b: Int;

Kotlin doesn't allow to have multiple declarations in one line. Kotlin不允许在一行中有多个声明。 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. Kotlin人员简单地发现C / java实践中的“int a,b,..”是他们希望在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. 如果只有一个,请为constructor编写简短形式。

If there are properties in class, they can be defined in constructor by using val or var . 如果类中有属性,则可以使用valvarconstructor定义它们。

Use it like following: 使用如下:

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

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

Do read Basic Syntax of Kotlin . 请阅读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: 你可以在这里引用这个帖子 ,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 . 一旦完成了单独行上的属性声明,你应该没有问题,因为kotlin genrates默认的getter和setter在内部具有与你的属性相同的可见性。 If you do not specify any visibility modifier, public is used by default, which means that your declarations will be visible everywhere . 如果未指定任何可见性修饰符,则默认使用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中的官方文档中的构造函数列

This error occurs because出现此错误是因为

  • Kotlin dont have support if you declare and even initialize 2 field variables on same line Kotlin 如果您在同一行声明甚至初始化 2 个字段变量,则不支持

  • There is default setter and getter associated with each field variable in kotlin kotlin中每个字段变量都有默认的setter和getter关联

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因此,当您在同一行声明它们时,第一个变量使用默认的 setter 和 getter 初始化,但另一个变量期望您提供自己的 setter 和 getter,因为语法错误

Error can be resolved via 2 ways错误可以通过两种方式解决

  • Declare setter and getter separately like this像这样分别声明setter和getter

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

     var a: Int var b: Int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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