复制代码
- //:ImmutableInteger.java
- //TheIntegerclasscannotbechanged
- importjava.util.*;
- publicclassImmutableInteger{
- publicstaticvoidmain(String[]args){
- Vectorv=newVector();
- for(inti=0;i<10;i++)
- v.addElement(newInteger(i));
- //Buthowdoyouchangetheint
- //insidetheInteger?
- }
- }///:~
复制代码
- //:MutableInteger.java
- //Achangeablewrapperclass
- importjava.util.*;
- classIntValue{
- intn;
- IntValue(intx){n=x;}
- publicStringtoString(){
- returnInteger.toString(n);
- }
- }
- publicclassMutableInteger{
- publicstaticvoidmain(String[]args){
- Vectorv=newVector();
- for(inti=0;i<10;i++)
- v.addElement(newIntValue(i));
- System.out.println(v);
- for(inti=0;i<v.size();i++)
- ((IntValue)v.elementAt(i)).n++;
- System.out.println(v);
- }
- }///:~
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |