英文:
I dont understand this java inheritance example
问题
以下是翻译好的部分:
我一直在学习 mooc.fi 的课程,对于继承的理解感到有些困难。特别是这个例子。
这个程序会打印什么?
public class Counter {
public int addToNumber(int number) {
return number + 1;
}
public int subtractFromNumber(int number) {
return number - 1;
}
}
public class SuperCounter extends Counter {
@Override
public int addToNumber(int number) {
return number + 5;
}
}
public static void main(String[] args) {
Counter counter = new Counter();
Counter superCounter = new SuperCounter();
int number = 3;
number = superCounter.subtractFromNumber(number);
number = superCounter.subtractFromNumber(number);
number = counter.addToNumber(number);
System.out.println(number);
}
显然这不是 6。需要一些帮助。
英文:
I have been doing a mooc.fi course and im having a hard time understanding inheritance. This example in particular.
What does the program print?
public class Counter {
public int addToNumber(int number) {
return number + 1;
}
public int subtractFromNumber(int number) {
return number - 1;
}
}
public class SuperCounter extends Counter {
@Override
public int addToNumber(int number) {
return number + 5;
}
}
public static void main(String[] args) {
Counter counter = new Counter();
Counter superCounter = new SuperCounter();
int number = 3;
number = superCounter.subtractFromNumber(number);
number = superCounter.subtractFromNumber(number);
number = counter.addToNumber(number);
System.out.println(number);
}
Apparently this isnt 6.
Some help would be appreciated.
答案1
得分: 3
结果是 2。让我们看一下。
Counter counter = new Counter();
给我们一个具有addToNumber
和subtractFromNumber
方法的counter
。Counter superCounter = new SuperCounter();
给我们一个具有addToNumber
方法的superCounter
。int number = 3;
number = superCounter.subtractFromNumber(number);
由于superCounter
没有覆盖旧的subtractFromNumber
方法,它会减去 1。现在number == 2
。number = superCounter.subtractFromNumber(number);
再次相同,减去 1,number == 1
。number = counter.addToNumber(number);
此方法会加上 1。现在number == 2
。
结果是我们预期的 2
。
英文:
The result is 2. Let's see.
Counter counter = new Counter();
gives us acounter
withaddToNumber
andsubtractFromNumber
methods.Counter superCounter = new SuperCounter();
gives us asuperCounter
withaddToNumber
method.int number = 3;
number = superCounter.subtractFromNumber(number);
Since superCounter does not override the old subtractFromNumber, it subtracts 1. Nownumber == 2
.number = superCounter.subtractFromNumber(number);
Same again, subtract 1,number == 1
.number = counter.addToNumber(number);
This method adds 1. Nownumber == 2
.
The result is 2
, as we expected.
答案2
得分: 1
你已经创建了2个类,其中一个继承自另一个(SuperCounter从Counter继承),这意味着它继承了一个方法“subtractFromNumber”,并且覆盖了另一个方法“addToNumber”。
接下来,你创建了这些类的实例“counter”和“superCounter”,同时在类“superCounter”的实例上调用方法“subtractFromNumber” 2次,在类“counter”的另一个实例上调用方法“addToNumber”。但由于你在类“counter”的实例上调用了方法“addToNumber”,因此实际上调用了类“Counter”的方法。
根据代码,你得到答案“2”:
- int number = 3; // 3
- number = superCounter.subtractFromNumber(number); // 3 - 1 = 2
- number = superCounter.subtractFromNumber(number); // 2 - 1 = 1
- number = counter.addToNumber(number); // 1 + 1 = 2
- System.out.println(number); // 2
英文:
You have created 2 classes, and one inherits from the other (SuperCounter from Counter), which means that it inherits one method "subtractFromNumber" and overrides the other method "addToNumber".
Next, you create instances of these classes "counter" and "superCounter", while calling the method "subtractFromNumber" on the instance of the class "superCounter" 2 times and the method "addToNumber" on the other instance of the class "counter". But since you call the method "addToNumber" on the instance of the class "counter", respectively, the method of the class "Counter" is called.
According to the code, you get the answer "2":
- int number = 3; // 3
- number = superCounter.subtractFromNumber(number); // 3 - 1 = 2
- number = superCounter.subtractFromNumber(number); // 2 - 1 = 1
- number = counter.addToNumber(number); // 1 + 1 = 2
- System.out.println(number); // 2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论