51 lines
1.1 KiB
Java
Raw Normal View History

2015-05-05 11:20:13 -07:00
//: references/Snake.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-05-05 11:20:13 -07:00
// Tests cloning to see if destination
// of references are also cloned.
public class Snake implements Cloneable {
private Snake next;
private char c;
// Value of i == number of segments
public Snake(int i, char x) {
c = x;
if(--i > 0)
next = new Snake(i, (char)(x + 1));
}
public void increment() {
c++;
if(next != null)
next.increment();
}
@Override
public String toString() {
String s = ":" + c;
if(next != null)
s += next.toString();
return s;
}
@Override
public Object clone() {
Object o = null;
try {
o = super.clone();
} catch(CloneNotSupportedException e) {
System.err.println("Snake can't clone");
}
return o;
}
public static void main(String[] args) {
Snake s = new Snake(5, 'a');
System.out.println("s = " + s);
Snake s2 = (Snake)s.clone();
System.out.println("s2 = " + s2);
s.increment();
2015-05-18 23:05:20 -07:00
System.out.println(
"after s.increment, s2 = " + s2);
2015-05-05 11:20:13 -07:00
}
2015-05-18 23:05:20 -07:00
} /* Output:
s = :a:b:c:d:e
s2 = :a:b:c:d:e
after s.increment, s2 = :a:c:d:e:f
*///:~