You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
What version of Android Studio are you using?
What API level are you targeting?
Additional information
Add any other context about the problem here.
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
}
}
//--------------------full code--------------------------
fun main() {
var quize = Quiz()
}
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)
}
// 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
Additional information
Add any other context about the problem here.
codelab: basic-android-compose-training-add-scrollable-list
The text was updated successfully, but these errors were encountered: