Advertisement
Guest User

When to use deep cloning?

a guest
Aug 1st, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1.  
  2.  class Position{
  3.    int x,y;
  4.    Position(int x,int y){
  5.     this.x = x;
  6.     this.y = y;
  7.    }
  8.    
  9.    int getX() {
  10.      return x;
  11.    }
  12.    
  13.    int getY() {
  14.      return y;
  15.    }
  16.    
  17.    void setX(int x) {
  18.      this.x = x;
  19.    }
  20.    
  21.    void setY(int y) {
  22.       this.y = y;
  23.    }
  24.  }
  25.  
  26.  class Box{
  27.         Position pos;
  28.         Box(Position p){
  29.            pos = new Position(p);      
  30.         }
  31.         Position getPosition(){
  32.           return pos;
  33.         }
  34.    }
  35.    
  36. class Room{
  37.     Box box;
  38.     Room(Box box){
  39.         this.box = box;
  40.     }
  41. }
  42.    
  43.     public class Sample{
  44.       public static void main(String args[]){
  45.         Position pos = new Position(3,5);
  46.         Box box = new Box(pos);    
  47.         pos.setX(5);
  48.         System.out.println( box.getPosition().getX()); // Will return 5.
  49.       }
  50.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement