// Animals.kt Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2020-10-27 File created. // Demonstrating a class that has both the primary and a secondary // constructor. class Animal( val species_name : String ) // primary constructor { var stomach_contents : String = "" // The following secondary constructor must call the primary constructor. constructor( another_animal : Animal ) : this( another_animal.species_name ) { stomach_contents = another_animal.stomach_contents } fun feed( food_for_this_animal : String ) { stomach_contents = stomach_contents + food_for_this_animal + ", " } fun make_speak() { print( "\n Hello, I am a " + species_name + "." + "\n I have eaten: " + stomach_contents + "\n" ) } } fun main() { var cat_object = Animal( "cat" ) var dog_object = Animal( "vegetarian dog" ) cat_object.feed( "fish" ) cat_object.feed( "chicken" ) dog_object.feed( "tomatoes" ) dog_object.feed( "potatoes" ) var another_cat = Animal( cat_object ) another_cat.feed( "milk" ) cat_object.make_speak() dog_object.make_speak() another_cat.make_speak() print( "\n" ) }