java - 转换对象类型

标签 java casting

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

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( )?

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

最佳答案

您的引用a的类型为Test,但Test中不存在method3

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/

相关文章:

java - 目录上下文 : Active Directory Ldap request: get groups of user with parent groups

c - 如何将结构转换为 uint8_t(错误 : conversion to non-scalar type requested)

Python - 将 float 的商安全转换为 int

casting - D 中的枚举类型安全

c++ - 将 unsigned short* 转换为 int*

java - ArrayList 值被覆盖?

Java 8 流从元素返回列表

java - -XX+UseCMSCompactAtFullCollection 的确切用途是什么?

JAVA 互动经纪商 API

c - printf 从 char 到 int 的转换太聪明了?