Constuctors
// File: Main.kt
// Programmer: Engineer Nolverto Urias Obeso
// Creation Date: 06/17/2023
// Email: nolvertou@gmail.com
// Description: Learning how to use constructors and init
package Aquarium
fun main() {
buildAquarium()
fishExample()
}
fun buildAquarium() {
val myAquarium = Aquarium()
println("Length: ${myAquarium.length} cm, " +
"Width: ${myAquarium.width} cm, " +
"Height: ${myAquarium.height} cm")
myAquarium.height = 80
println("Height: ${myAquarium.height} cm")
println("Volume: ${myAquarium.height} liters")
val smallAquarium = Aquarium(length = 20, width = 15, height = 30)
println("Small Aquarium: ${smallAquarium.volume} liters with " +
"Length: ${smallAquarium.length} cm, " +
"Width: ${smallAquarium.width} cm, " +
"Height: ${smallAquarium.height} cm" )
val smallAquarium2 = Aquarium(numberOfFish = 9)
println("Small Aquarium 2: ${smallAquarium2.volume} liters with " +
"Length: ${smallAquarium2.length} cm, " +
"Width: ${smallAquarium2.width} cm, " +
"Height: ${smallAquarium2.height} cm")
}
fun fishExample(){
val fish = Fish(true, 20)
println("Is the fish friendly? ${fish.friendly}. It needs volume ${fish.size}")
}
package Aquarium
// Most of the classes only provide a primary constructor
class Aquarium(var length: Int = 100, var width: Int = 20, var height: Int = 40) { // Primary Constructor
var volume: Int
get() = width * height * length / 1000 // It calculates the volume
set(value){ height = (value * 1000) / (width * length)} // It sets the height giving the volume value
var water = volume * 0.9 // It's used the 90% of the tank volume
// The arguments have to match exactly the with one of the available constructors
// If you declare a secondary constructor it must containt a call to the primary constructor by using this()
constructor(numberOfFish:Int): this(){ // Secondary constructor
val water:Int = numberOfFish * 2000 //cm3
val tank: Double = water + water * 0.1
height = (tank/(length*width)).toInt() // Calculates the height depending of number of fish
}
}
package Aquarium
class Fish(val friendly: Boolean = true, volumeNeeded : Int) {
val size: Int
// Note: You can have more than 1 init{} block, and they always run before secondary constructors
// Init can be almost anywhere in your class
// They are executed in order, from the top of the class to the bottom
// So, if you were using any properties they need to come before the init blocks
init{
println("first init block")
}
// Calculate the size of that object creation time
// Init works just like a constructor
init {
if(friendly){
size = volumeNeeded
}else{ // fish is aggressive
size = volumeNeeded * 2
}
}
init {
println("constructed fish of size $volumeNeeded final size ${this.size}")
}
init{
println("last init block")
}
// To write clean kotlin code just use one primary constructor and init blocks
// When you need more flexibility consider adding helper functions like make default fish
constructor(): this(true, 9){
println("running secondary constructor")
}
}
Comments
Post a Comment