Think in Java第二章的学习

Think in Java这本书的作者,用通俗易懂的话语介绍Java相关知识,在章节最后会有关于本章所学内容的练习题。攻略这些练习题让我有了成就感,同时也是对自己所学内容的肯定。

例如下列代码中打印输出的一些注解语句,让人很容易看懂:

public class Practice06 {
static class StaticTest {
static int i = 48;
}
static class Incrementable {
static void increment() {
StaticTest.i++;
}
}
public static class ITest {
public static void main(String[] args) {
System.out.println("StaticTest.i="+StaticTest.i);
StaticTest st1 = new StaticTest();
StaticTest st2 = new StaticTest();
System.out.println("st1.i="+st1.i);
System.out.println("st2.i="+st2.i);
Incrementable it = new Incrementable();
it.increment();
System.out.println("it.increment()方法被调用后:");
System.out.println("st1.i="+st1.i);
System.out.println("st2.i="+st2.i);
Incrementable.increment();
System.out.println("Incrementable.increment()方法被调用后: ");
System.out.println("st1.i="+st1.i);
System.out.println("st2.i="+st2.i);
}
}
}
==================分割线===================
public class Practice10 {
public static void main(String[] args) {
AllTheColorsOfTheRainbow act = new AllTheColorsOfTheRainbow();
System.out.println("act.anIntegerRepresentingColors = "+act.anIntegerRepresentingColors);
act.changeColor(7);
act.changeTheHueOfTheColor(77);
System.out.println("颜色改变后,act.anIntegerRepresentingColors = "+act.anIntegerRepresentingColors);
System.out.println("act.hue = "+act.hue);
}
}
class AllTheColorsOfTheRainbow {
int anIntegerRepresentingColors = 0;//用基本类型来代表颜色
int hue = 0;
void changeTheHueOfTheColor (int newHue) {
hue = newHue;
}
int changeColor(int newColor) {
return anIntegerRepresentingColors = newColor;
}
}
本书练习题大多都是章节内容中出现过的代码变形,自己一步一步写出来的代码,运行成功后就感觉自己的努力没有白费,没有敲了一天"BUG",233333

猜你喜欢

转载自www.cnblogs.com/yyslif/p/11122471.html