Home > Net >  Unable to display the age of an object that was passed to a kotlin function
Unable to display the age of an object that was passed to a kotlin function

Time:10-12

I'm just starting to learn kotlin and ran into a problem:

I have a Person class that has two fields -age (Int data type) -name (data type String)

there is also a oldUp function where I pass a Person object and increment the object's age field by 10.

Before the end of the program ** I want to display the age of the object that was passed to oldUp ** However, age is not shown.

my code:

class Person(var name: String, var age: Int){
}

fun growOld(human: Person){
    human.age =10
}


fun main(args: Array<String>) {
    var human = Person("Kitty",6)
    growOld(human)
    println(human)
}

CodePudding user response:

If you want to print the age, you can just write: println(human.age).

In your example it might be cleaner to add the growOld method to your class so you can call it on the object. For example:

class Person(var name: String, var age: Int){
    fun growOld() {
        this.age  = 10
    }
}


fun main() {
    var human = Person("Kitty", 6)
    println(human.age) // prints 6
    human.growOld()
    println(human.age) // prints 16
    println(human.name) // prints Kitty
}

CodePudding user response:

The problem is you're trying to print the human object itself. Under the hood, this calls its toString() method - every class has one of these, because it's defined on the type all classes derive from. If you don't override it and provide a nice way to "pretty print" your object, it'll use the default implementation, which is basically a reference to the object in memory.

A lot of classes you use have a nice toString() implementation, e.g. if you print a List you get ["something", "that", "looks", "like", "this"]. But that behaviour needed to be coded in - and you need to do that for your Person class too!

So you can override the default implementation like this:

override fun toString(): String {
    // return a String here
}

override means you're taking an existing function and writing your own version of it to use instead - if this doesn't match an existing function you can override, you'll get an error. You'll also get an error if you don't use the override keyword for a function that looks exactly like an existing one in a supertype - it's just to make sure you don't accidentally do the wrong thing. In IntelliJ you can do Ctrl O to override existing functions if you like.


So you could do something like this:

// inside your Person class
override fun toString(): String {
    return "Name: $name, age: $age"
}

and then when you use it in a print statement, or in a string (like "Details: $person" or val details = "Details: " person) it will call that toString() method and get the string you produced.


Another way to approach this is to use a data class:

data class Person(var name: String, var age: Int)

A data class is a special kind of class where all your "data" goes in the constructor (as properties, either val or var), and then you get some boilerplate stuff for free which uses those properties (and only those properties). Things like an equals() and hashCode() implementation that uses that data - and the relevant thing here, it gives you a toString() implementation that pretty prints name and age. Try it out!

Data classes can be really handy for simple data objects like you have here - but in normal classes, overriding toString() yourself is the general way of doing things. And you can still override a data class's toString if you want - sometimes you might want a more complex representation, or nice formatting, or you might want to only include some properties and ignore others. You're in control of how it prints itself!

And if you just want to print the age property, or print anything at all using the data in your object, then you just need to do what Robin's answer says. You don't need a toString() implementation at all for that (and since this is how you usually use objects, often you won't need to write a toString for your own classes at all)

  • Related