Integer是int封装类,它可以直接Integer a=1; 创建对象。
查询资料,Integer a=1;实际上执行的是Integer a=Integer.valueOf(1)
我参考Integer的源代码也创建了个Integer类,可是为何不能直接赋值?
自己创建的Integer类
public class Integer extends Number implements Comparable<Integer> {
//Integer的值
private static int value;
/**
* 构造方法用来 new创建对象
* @param v
*/
public Integer (int v){
this.value=v;
}
/**
* 静态方法,用来直接=赋值( 可是就是不管用。求解决)
* @param value
* @return
*/
public static Integer valueOf(int value){
return new Integer(value);
}
/**
* 以下都是抽象类的抽象方法的实现,和接口的方法的实现,
*/
public int compareTo(Integer o) {
return (int) this.value;
}
public int intValue() {
return (int) this.value;
}
public long longValue() {
return this.value;
}
public float floatValue() {
return this.value;
}
public double doubleValue() {
return this.value;
}
public String toString() {
return this.value+"";
}
}
测试类
不是不能赋值,你的jdk可能比较低,切换高的。
你是说integer没直接复制么?升级一下jdk看看底层代码,没关注过
这是我的jdk版本
我觉得跟版本没有关系,,提示报什么错误了么?肯定是没有转换成功啊,你肯定也看了原来的源代码参考写的,不应该有大问题,会不会是没有调用你的那个
类?一般是默认java.lang.*包
//Integer的值
private static int value;
不应该定义为static吧。