English 中文(简体)
如果我把一个子类当作它的超级类, 并称之为一种在子类中被压倒的方法, 它是执行被压倒的还是原始的方法?
原标题:If I ve cast a subclass as its superclass, and call a method that was overridden in the subclass, does it perform the overridden or original method?

考虑:

dog Animal 的子类, 而 dog 推翻了 Animal.eat ()

Animal[] animals = getAllAnimals();
for (int i = 0; i < animals.length; i++) {
    animals[i].eat();
}

如果 Animal.eat () dog.eat () 所覆盖, 当该方法从类型 Animal ( animals[i] ) 的标识符中被调用时, 则该方法被称为哪一种? )

问题回答

亚类方法将被称为“亚类方法 ” 。 这是< a href='" http://docs. oracle.com/javase/tuative/java/ IandI/polymodrophism. html' rel=“ noreferrer''>polyformism 的美。

子类是唯一的方法调用, 除非子类调用像这样的超级类 :

class Dog {
  public eat() {
     super.eat();
  }
}

代码代码

Animal a = new Dog();
a.eat();

将会调用 dogs eat 方法。 但请注意! 如果您有的话

class Animal {
  public void eat(Animal victim) { 
    System.out.println("Just ate a cute " + victim.getClass().getSimpleName()); 
  }
}

并且您有一个猫 来定义另外一种方法:

class Cat extends Animal {
  public void eat(Mouse m) { System.out.println("Grabbed a MOUSE!"); }
}

然后用它们:

Animal cat = new Cat();
Animal mouse = new Mouse();
cat.eat(mouse);

这将打印“ 只是吃了一只可爱的老鼠 ”, 而不是“ 发明了一个摩擦!! ” 。 为什么? 因为多形态主义只对圆点左侧的物体起作用, 使用方法被引用 。

它会调用版本 在子类。

如果你能绕过一个被分类为超级类的物体, 而不是得到分类方法的话, 继承是完全没有用处的!

http://homepage1.nifty.com/algafield/sscce.html" rel=“nofollow”>sscce

/**
 * @author fpuga http://conocimientoabierto.es
 * 
 * Inheritance test for http://stackoverflow.com/questions/10722447/
 *
 */


public class InheritanceTest {

    public static void main(String[] args) {
    Animal animals[] = new Animal[2];
    animals[0] = new Animal();
    animals[1] = new Dog();

    for (int i = 0; i < animals.length; i++) {
        animals[i].eat();

        if (animals[i] instanceof Dog) {
        System.out.println("!!Its a dog instance!!");
        ((Dog) animals[i]).eat();
        }
    }

    }


    private static class Animal {
    public void eat() {
        System.out.println("I m an animal");
    }
    }

    private static class Dog extends Animal {
    @Override
    public void eat() {
        System.out.println("I m dog");
    }
    }

}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签