????Java??????????????
???????????? ???????[ 2012/11/13 11:38:36 ] ????????
????????????????3#???????????
?????????????????get??????÷????????????????????????????á????????????У?
/*** Example class.The value should never * be negative.*/ public class Example{
private Integer i = new Integer (0);
public Example (){
}
/*** Set x. x must be nonnegative* or an exception will be thrown*/
public synchronized void setValues (int x) throws IllegalArgumentException{ if (x < 0)
throw new IllegalArgumentException();
i = new Integer (x);
}
public synchronized Integer getValue(){
// We can’t clone Integers so we makea copy this way. return new Integer (i.intValue());
}
}
??????δ?????????????????????1#?????????????????????Integer??????String??????????????????????????????????????Integer???????????????????????????
????????getValue()????д???
public synchronized Integer getValue(){ // ’i’ is immutable?? so it is safe to return it instead of a copy. return i; }
????Java?????C++???????????????????JDK ????????????????????
????Boolean
????Byte
????Character
????Class
????Double
????Float
????Integer
????Long
????Short
????String
?????????Exception??????
???????????????4#??????????????????
????Java???????????飬???????????????????д???μ???????????????μ?????????????????飬???????Object??clone????????п???????
public class Example{ private int[] copy;
/*** Save a copy of ’data’. ’data’ cannot be null.*/ public void saveCopy (int[] data){
copy = new int[data.length];
for (int i = 0; i < copy.length;
++i) copy[i] = data[i];
}
}
??????δ?????????????????????????saveCopy()????????????????
void saveCopy (int[] data){ try{ copy = (int[])data.clone(); }catch (CloneNotSupportedException e){ // Can’t get here. } }
?????????????????飬??д???μ??????????????????????
static int[] cloneArray (int[] data){
try{
return(int[])data.clone();
}
catch(CloneNotSupportedException e){
// Can’t get here.
}
}
??????????????????saveCopy??????????????
void saveCopy (int[] data){
copy = cloneArray ( data);
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11