首页 > 其他分享 >Kotlin Notes - 1

Kotlin Notes - 1

时间:2023-11-13 15:25:53浏览次数:31  
标签:name val Kotlin Notes constructor println open class

A class in Kotlin has a primary constructor and possibly one or more secondary constructors.

  // primary constructor
  class Person(val name: String) {
    val children: MutableList<Person> = mutableListOf()
    // secondary constructor
    constructor(name: String, parent: Person) : this(name) {
        parent.children.add(this)
    }
}

Code in initializer blocks effectively becomes part of the primary constructor. So the code in all initializer blocks and property initializers is executed before the body of the secondary constructor.

  class Constructors {
    init {
        println("Init block")
    }

    constructor(i: Int) {
        println("Constructor $i")
    }
}

To create an instance of a class, call the constructor as if it were a regular function:

  val invoice = Invoice()

  val customer = Customer("Joe Smith")

All classes Kotlin have a common superclass: Any, like Object in Java. Any has three methods: equals(), hashCode(), and toString().

By default, Kotlin classes are final. To make a class inheritable, mark it with the open keyword:

  open class Base // Class is open for inheritance

To declare an explicit supertype, place the type after a colon in the class header:

  open class Base(p: Int)

  class Derived(p: Int) : Base(p)

Kotlin requires explicit modifiers for overridable members and overrides:

  open class Shape {
    open fun draw() { /*...*/ }
    fun fill() { /*...*/ }
  }

  class Circle() : Shape() {
      override fun draw() { /*...*/ }
  }

Overriding properties:

  open class Shape {
    open val vertexCount: Int = 0
  }

  class Rectangle : Shape() {
      override val vertexCount = 4
  }
  
  // Note that you can use the override keyword as part of the property declaration in a primary constructor:
  class Rectangle(override val vertexCount = 4) : Shape {}

Base class initializes before derived class

  open class Base(val name: String) {

    init { println("Initializing a base class") }

    open val size: Int = 
        name.length.also { println("Initializing size in the base class: $it") }
  }

  class Derived(
      name: String,
      val lastName: String,
  ) : Base(name.replaceFirstChar { it.uppercase() }.also { println("Argument for the base class: $it") }) {

      init { println("Initializing a derived class") }

      override val size: Int =
          (super.size + lastName.length).also { println("Initializing size in the derived class: $it") }
  }

标签:name,val,Kotlin,Notes,constructor,println,open,class
From: https://www.cnblogs.com/otf-notes/p/17829178.html

相关文章

  • kotlin 内联函数 inline
    一、当函数被声明为内联函数(函数的前缀增加inline),那么函数体会被直接替换到函数被声明的地方,而不是被正常的调用。如下的代码inlinefunsynchronized(lock:Lock,action:()->Unit){lock.lock()try{returnaction()}finally{lock.unlo......
  • kotlin 高阶函数
    一、定义:以另一个函数作为参数或者返回值的函数1、kotlin中,函数以lambda或者函数引用来表示 二、函数类型1、如下是函数的类型上述声明了函数的类型,括号内包含了该函数类型需要传入的参数类型,紧接着箭头,最后是返回的类型(在声明函数类型时候,返回类型即使是Unit也不可以省略)......
  • 一文快速实战Kotlin协程与Flow
    前言不知道大家有没有跟我一样的感受:即使自己用心在网上学过协程和Flow了,但过了一段时间就又忘掉了。这大部分的原因其实是因为我们缺少实战。我平时工作里根本就接触不到协程和Flow,自己又不敢硬往上写,万一出问题了咋整?所以一直就处于理论学习阶段,导致我学了就跟没学一样。今天就带......
  • 知乎问题采集如此轻松,Kotlin来帮忙
    知乎是国内最好的一个知识学习的平台,我们平时很多问题都能在知乎上找到很好的答案。那么今天我就用Kotlin编写一段知乎问题收集的程序,我们可以根据自己需要的问题,进行针对性的采集,非常的不错,一起来看看吧。```kotlinimportokhttp3.OkHttpClientimportokhttp3.Requestimportja......
  • Kotlin协程学习——协程的基本介绍
    我们为什么需要学习Kotlin协程呢?我们已经有了成熟的JVM库,比如RxJava或Reactor。此外,Java本身就支持多线程,很多人也选择使用普通的回调函数。很明显,我们已经有了很多选项来执行异步操作。Kotlin协程提供了更多的功能。它们是一个概念的实现,该概念最早在1963年被描述,但等待了多年才......
  • kotlin 重载运算符
    一、二元运算符的重载1、常见的运算符有:加、减、乘、除、求余;我们要重载这些运算符的操作这里以加法重载运算符为例dataclassPoint(valx:Int,valy:Int){operatorfunplus(other:Point):Point{returnPoint(x+other.x,y+other.y)}}如上......
  • Kotlin-嵌套类_内部类_匿名内部类
    Kotlin-嵌套类&内部类&匿名内部类1.嵌套类类可以被嵌套在其它类中:classOuter{privatevalbar:Int=1classNested{funfoo(){println("fooinOuter#Nested#foo()")}funtest(){//println("Ne......
  • Kotlin语言基础入门到熟悉:Lambda 表达式
    什么是Lambda表达式?Lambda表达式,其实就是匿名函数。而函数其实就是功能(function),匿名函数,就是匿名的功能代码了。在Kotlin当中,函数也是作为类型的一种出现的,尽管在当前的版本中,函数类型的灵活性还不如Python这样的语言,不过它也是可以被赋值和传递的,这主要就体现在Lambda表......
  • Kotlin语言基础入门:Kotlin的常用写法
    Kotlin的常用写法1.方法参数的默认值可以给方法的参数指定默认值funsomeFunction(a:Int=0,b:String=""){/*方法实现*/}2.过滤列表找出列表中满足某个条件的所有元素。使用filter方法。其中x是自己定义的参数名。vallist=Arrays.asList(1,2,3,4,5,6,7)val......
  • Kotlin语言基础入门:Kotlin简介
    在2019年GoogleI/O大会上,Google宣布今后将优先采用Kotlin进行Android开发。一,简介Kotlin是一种富有表现力且简洁的编程语言,不仅可以减少常见代码错误,还可以轻松集成到现有应用中。Google列举的Kotlin的优势:富有表现力且简洁:可以使用更少的代码实现更多的功能。表达自己的......