在TypeScript中执行继承时出错
我一直在试图理解typescript中的继承在TypeScript中执行继承时出错,typescript,Typescript,我一直在试图理解typescript中的继承 class user { name: string; email: string; constructor(name: string, email: string) { this.name = name; this.email = email; } checkUserData = id => { console.log( "This user name is" + th
class user {
name: string;
email: string;
constructor(name: string, email: string) {
this.name = name;
this.email = email;
}
checkUserData = id => {
console.log(
"This user name is" +
this.name +
"his email address is" +
this.email +
"and his ID is" +
id
);
};
}
class college extends user {
id: number;
constructor(name, email, id) {
super(name, email);
this.id = id;
}
checkUserData = () => {
super.checkUserData(this.id);
};
}
let newUser: user = new college("Rohit Bhatia", "iro@gmail.com", 4556);
console.log(newUser.checkUserData());
这是我的代码,这里我得到以下错误
index.ts:31:11-错误TS2340:仅用于
基类可以通过“super”关键字访问
31 super.checkUserData(this.id);
~~~~~~~~~~~~~
index.ts:37:13-错误TS2554:应为1个参数,但得到0
37 console.log(newUser.checkUserData());
~~~~~~~~~~~~~~~~~~~~~~~
索引.ts:10:19
10 checkUserData=id=>{
~~
未提供“id”的参数
在我的代码中,我看不到使用私有方法
,那么为什么我会出现这样的错误?而且,我知道我的代码中有这么多错误,有人能帮我修复吗
我的意图是什么?
在类用户中,
在父类(将其视为公共类)中创建一个新属性checkUserData,该属性以ID为参数,
从一个类调用它的子类并将id传递给它。这感觉像是两个独立的问题,但我将在这里回答这两个问题:
以下是我如何修改您的代码:
// non-primitive types should start with a capital letter by convention
class User {
name: string;
email: string;
constructor(name: string, email: string) {
this.name = name;
this.email = email;
}
// a method on the prototype, not an instance property
checkUserData(id: number) {
console.log(
"This user name is" +
this.name +
"his email address is" +
this.email +
"and his ID is" +
id
);
};
}
// non-primitive types should start with a capital letter by convention
class College extends User {
id: number;
// annotate types on function parameters
constructor(name: string, email: string, id: number) {
super(name, email);
this.id = id;
}
// a method on the prototype, not function property
checkUserData() {
super.checkUserData(this.id);
};
}
// do not widen to user type, let the compiler infer as college
let newUser = new College("Rohit Bhatia", "iro@gmail.com", 4556);
console.log(newUser.checkUserData());
注意:按照惯例,非原语类型以大写字母开头,因此我将user
更改为user
,将college
更改为college
。使用小写类名并不是无效的,但这违反了预期。如果需要,请随意将它们保留为小写
问题一:“为什么我不能叫超级” 回答一:你使用的是函数属性而不是方法。请使用方法 详细信息:我已更改
checkUserData()User
和College
中的都是a。这意味着它们被添加到User.prototype
和College.prototype
,而User
和College
的实例只需通过继承即可。现在,您可以使用this
和College.prototype.checkUserData()
。按照您定义方法的方式,这是不可能的。箭头函数没有自己的this
或super
上下文,并且User
和College
的每个实例都将获得箭头函数的副本,因此无法通过原型继承重写它。箭头函数是(正如书中所说)不适合作为方法
问题二:“为什么newUser.checkUserData()
给我一个错误?”
回答二:您已声明newUser
属于User
类型。请将其保留为未注释,或声明为College
类型
详细信息:当您说让新用户:User=…
时,您告诉编译器它是用户
,编译器不会试图确定它是否是更具体的类型,如学院
,即使您知道它是一种类型。因为用户
实例需要它的checkUserData()
方法要获取单个参数,您将得到一个错误。解决此问题的方法是让编译器知道newUser
实际上是一个College
。您可以通过注释它来显式执行此操作(让newUser:College=…
),或者您可以省去注释,通过查看newcollege(…)
的返回类型,让编译器知道它是College
实例
好的,希望能有帮助。祝你好运!这感觉像是两个不同的问题,但我将在这里回答这两个问题:
以下是我如何修改您的代码:
// non-primitive types should start with a capital letter by convention
class User {
name: string;
email: string;
constructor(name: string, email: string) {
this.name = name;
this.email = email;
}
// a method on the prototype, not an instance property
checkUserData(id: number) {
console.log(
"This user name is" +
this.name +
"his email address is" +
this.email +
"and his ID is" +
id
);
};
}
// non-primitive types should start with a capital letter by convention
class College extends User {
id: number;
// annotate types on function parameters
constructor(name: string, email: string, id: number) {
super(name, email);
this.id = id;
}
// a method on the prototype, not function property
checkUserData() {
super.checkUserData(this.id);
};
}
// do not widen to user type, let the compiler infer as college
let newUser = new College("Rohit Bhatia", "iro@gmail.com", 4556);
console.log(newUser.checkUserData());
注意:按照惯例,非原语类型以大写字母开头,因此我将user
更改为user
,将college
更改为college
。使用小写类名并不是无效的,但这违反了预期。如果需要,请随意将它们保留为小写
问题一:“为什么我不能叫超级”
回答一:你使用的是函数属性而不是方法。请使用方法
详细信息:我已更改checkUserData()User
和College
中的都是a。这意味着它们被添加到User.prototype
和College.prototype
,而User
和College
的实例只需通过继承即可。现在,您可以使用this
和College.prototype.checkUserData()
。按照您定义方法的方式,这是不可能的。箭头函数没有自己的this
或super
上下文,并且User
和College
的每个实例都将获得箭头函数的副本,因此无法通过原型继承重写它。箭头函数是(正如书中所说)不适合作为方法
问题二:“为什么newUser.checkUserData()
给我一个错误?”
回答二:您已声明newUser
属于User
类型。请将其保留为未注释,或声明为College
类型
详细信息:当您说让新用户:User=…
时,您告诉编译器它是用户
,编译器不会试图确定它是否是更具体的类型,如学院
,即使您知道它是一种类型。因为用户
实例需要它的checkUserData()
方法要获取单个参数,您将得到一个错误。解决此问题的方法是让编译器知道newUser
实际上是一个College
。您可以通过注释它来显式执行此操作(让newUser:College=…
),或者您可以省去注释,通过查看newcollege(…)
的返回类型,让编译器知道它是College
实例
好吧,希望你不会