Beginner question - Inheritance - why isn't my age constructor parameter used?

前端 未结 2 664
挽巷
挽巷 2020-12-22 05:40

Noob question here, I am working my way through a Udemy beginner Kotlin course and I can\'t work out why my age parameter isn\'t used when I use my derived class, but will w

相关标签:
2条回答
  • 2020-12-22 06:27

    What Umberto Cozzi said is right, it is to do with the fact that you're referring to an open value in the constructor. If you step through the code you'll see that the sequence of events is:

    1. You call the Student constructor, passing in a value for age.
    2. The first thing that constructor does (before constructing the Student object) is call the superclass's constructor.
    3. During the construction of the superclass (Person) you hit this line of code: val dob = thisyear - age. age is an open member (i.e. overridden in the subclass, Student). So the value should be retrieved from Student. The problem is that at this point the Student hasn't been fully constructed yet (remember the first thing its constructor did was call the superclass's constructor). So it's not possible yet for Person to ask Student what value age should be, as that value hasn't been set yet. If you step through the code and check out the value of age at this line of code, it's zero (the default value for an Int).

    So the question is what to do? And I think the thing you should ask is: why does Student override age (and indeed firstname and surname). Is there any different in behaviour of age, firstname and surname between Person and Student? The answer is probably no. So Student shouldn't override these properties: instead it should declare them simply as constructor parameters (without val or var) and pass those values to the base class. In other words, Student should look as follows:

    class Student(firstname: String, surname: String, age: Int, val studentID: Int) :
            Person(firstname, surname, age) {
        ...
    

    You might also want to be aware of the fact that your line of code that declares thisyear actually creates a property of Person, called thisyear, which I guess you don't want. Any val or var members that are declared directly in the class (rather than in a function) are a declaration of a property (and this is why this is all being calculated immediately during the construction of the Person object). So you might well want to inline this as:

    val dob = Calendar.getInstance().get(Calendar.YEAR) - age
    

    If the calculation is more complex and requires more lines of code, just create a private method (e.g. calculateDob) and call that, e.g. val dob = calculateDob(age)

    There's also the slight anomaly that age is a var (i.e. can change) whereas dob is a val (i.e. can't change). So a user could change the value of age, but dob won't be updated. One possible way to solve this is to change dob so that instead of it being a property which is assigned a (read-only) value during construction, make it a property with a getter which will calculate the value every time it's called, e.g.

    val dob
        get() = Calendar.getInstance().get(Calendar.YEAR) - age
    

    Another option is to add a backing field and getter/setter for age and update dob whenever age is updated.

    0 讨论(0)
  • 2020-12-22 06:39

    the documentation you are referring to, says explicitly "When designing a base class, you should therefore avoid using open members in the constructors, property initializers, and init blocks"

    this is exactly what is happening in your example. In the base class Person you use the open member age in the constructor (the line where you calculate the dob). This should be avoided because when that line calculating dob is executed, age did not yet receive a value from the derived class.

    Ok, I did not answer at the "How to correct this?" but I hope it helps in clarifying what is going on

    0 讨论(0)
提交回复
热议问题