账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    ts中继承后的this和super的问题
    74
    0
    首先我drawImgListArea是对父类的重写,在这块我想调用父类的drawImgListContent方法,但是我看了看这个时候我使用this和super都能成功调用,因为子类继承了父类所以this指向子类也能找到drawImgListContent,那这个super有什么用呢
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 蜜雪冰爽 普通会员 1楼

      在 TypeScript 中,当一个子类继承自一个父类时,thissuper 属性的值会发生一些变化。这是因为在 TypeScript 中,父类和子类之间存在继承关系,这种关系通过 extends 关键字来定义。当子类继承自父类时,子类将继承父类的所有属性和方法,包括 thissuper 属性。

      this 属性中,this 在父类的构造函数或方法调用中默认是当前对象,如果 this 是一个未定义的对象(例如 undefinednull),则会在当前对象上执行 this 的默认行为,例如调用 this.namethis.toString() 方法。如果 this 是一个已定义的对象(例如 new MyClass()),则thisthis 值会是子类对象。

      super 属性中,super 在父类的构造函数或方法调用中是作为指针传递的,它的值取决于父类的构造函数或方法的参数类型。如果父类的构造函数或方法没有接受 super 参数,那么 super 的值将是 null。如果父类的构造函数或方法接受 super 参数,那么 super 的值将是子类对象的引用,可以用来指向父类的实例变量或方法。

      以下是一个简单的示例,展示 thissuper 在子类继承父类时的值变化:

      ```typescript class Parent { constructor(private name: string) {}

      public getName(): string {
          return this.name;
      }
      

      }

      class Child extends Parent { constructor(private name: string, super: Parent) { super(name); }

      public getName(): string {
          return super.getName();
      }
      

      }

      let parent = new Child("John", null); console.log(parent.getName()); // 输出 "John"

      let child = new Child("Jane", Child); console.log(child.getName()); // 输出 "Child" console.log(child.super.getName()); // 输出 "Parent" ```

      在这个示例中,我们首先定义了一个 Parent 类,它有一个 name 属性和一个 getName 方法。然后,我们定义了一个 Child 类,它继承自 Parent 类,并在 getName 方法中使用 super 属性来指向 Parent 类的 getName 方法。在 Child 类的构造函数中,我们传递了一个 Parent 类的实例 parent 作为参数,所以 thisthis 值是 parent。然后,我们调用 getName 方法,并打印出 parentname,和 Childname,以及 Parent 类的 getName 方法的返回值,即 parentname

      因此,当子类继承自父类时,thissuper 的值会发生变化,thisthis 值会是子类对象,super 的值会是子类对象的引用,指向父类的实例变量或方法。

    • 琉璃〆玥倾城╮ 普通会员 2楼

      在 TypeScript 中,当一个子类继承自一个父类时,thissuper 属性的值会发生一些变化。这是因为在 TypeScript 中,父类和子类之间存在继承关系,这种关系通过 extends 关键字来定义。当子类继承自父类时,子类将继承父类的所有属性和方法,包括 thissuper 属性。

      this 属性中,this 在父类的构造函数或方法调用中默认是当前对象,如果 this 是一个未定义的对象(例如 undefinednull),则会在当前对象上执行 this 的默认行为,例如调用 this.namethis.toString() 方法。如果 this 是一个已定义的对象(例如 new MyClass()),则thisthis 值会是子类对象。

      super 属性中,super 在父类的构造函数或方法调用中是作为指针传递的,它的值取决于父类的构造函数或方法的参数类型。如果父类的构造函数或方法没有接受 super 参数,那么 super 的值将是 null。如果父类的构造函数或方法接受 super 参数,那么 super 的值将是子类对象的引用,可以用来指向父类的实例变量或方法。

      以下是一个简单的示例,展示 thissuper 在子类继承父类时的值变化:

      ```typescript class Parent { constructor(private name: string) {}

      public getName(): string {
          return this.name;
      }
      

      }

      class Child extends Parent { constructor(private name: string, super: Parent) { super(name); }

      public getName(): string {
          return super.getName();
      }
      

      }

      let parent = new Child("John", null); console.log(parent.getName()); // 输出 "John"

      let child = new Child("Jane", Child); console.log(child.getName()); // 输出 "Child" console.log(child.super.getName()); // 输出 "Parent" ```

      在这个示例中,我们首先定义了一个 Parent 类,它有一个 name 属性和一个 getName 方法。然后,我们定义了一个 Child 类,它继承自 Parent 类,并在 getName 方法中使用 super 属性来指向 Parent 类的 getName 方法。在 Child 类的构造函数中,我们传递了一个 Parent 类的实例 parent 作为参数,所以 thisthis 值是 parent。然后,我们调用 getName 方法,并打印出 parentname,和 Childname,以及 Parent 类的 getName 方法的返回值,即 parentname

      因此,当子类继承自父类时,thissuper 的值会发生变化,thisthis 值会是子类对象,super 的值会是子类对象的引用,指向父类的实例变量或方法。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部