简体   繁体   English

如何从另一个父类方法调用父类方法

[英]How To Call Parent Class Method From Another Parent Class Method

So I have the following below. 所以我在下面有以下内容。 How do I make it where when I call rabbit.halfRun(); 当我打电话给rabbit.halfRun();时,我该怎么做呢rabbit.halfRun(); it runs the code in Animal.run() instead of Rabbit.run() 它在Animal.run()而不是Rabbit.run()运行代码

 class Animal { constructor(name) { this.speed = 0; this.name = name; } run(speed) { this.speed += speed; console.log(`${this.name} runs with speed ${this.speed}.`); } halfRun(){ var newSpeed = this.speed / 2; // expect to be run() from Animal class, not Rabbit class this.run(newSpeed) } } class Rabbit extends Animal { run(speed) { var speed = speed * 2; super.run(speed); } } let rabbit = new Rabbit("White Rabbit"); rabbit.run(5); // expect and get 10 rabbit.halfRun(); // expect 5 but I get 20 because it is running the run() from Rabbit 

The reason why you are getting 20 is because in your halfRun function: 你得到20的原因是因为你的halfRun函数:

function halfRun(){
  var newSpeed = this.speed / 2;
  this.run(newSpeed);
}

You are calling this.run(newSpeed) which is referring to the rabbit class' run method instead of the animal one since you are calling rabbit.halfRun() . 您正在调用this.run(newSpeed) ,它指的是兔子类的run方法而不是动物方法,因为您正在调用rabbit.halfRun() Therefore, it will be calling the rabbit class' implementation of run() since it has been overridden 因此,它将调用rabbit类的run()实现,因为它已被覆盖

Also, I believe you are setting up your halfRun() function wrong because although you halve the speed, you are still adding it onto the current speed value. 另外,我相信你设置的halfRun()函数是错误的,因为虽然你将速度减半,但你仍然将它添加到当前的速度值上。

To fix your issue, you can simply just set the speed in your half run function: 要解决您的问题,您只需在半跑功能中设置速度:

function halfRun(){
  this.speed=this.speed / 2;
}

because halfrun is calling both methods.'run' method of the rabbit class to multiply it with 2 and then 'run' of rabbit class is running super run of animal class. 因为halfrun正在调用两个方法。兔子类的'run'方法将它乘以2,然后兔子类的'run'运行超级动物类。 so both run methods are in action. 所以两种运行方法都在运行。

the issue is something like 问题是这样的

when rabbit.run() is called the speed variable holds the value of 10 and prints it 当调用rabbit.run()时,速度变量保持值10并打印它

speed = 10 速度= 10

when rabbit.halfRun() is called it will divide the value by 2 which means 10/2 is five which should be expected output 当调用rabbit.halfRun()时,它会将值除以2,这意味着10/2是5,这应该是预期的输出

before to the end of method you called the this.run(newSpeed)// this.run(5) 在方法结束之前你调用this.run(newSpeed)// this.run(5)

so it will call the run method in the rabbit class 所以它将调用rabbit类中的run方法

so speed = speed * 2 // 5 * 2 所以速度=速度* 2 // 5 * 2

which is 10 这是10

at last you are calling the super.run(speed) // super.run(10) 最后你打电话给super.run(速度)// super.run(10)

since previously rabbit.run() has been executed the speed holds the value 10 由于之前执行了rabbit.run(),速度保持值为10

so this.speed =+ speed // 10 =+ 10 which gives 20 所以this.speed = + speed // 10 = + 10,给出20

Its like function overriding where the actual method to be called is decided at the runtime based on the instance its pointing to. 它就像覆盖实际调用方法的函数一样,在运行时基于它指向的实例来决定。

so because the rabbit vairable is pointing to an object of class Rabbit and class Rabbit has overriding method run on it so even if you call the halfRun from parent class its actually calling the run method of class Rabbit . 所以,因为rabbit vairable指向的对象class Rabbitclass Rabbit具有压倒一切的方法run就可以了,所以即使你拨打halfRun从父类的实际调用run的方法class Rabbit its because the actual function to be called was decided at runtime based on the object the rabbit vairable is pointing to. 它因为实际函数的调用是在运行时基于对象的决定rabbit vairable指向。

So i think its expected result and its fine. 所以我认为它的预期结果和罚款。

but yes then in Javascript i guess there is no such keyword like self which should call the method of current class in time of inheritance. 但是,然后在Javascript中我猜没有像self这样的关键字应该在继承时调用当前类的方法。

I have added logs to describe the code hope you will understand why you got 20 as the result. 我添加了日志来描述代码,希望你能理解为什么你得到20的结果。 This is what happens to your speed, 这就是你的速度,
0 -> 5*2 -> 0+10 -> 10/2 -> 5*2 -> 10+10 -> 20 0 - > 5 * 2 - > 0 + 10 - > 10/2 - > 5 * 2 - > 10 + 10 - > 20

 class Animal { constructor(name) { this.speed = 0; this.name = name; } run(speed) { console.log(`Received ${speed} from Rabbit class`); console.log(`Adding ${speed} to ${this.speed}`); this.speed += speed; console.log(`Current speed at run(Animal class): ${this.speed}.`); } halfRun(){ console.log(`Deviding the current speed ${this.speed} by 2`); var newSpeed = this.speed / 2; console.log(`New speed passed to Rabbit class ${newSpeed}`); this.run(newSpeed) } } class Rabbit extends Animal { run(speed) { console.log(`Current speed: ${this.speed}`); console.log(`Multiplying ${speed} by 2`); var speed = speed * 2; console.log(`Passing ${speed} to Animal class`); super.run(speed); } } let rabbit = new Rabbit("White Rabbit"); rabbit.run(5); // expect 10 rabbit.halfRun(); // expect 20 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM