个人技术分享

安卓手机APP开发__Kotlin编程语言的面向对象开发介绍

概述

本文描述一下接口的编写,类的接口继承,类的属性和方法的编写

错误的代码的示例

fun main() {
   Quiz().printProgressBar()
}

interface ProgressPrintable {
    val progressText: String
    fun printProgressBar()
}

class Quiz : ProgressPrintable {
    val question1 = Question<String>("Quoth the raven ___", "nevermore", Difficulty.MEDIUM)
    val question2 = Question<Boolean>("The sky is green. True or false", false, Difficulty.EASY)
    val question3 = Question<Int>("How many days are there between full moons?", 28, Difficulty.HARD)
    
    override  val progressText: String
    get() = "${answered} of ${total} answered"
    override  fun printProgressBar() {
    repeat(Quiz.answered) { print("▓") }
    repeat(Quiz.total - Quiz.answered) { print("?") }
    println()
    println(Quiz.progressText)
      }
  companion object StudentProgress {
        var total: Int = 10
        var answered: Int = 3
     val progressText: String
     fun printProgressBar() {
    repeat(Quiz.answered) { print("▓") }
   repeat(Quiz.total - Quiz.answered) { print("?") }
    println()
    println(progressText)
}
    }
}

data class Question<T>(
    val questionText: String,
    val answer: T,
    val difficulty: Difficulty
)

enum class Difficulty {
    EASY, MEDIUM, HARD
}

总结

当一个类被定义为数据类时,如下的方法被实现了

    equals()
    hashCode(): 当你使用集合类型时你能看到这个方法
    toString()
    componentN(): component1(), component2(), 等等.
    copy()
注意的是一个数据类在它的构造函数中,至少有一个参数,并且所有的参数必须使用
val 或者是 var。一个数据类也不能是抽象类,内部类。

正确的示例的代码

interface ProgressPrintable {
    val progressText: String
    fun printProgressBar()
}
enum class Difficulty {
    EASY, MEDIUM, HARD
}
data class Question<T>(
    val questionText: String,
    val answer: T,
    val difficulty: Difficulty
)
class Quiz : ProgressPrintable {
    val question1 = Question<String>("Quoth the raven ___", "nevermore", Difficulty.MEDIUM)
    val question2 = Question<Boolean>("The sky is green. True or false", false, Difficulty.EASY)
    val question3 = Question<Int>("How many days are there between full moons?", 28, Difficulty.HARD)
    override  val progressText: String
    get() = "${answered} of ${total} answered"
    override  fun printProgressBar() {
    repeat(Quiz.answered) { print("▓") }
    repeat(Quiz.total - Quiz.answered) { print("?") }
    println()
    println(Quiz.progressText)
      }
    companion   object StudentProgress {
        var total: Int = 10
        var answered: Int = 3
      val progressText: String
    get() = "${answered} of ${total} answered"
      fun printProgressBar() {
    repeat(Quiz.answered) { print("▓") }
    repeat(Quiz.total - Quiz.answered) { print("?") }
    println()
    println(Quiz.progressText)
      }
    }
 
}
fun main() {
    val question1 = Question<String>("Quoth the raven ___", "nevermore", Difficulty.MEDIUM)
    val question2 = Question<Boolean>("The sky is green. True or false", false, Difficulty.EASY)
    val question3 = Question<Int>("How many days are there between full moons?", 28, Difficulty.HARD)
    println(question1.toString())
    println("${Quiz.answered} of ${Quiz.total} answered.")
     println(Quiz.progressText)

   Quiz.printProgressBar()
}

执行的结果

Question(questionText=Quoth the raven ___, answer=nevermore, difficulty=MEDIUM)

3 of 10 answered.

3 of 10 answered

▓▓▓???????

3 of 10 answered