【问题标题】:Unable to perform typecasting -incompatible type, int cannot be converted to c2无法执行类型转换 - 不兼容类型,int 无法转换为 c2
【发布时间】:2018-01-02 07:21:15
【问题描述】:

这是我写的一个简单的代码:

class c1
{
    int x = 10;
}
class c2 extends c1
{
    int x = 20;
}
class c3
{
    public static void main(String [] args)
    {
        c1 a = new c2(); 
        System.out.println(a.x); //This works and output is 10.
        System.out.println((c2)a.x); //Error is here
    }
}

错误显示,

incompatible type, int cannot be converted to c2

我确定代码是正确的。 由于引用 a 的类型转换为 c2(在显示错误的语句中),因此应该显示 20。

但是,没有发生类型转换。 为什么?

【问题讨论】:

  • 您的代码毫无意义。您正在尝试将 x 从 a (这是一个 int)转换为 c2 类型。

标签: java typecasting-operator


【解决方案1】:

应该是((c2) a).x

你需要有一个额外的括号才有意义。因为编译器认为您正在尝试将整个事情转换为 c2。

System.out.println(((c2) a).x);

【讨论】:

    【解决方案2】:

    这是运算符优先级(强制转换与点)的问题,但不幸的是 JLS 不认为这些是运算符,因此不会在表中公布它们的优先级。这是优先级:

    • new 运算符具有最高优先级:new A().foo(); 表示 (new A()).foo();
    • 点运算符的优先级高于强制转换运算符:(double)a.x 表示 (double)(a.x)

    Java 程序员不将它们称为“运算符”,但在其他语言中它们是。所以对于来自其他语言的读者来说,这是有道理的。

    【讨论】:

      【解决方案3】:

      目前,您正在尝试将 a.x 转换为整数 C2。 因为您需要将 c1 转换为 c2 尝试

         System.out.println((c2)a)).x;
      

      【讨论】:

        【解决方案4】:

        您没有使用(c2)a.x 将 c1 转换为 c2,而是将整数 x 转换为 c2,因此出现错误。

        【讨论】:

          【解决方案5】:

          首先,我想在这一行纠正你..

          我确定代码是正确的。

          不,代码不正确。

          我已经尝试逐步解释您的代码,将概念作为代码中的 cmets。

          public static void main(String [] args)
          {
              c1 a; // this is just a reference of Type 'c1', holding nothing.
              a = new c2(); // assigned new Object of Type 'c2', perfectly fine, no issues with polymorphism.
              /* but here,
               c2 has value of 'x' as 20;
               but when if you use the reference of 'a' (which is of type c1), i.e. 'c1.x' then JVM follows the reference.
               Meaning to say is, it will go to class 'c1' and print it's value of 'x' which is 10 & you are getting the same.
          
              */
              System.out.println((long)a.x); //This works and output is 10.
              /*
               your code, (c2)a.x
               Now, Problem starts here,
               'a.x' is of type 'int', and you are casting this 'int' to type 'c2',
               you will have to convert the reference of 'a' to 'c2'
               (i.e 'a' of type 'c1' to 'a' of type 'c2', not 'a.x' to type of 'c2').
               So the solution would be, ((c2)a) <---- this is now of type 'c2', now access it's member 'x', it will show you value 20.
          
          
               */
          
              System.out.println((c2)a.x); //Error is here
          }
          

          您应该阅读这两个与 java 中的强制转换相关的答案。

          1) https://stackoverflow.com/a/30776990

          2) https://stackoverflow.com/a/1302612

          【讨论】:

            猜你喜欢
            • 2017-10-03
            • 2017-03-29
            • 1970-01-01
            • 2018-07-03
            • 2017-03-18
            • 2018-05-28
            • 2017-03-27
            • 2017-02-13
            • 1970-01-01
            相关资源
            最近更新 更多