找了好几书JAVA的书,看了几章,都看不下去。
我觉得适合《Teach Yourself Java 2 in 21 Days》(Rogers Cadenhead Laura Lemay)还是适合我的。
孙卫琴那本,我感觉就罗嗦多了没到我点子上。
接口,抽象类这些内容讲得通俗易懂,看着不费力。
代码:
1 class VolcanoRobot { 2 String status; 3 int speed; 4 float temperature; 5 6 void checkTemperature() { 7 if (temperature > 660) { 8 status = "returning home."; 9 speed = 5; 10 } 11 } 12 13 void showAttributes() { 14 System.out.println("Status: " + status); 15 System.out.println("Speed: " + speed); 16 System.out.println("Temperature: " + temperature); 17 } 18 }
1 class VolcanoApplication { 2 public static void main(String[] arguments) { 3 VolcanoRobot dante = new VolcanoRobot(); 4 dante.status = "exploring"; 5 dante.speed = 2; 6 dante.temperature = 510; 7 8 dante.showAttributes(); 9 System.out.println("Increasing speed to 3."); 10 dante.speed = 3; 11 dante.showAttributes(); 12 System.out.println("Changing temperature to 670."); 13 dante.temperature = 670; 14 dante.showAttributes(); 15 System.out.println("Checking the temperature."); 16 dante.checkTemperature(); 17 dante.showAttributes(); 18 } 19 }
运行: