| String s = new String ("Text here"); |
| String temp = "Text here"; String s = new String (temp); |
| String s = "Text here"; |
| import java.awt.Dimension; /***Example class.The x and y values should never*be negative.*/ public class Example{ private Dimension d = new Dimension (0, 0); public Example (){ } /*** Set height and width. Both height and width must be nonnegative * or an exception is thrown.*/ public synchronized void setValues (int height,int width) throws IllegalArgumentException{ if (height < 0 || width < 0) throw new IllegalArgumentException(); d.height = height; d.width = width; } public synchronized Dimension getValues(){ // Ooops! Breaks encapsulation return d; } } |
| Example ex = new Example(); Dimension d = ex.getValues(); d.height = -5; d.width = -10; |
| public synchronized Dimension getValues(){ return new Dimension (d.x, d.y); } |
| /*** 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()); } } |
| public synchronized Integer getValue(){ // ’i’ is immutable, so it is safe to return it instead of a copy. return i; } |
| 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]; } } |
| 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. } } |
| void saveCopy (int[] data){ copy = cloneArray ( data); } |
| import java.awt.Dimension; /*** Example class. The height and width values should never * be negative. */ public class Example{ static final public int TOTAL_VALUES = 10; private Dimension[] d = new Dimension[TOTAL_VALUES]; public Example (){ } /*** Set height and width. Both height and width must be nonnegative * or an exception will be thrown. */ public synchronized void setValues (int index, int height, int width) throws IllegalArgumentException{ if (height < 0 || width < 0) throw new IllegalArgumentException(); if (d[index] == null) d[index] = new Dimension(); d[index].height = height; d[index].width = width; } public synchronized Dimension[] getValues() throws CloneNotSupportedException{ return (Dimension[])d.clone(); } } |
| public synchronized Dimension[] getValues() throws CloneNotSupportedException{ Dimension[] copy = (Dimension[])d.clone(); for (int i = 0; i < copy.length; ++i){ // NOTE: Dimension isn’t cloneable. if (d[i] != null) copy[i] = new Dimension (d[i].height, d[i].width); } return copy; } |
| public void store (int[] data) throws CloneNotSupportedException{ this.data = (int[])data.clone(); // OK } |
| public void wrongStore (int[][] data) throws CloneNotSupportedException{ this.data = (int[][])data.clone(); // Not OK! } public void rightStore (int[][] data){ // OK! this.data = (int[][])data.clone(); for (int i = 0; i < data.length; ++i){ if (data[i] != null) this.data[i] = (int[])data[i].clone(); } } |
关注此文的读者还看过: