// 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 clonedpublic class Snake implements Cloneable {private Snake next;private char c;// Value of i == number of segmentspublic 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();}@Overridepublic String toString() {String s = ":" + c;if(next != null)s += next.toString();return s;}@Overridepublic 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:es2 = :a:b:c:d:eafter s.increment, s2 = :a:c:d:e:f*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。