我不理解这个Java继承示例。

huangapple go评论156阅读模式
英文:

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。让我们看一下。

  1. Counter counter = new Counter(); 给我们一个具有 addToNumbersubtractFromNumber 方法的 counter
  2. Counter superCounter = new SuperCounter(); 给我们一个具有 addToNumber 方法的 superCounter
  3. int number = 3;
  4. number = superCounter.subtractFromNumber(number); 由于 superCounter 没有覆盖旧的 subtractFromNumber 方法,它会减去 1。现在 number == 2
  5. number = superCounter.subtractFromNumber(number); 再次相同,减去 1,number == 1
  6. number = counter.addToNumber(number); 此方法会加上 1。现在 number == 2

结果是我们预期的 2

英文:

The result is 2. Let's see.

  1. Counter counter = new Counter(); gives us a counter with addToNumber and subtractFromNumber methods.
  2. Counter superCounter = new SuperCounter(); gives us a superCounter with addToNumber method.
  3. int number = 3;
  4. number = superCounter.subtractFromNumber(number); Since superCounter does not override the old subtractFromNumber, it subtracts 1. Now number == 2.
  5. number = superCounter.subtractFromNumber(number); Same again, subtract 1, number == 1.
  6. number = counter.addToNumber(number); This method adds 1. Now number == 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”:

  1. int number = 3; // 3
  2. number = superCounter.subtractFromNumber(number); // 3 - 1 = 2
  3. number = superCounter.subtractFromNumber(number); // 2 - 1 = 1
  4. number = counter.addToNumber(number); // 1 + 1 = 2
  5. 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":

  1. int number = 3; // 3
  2. number = superCounter.subtractFromNumber(number); // 3 - 1 = 2
  3. number = superCounter.subtractFromNumber(number); // 2 - 1 = 1
  4. number = counter.addToNumber(number); // 1 + 1 = 2
  5. System.out.println(number); // 2

huangapple
  • 本文由 发表于 2020年10月28日 00:59:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64559343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定