forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.java
More file actions
51 lines (50 loc) · 1.19 KB
/
Snake.java
File metadata and controls
51 lines (50 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// references/Snake.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Tests cloning to see if reference
// destinations 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 Snake clone() {
try {
return (Snake)super.clone();
} catch(CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
Snake s = new Snake(5, 'a');
System.out.println("s = " + s);
Snake s2 = s.clone();
System.out.println("s2 = " + s2);
s.increment();
System.out.println(
"after s.increment, s2 = " + s2);
}
}
/* Output:
s = :a:b:c:d:e
s2 = :a:b:c:d:e
after s.increment, s2 = :a:c:d:e:f
*/