Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Compose Generics Objects and Extensions #139

Open
BlackWhite7 opened this issue Oct 27, 2024 · 0 comments
Open

Android Compose Generics Objects and Extensions #139

BlackWhite7 opened this issue Oct 27, 2024 · 0 comments

Comments

@BlackWhite7
Copy link

Name of the Codelab or Codelab URL
https://developer.android.com/codelabs/basic-android-kotlin-compose-generics?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-3-pathway-1%3F_gl%3D1*12qd4xw*_up*MQ..*_ga*MTYzMTUzNzQwOC4xNzI5OTc5MTQ4*_ga_6HH9YJMN9M*MTcyOTk3OTE0Ny4xLjAuMTcyOTk3OTE0Ny4wLjAuNjczMjA3MDY1%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-generics&_gl=1*1pwa6jj*_up*MQ..*_ga*MTYzMTUzNzQwOC4xNzI5OTc5MTQ4*_ga_6HH9YJMN9M*MTcyOTk3OTE0Ny4xLjAuMTcyOTk3OTE0Ny4wLjAuNjczMjA3MDY1#4

Describe the problem
The example of a companion object is not good. Because a companion object allows you to access its properties and methods from inside the class containing it.
So, it is almost useless to access the properties outside the including class without making an instance.(here it is Quiz)
: the companion properties are kind of CONSTANTS!
fun main() {
println("${Quiz.answered} of ${Quiz.total} answered.")
}

But if you make an instance Quiz class and then try to access the companion properties, compile errors occur.
fun main() {
var quiz = Quiz()
println("${quiz.answered} of ${quiz.total} answered.")
}

In which lesson and step of the codelab can this issue be found?
Lesson number + step number.
[5. Use a singleton object]

How to reproduce?
The good example is like this. A companion singleton object must be used in the class containing it, like a property for more concise syntax.
Inside Quiz class, you can use the properties of StrudentProgress object like Quiz properties because it is a companion.

class Quiz {
val question1 = Question("Quoth the raven ___", "nevermore", Difficulty.MEDIUM)
....
companion object StudentProgress {
var total: Int = 10
var answered: Int = 3
}

//**here is an  example of the companion singleton object StudentProgress  *************
fun printResult() {
    println("$answered of $total are answered.")
}

}

//--------------------full code--------------------------
fun main() {
var quize = Quiz()

quize.printResult()

}

class Quiz {
val question1 = Question("Quoth the raven ___", "nevermore", Difficulty.MEDIUM)
val question2 = Question("The sky is green. True or false", false, Difficulty.EASY)
val question3 = Question("How many days are there between full moons?", 28, Difficulty.HARD)

companion object StudentProgress {
    var total: Int = 10
    var answered: Int = 3
}

//**here is the example of a companion singleton object *************
fun printResult() {
    println("$answered of $total are answered.")
}

}

// only class generator declaration type
data class Question(
var questionText: String,
var answer: T,
var difficulty: Difficulty
)
// enumeration class is a set class containing only Constants.
enum class Difficulty {
EASY, MEDIUM, HARD
}

Versions

  1. What version of Android Studio are you using?
  2. What API level are you targeting?

Additional information
Add any other context about the problem here.

codelab: basic-android-compose-training-add-scrollable-list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant