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+"";
}
}
测试类