Thinking in Java 第2章练习

Thinking in Java1//: object/HelloDate.java/** * Default initialize class. * @author C.L.Wang * @author Caroline Wendy * @version 1.0 */{/*** Entrv point to class & application.* @param args array of string arguments* @throws java.lang.Exception No exceptions thrown*/(String[] args) {DefaultInit di = new DefaultInit();System.out.println(“default int = ” + di.i + “, default char = ” + di.c);}/*** Output:* default int = 0, default char =*///:~}2//: object/HelloDate.java/** * Say Hello World * @author C.L.Wang * @author Caroline Wendy * @version 1.0 */{/*** Entrv point to class & application.* @param args array of string arguments* @throws java.lang.Exception No exceptions thrown*/(String[] args) {System.out.println(“Hello, World”);}/*** Output:* Hello, World*///:~}3public class ATNTest { (String[] args) {class ATypeName {int i;double d;boolean b;void show() {System.out.println(i);System.out.println(d);System.out.println(b);}}ATypeName a = new ATypeName();a.i = 3;a.d = 2.7;a.b = false;a.show(); }}4&5class DataOnly { int i; double d; boolean b; void show() {System.out.println(i);System.out.println(d);System.out.println(b); }}{ (String[] args) {DataOnly data = new DataOnly();data.i = 47;data.d = 1.1;data.b = false;data.show(); }}/*** Output:* 47* 1.1* false*///:~6//: StorageTest.java/** * 存储字节数 * Created by wangchenlong on 15/7/6. */{/*** 字符串的字节数* @param s 字符串* @return 字节数*/(String s) {return s.length()*2;}(String[] args) {String s = “Caroline”;System.out.println(s + “字节数占用: ” + storage(s));}/*** Output:* Caroline字节数占用: 16*///:~}7//: IncrementableTest.java/** * 静态变量测试 * Created by wangchenlong on 15/7/6. */class StaticTest {static int i = 47;}class Incrementable {static void increment() {StaticTest.i++;}}{(String[] args) {Incrementable.increment();System.out.println(“数字变为” + StaticTest.i);}}/** * output: * 数字变为48 *///:~8//: OneStaticTest.java/** * static变量单实例测试 * Created by C.L.Wang on 15/7/6. */{(String[] args) {StaticTest s1 = new StaticTest();StaticTest s2 = new StaticTest();//noinspection AccessStaticViaInstanceSystem.out.println(“s1.i = ” + s1.i + “, s2.i = ” + s2.i);Incrementable.increment();//noinspection AccessStaticViaInstanceSystem.out.println(“after increment: ” + “s1.i = ” + s1.i + “, s2.i = ” + s2.i);}}/** * output: * s1.i = 47, s2.i = 47 * after increment: s1.i = 48, s2.i = 48 *///:~9//: AutoBoxTest.java/** * 基本类型和包装器类型 * Created by C.L.Wang on 15/7/6. */{(String[] args) {boolean b = false;char c = ‘x’;byte t = 8;short s = 16;int i = 32;long l = 64;float f = 0.32f;double d = 0.64;Boolean B = b;System.out.println(“boolean b = ” + b);System.out.println(“Boolean B = ” + B);Character C = c;System.out.println(“char c = ” + c);System.out.println(“Character C = ” + C);Byte T = t;System.out.println(“byte t = ” + t);System.out.println(“Byte T = ” + T);Short S = s;System.out.println(“short s = ” + s);System.out.println(“Short S = ” + S);Integer I = i;System.out.println(“int i = ” + i);System.out.println(“Integer I = ” + I);Long L = l;System.out.println(“long l = ” + l);System.out.println(“Long L = ” + L);Float F = f;System.out.println(“float f = ” + f);System.out.println(“Float F = ” + F);Double D = d;System.out.println(“double d = ” + d);System.out.println(“Double D = ” + D);}}/** * Output: * … *///:~10//: CommentLineTest.java/** * 命令行参数 * Created by C.L.Wang on 15/7/6. */{(String[] args) {if (args.length != 3) {System.out.println(“参数不足3个”);return;}String result = “”;for (String s : args) {result += s + ” “;}System.out.println(“参数: ” + result);}}/** * Output: * 参数: caroline wendy girl *///:~11//:AllTheColorsOfTheRainbow/** * 编码风格测试 * Created by C.L.Wang on 15/7/6. */{(String[] args) {AllTheColorOfTheRainbow atc = new AllTheColorOfTheRainbow();atc.changeTheHusOfTheColor(16);System.out.println(“theHueOfTheColor = ” + atc.theHueOfTheColor);System.out.println(“anIntegerRepresentingColors = ” + atc.changeAnIntegerRepresentingColors(32));}}class AllTheColorOfTheRainbow {int anIntegerRepresentingColors;int theHueOfTheColor;void changeTheHusOfTheColor(int newHue) {theHueOfTheColor = newHue;}int changeAnIntegerRepresentingColors(int theHueOfTheColor) {return anIntegerRepresentingColors = theHueOfTheColor;}}/** * Output: * theHueOfTheColor = 16 * anIntegerRepresentingColors = 32 *///:~12&13&14&15

参考:

,我等你用尽了所有的哀伤;而你眼中却有我所不懂的凄凉。

Thinking in Java 第2章练习

相关文章:

你感兴趣的文章:

标签云: