classautohotkeyextendsahk2

AHK V2 Class extends troubles


I'm not sure if I'm using classes wrong, but any assistance would be appreciated. I'm using Autohotkey V2 and I'm trying to make a class extend to another class.

Class Person{
  __New(info){
    this.name := info.name
    this.age := info.age
  }
}

Class Teacher extends Person{
  __New(subject){
    this.subject := subject
  }
}

Class Student extends Person{
  __New(deskLocation){
    this.deskLocation := deskLocation
  }
}

I figure laying out the classes like this works because every Teacher and Student is a Person but Teachers and Students are different. So, when I declare my Teacher or Student class I use

T1 := Teacher("gym")
S1 := Student(11)

Which works fine, however, I'm not sure how I should be setting the properties for each Person.

I've tried researching, lol, but haven't found a solution. Which makes me think I'm doing something wrong, but I don't know what. I've also thought about declaring the properties in the Person Class and then assigning them in the Teacher and Student Classes through a method

Class Person{
    name := ""
    age := ""
}

Class Teacher extends Person{
  __New(subject, name, age){
    this.subject := subject
    this.Define("Name","Age")
  }

  Define(name, age) {
    this.name := name
    this.age := age
  }
}

but this would be highly repetitive because I would need to do it for each property then may as well just remove the Person class.


Solution

  • #Requires AutoHotkey v2
    
    class Person
    {
        __New(info)
        {
            this.name := info.name
            this.age  := info.age
        }
    }
    
    class Teacher extends Person
    {
        __New(info, subject)
        {
            super.__New(info)
            this.subject := subject
        }
    }
    
    class Student extends Person
    {
        __New(info, deskLocation)
        {
            super.__New(info)
            this.deskLocation := deskLocation
        }
    }
    
    t1 := Teacher({name: "Nikola",age: 100}, "Spanish")
    MsgBox(t1.name " " t1.age)
    MsgBox(t1 is Person)
    MsgBox(t1 is Teacher)