通过超级单词

时间:2016-12-16 14:35:03

标签: java

阅读blog时遇到问题。以下让我困惑了一段时间。

父:

class Insect {
private int size;
private String color;

public Insect(int size, String color) {
    this.size = size;
    this.color = color;
}

//getter, setter

public void move() {
    System.out.println("Move");
}

public void attack() {
    move(); //assuming an insect needs to move before attacking
    System.out.println("Attack");
}
}

子:

class Bee extends Insect {
public Bee(int size, String color) {
    super(size, color);
}

public void move() {
    System.out.println("Fly");
}

public void attack() {
    move();
    super.attack();
}

主要:

public static void main(String[] args) {
    Insect i = new Bee(1, "red");
    i.attack();
}

为什么结果是“飞行攻击”而不是“飞行攻击”? 调用super.attack(),应该调用Insect类的move()方法,不应该吗?

0 个答案:

没有答案