Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

why compiler kotlin with error message Conflicting overloads problem

I am a beginner in Kotlin. I have a kt file that no class function name "test1" which is the same as the class name. android studio compiler display error message.

Kotlin: Conflicting overloads: public constructor test1() defined in com.demo.test1, public fun test1(): Unit defined in com.demo.

Why can’t class name and function name be the same? they are essentially different in nature,
or how to correct them?

test1.kt

package com.demo

class test1() {
    init { 
        println("First initializer ");
    }
}

main.kt

package com.demo

fun test1() {
    var s1: String? = "10.25"
    println(s1) 
}

fun main(argus: Array<String>) {
    test1();
}
question from:https://stackoverflow.com/questions/65887884/why-compiler-kotlin-with-error-message-conflicting-overloads-problem

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It basically says that you have two functions with the same definition in the current context: test1() and the constructor of the test1 class.

To solve this problem you can rename your class into Test1 (capital t). This is also one of the general programming rules: class names must start with a capital letter and should follow the PascalCase type https://www.chaseadams.io/posts/most-common-programming-case-types/#camelcase

In the Kotlin documentation you also have a coding conventions guide: https://kotlinlang.org/docs/reference/coding-conventions.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...