Guy Bieber
03/11/2020, 6:12 PMabstract class Parent {
init {
print("Parent Init: ")
var str = getString()
println(" $str")
}
constructor () {
print("Parent Constructor: ")
var str = getString()
println(" $str")
}
protected abstract fun getString () : String
}
class Child : Parent {
var myString : String = "test"
init {
print("Child Init: ")
var str = getString()
println("$str")
}
constructor () {
print("Child Constructor: ")
var str = getString()
println("$str")
}
override fun getString() : String {
return myString
}
}
var child : Child = Child()
println("Main: ${child.myString}")
Example output
Parent Init: null
Parent Constructor: null
Child Init: test
Child Constructor: test
Main: test
araqnid
03/11/2020, 7:15 PMRuss Tennant
03/11/2020, 7:27 PMRuss Tennant
03/11/2020, 7:28 PMReports possible escapes of this during object construction. Escapes occur when this is used as a method argument or the object of an assignment in a constructor or initializer. Such escapes may result in subtle bugs, as the object is now available in a context in which it is not guaranteed to be initialized.
Guy Bieber
03/11/2020, 8:08 PMGuy Bieber
03/11/2020, 8:09 PMRuss Tennant
03/11/2020, 8:10 PMGuy Bieber
03/11/2020, 8:13 PM