黑马程序员技术交流社区
标题:
和张老师写的代码一样却报错
[打印本页]
作者:
§傻、才乖
时间:
2013-12-20 17:17
标题:
和张老师写的代码一样却报错
int[] a1 = new int[3];
int[][] a2 = new int[3][];
String[] a3 = new String[3];
System.out.println(a1.getClass() ==a2.getClass());
System.out.println(a1.getClass()==a3.getClass());
复制代码
两条输出语句报错
Multiple markers at this line
- Incompatible operand types Class<capture#5-of ? extends int[]> and Class<capture#6-of ? extends
int[][]>
- Incompatible operand types Class<capture#5-of ? extends int[][]> and Class<capture#6-of ?
extends int[]>
- Line breakpoint:ReflectTest [line: 42] - main(String[])
作者:
熊志伟
时间:
2013-12-20 17:38
System.out.println(a1.getClass() ==a2.getClass());
System.out.println(a1.getClass()==a3.getClass());
复制代码
输出语句有问题:
再看下是不是前面有条件语句
作者:
§傻、才乖
时间:
2013-12-20 17:47
熊志伟 发表于 2013-12-20 17:38
输出语句有问题:
再看下是不是前面有条件语句
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String str = "abcd";
Class<?> clazz = Class.forName("java.lang.String");
Constructor<?> constructor = clazz.getConstructor(StringBuilder.class);
str = (String) constructor.newInstance(new StringBuilder("abc"));
System.out.println(str.charAt(1));
ReflectPoint pt = new ReflectPoint(3, 5);
String variableName = "y";
Field field = getField(pt, variableName);
System.out.println(field.get(pt));
char oldChar = 'b';
char newChar = 'a';
changeStringVal(pt, oldChar, newChar);
System.out.println(pt);
Method methodCharAt = String.class.getMethod("charAt", int.class);
System.out.println(methodCharAt.invoke(str, 2));
System.out.println(methodCharAt.invoke(str, new Object[] { 1 }));
String startingClassName = args[0];
Method mainMethod = Class.forName(startingClassName).getMethod("main",String[].class);
mainMethod.invoke(null, (Object) new String[] { "aaa", "bbb", "ccc" });
int[] a1 = new int[3];
int[][] a2 = new int[3][];
String[] a3 = new String[3];
System.out.println(a1.getClass() ==a2.getClass());
System.out.println(a1.getClass()==a3.getClass());
}
复制代码
作者:
熊志伟
时间:
2013-12-20 21:40
§傻、才乖 发表于 2013-12-20 17:47
JDK版本问题造成的:对于数组类型的变量,编译器在编译时,其实已经知道类型了,编译器认为这两个类型是不一样的,就报错。你改下JDK版本试下
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2