A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李哲 中级黑马   /  2012-4-11 10:58  /  1539 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

下面的代码,我注释的地方为什么张老师的可以编译通过,我的不可以呢?
难道是版本问题?


  1. import java.lang.reflect.*;


  2. class ReflectTest
  3. {       
  4.         public static void main(String[] args) throws Exception
  5.         {
  6.                
  7.                
  8.                
  9.                 int[] a1=new int[3];
  10.                 int[] a2=new int[4];
  11.                 int[][] a3=new int[2][3];
  12.                 String [] a4=new String [3];
  13.                
  14.                 System.out.println(a1.getClass() == a2.getClass());       
  15.                 //System.out.println(a1.getClass() == a3.getClass());
  16.                 //System.out.println(a1.getClass() == a4.getClass());
  17.                 System.out.println(a1.getClass().getSuperclass().getName());       
  18.                
  19.                
  20.                
  21.                
  22.                
  23.         }
  24. }

  25.        
复制代码

3 个回复

倒序浏览
我也遇到这种问题了,应该是版本的问题,现在的版本是,一个int型一维数组的class对象不能直接与int型多维数组的class对象或者一个string类型的class对象直接进行比较吧。
回复 使用道具 举报
程序给你改了一下,可以比较运行了

class ReflectTest
{        
        public static void main(String[] args) throws Exception
        {
               
               
               
                int[] a1=new int[3];
                int[] a2=new int[4];
                int[][] a3=new int[2][3];
                String [] a4=new String [3];
               
                System.out.println(a1.getClass() == a2.getClass());        
               System.out.println((a1.getClass()). equals (a3.getClass()));
                System.out.println((a3.getClass()). equals (a4.getClass()));
                System.out.println(a3.getClass().getSuperclass().getName());        
               
               
               
               
               
        }
}

        
回复 使用道具 举报
改为:
System.out.println((a1.getClass()). equals (a3.getClass()));
System.out.println((a3.getClass()). equals (a4.getClass()));
因为这样是比较内容是否相等的而
    System.out.println(a1.getClass() == a3.getClass());
    System.out.println(a1.getClass() == a4.getClass());
这样写的话是比较它们的地址是否相等因为这两个数在内存中的地址 是不相等的所以会编译不过去啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马