AHK V2 班再添麻烦

问题描述 投票:0回答:1

我不确定我是否使用了错误的类,但任何帮助将不胜感激。我正在使用 Autohotkey V2,我正在尝试将一个类扩展到另一个类。

`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
  }
}`

我认为这样布置课程是有效的,因为每个老师和学生都是一个人,但老师和学生是不同的。因此,当我声明我的教师或学生课程时,我使用

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

这工作正常,但是,我不确定应该如何为每个人设置属性。

我尝试过研究,哈哈,但还没有找到解决方案。这让我觉得我做错了什么,但我不知道是什么。我还考虑过在 Person 类中声明属性,然后通过一种方法在 Teacher 和 Student 类中分配它们 `类人{ 名称:=“” 年龄:=“” }

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

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

但这会是高度重复的,因为我需要为每个属性执行此操作,然后最好删除 Person 类。

class autohotkey extends ahk2
1个回答
0
投票

#需要 AutoHotkey v2

`类人 { __新(信息) { this.name := 信息.name 这个.年龄 := 信息.年龄 } }

class Teacher 扩展 Person { __New(信息,主题) { 超级.__新(信息) this.subject := 主题 } }

Student 类扩展 Person { __New(信息,办公桌位置) { 超级.__新(信息) this.deskLocation := 桌面位置 } }

t1 := 老师({name: "Nikola",age: 100}, "Spanish") MsgBox(t1.名称“”t1.年龄) MsgBox(t1 是人) MsgBox(t1是老师)`

© www.soinside.com 2019 - 2024. All rights reserved.