我对转换对象类型有些困惑。给出以下示例:

public class Test() {

 public void method1() { System.out.println("Method 1");
 public void method2() { System.out.println("Method 2");
 }

public class Test1() extends Test{

  @Override public void method1() { System.out.println("Method 11");
  @Override public void method2() { System.out.println("Method 22");
   public void method 3() { System.out.println("Method 3");

   public static void main(String[] args) {
       Test a = new Test1();
       a.method1(); //method invokes the overridden method1() of Test1, not Test the superclass
       a.method2(); //method invokes the overridden method2() of Test1, not Test the superclass
       a.method3(); //error, must cast == ((Test1)a).method3();
    }
}


我感到困惑的是,当我调用method1和method2时,编译器或JVM能够调用派生类或子类的方法,那么为什么仍需要向下转换/转换才能调用method3()?

我尝试重载超类的method1和method2,而不是重写它;在允许您调用重载的方法之前,编译器/ JVM将要求您转换对象引用变量。那么,这意味着没有向下转换,您只能调用超类的方法以及子类中定义的重写方法吗?

最佳答案

您的引用aTest类型,但是method3Test中不存在。

Test a = new Test1();
// ^-reference   ^-object
//   type          type


规则:


引用类型告诉我们哪些方法可见。
对象类型告诉我们考虑了哪些可见方法的实现。



这是我能想到的最基本的例子之一:

public class Animal
{
     public void speak()
     {
          System.out.println("BLARGHGH");
     }
}

public class Dog extends Animal
{
     @Override
     public void speak()
     {
          System.out.println("Woof");
     }
}

public class Cat extends Animal
{
     @Override
     public void speak()
     {
          System.out.println("Meow");
     }

     public void throwUpFurball()
     {
          System.out.println("So fluffy!");
     }
}

public class Test()
{
     public static void main(final String args[])
     {
          Animal animal1 = new Animal();
          animal1.speak(); // BLARGHGH
          animal1.throwUpFurball(); // Compilation error - method not found. It will ask for casting

          Animal animal2 = new Cat();
          animal2.speak(); // Meow
          ((Cat)animal2).throwUpFurball(); // Must be cast, because throwUpFurball does not exist in Animal

          Animal animal3 = new Dog();
          animal3.speak(); // Woof
          ((Cat)animal3).throwUpFurball(); // Compiles, but throws ClassCastException at runtime, because the object type of animal3 is Dog

          Cat animal4 = new Cat();
          animal4.speak(); // Meow
          animal4.throwUpFurball(); // No casting necessary, because animal4 is of type Cat

          Object animal5 = new Animal();
          animal5.speak(); // Again, compilation problem. The type Object does not contain the speak() API, so it will require casting.
     }
}

关于java - 转换对象类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20826028/

10-14 11:55
查看更多