最近在阅读《Thinking in Java》,给第二章的习题部分,做个记录。
(1) 参照本章的第一个例子,创建一个“Hello,World”程序,在屏幕上简单地显示这句话。注意在自己的
类里只需一个方法(“main”方法会在程序启动时执行)。记住要把它设为static 形式,并置入自变量列
表——即使根本不会用到这个列表。用javac 编译这个程序,再用java 运行它。
package com.test.c02;
/**
* 练习题 第二章 (1)
* 切换到当前java文件所在目录: cd
* 编译java文件,生成.class文件: javac Test0201.java
* 运行.class文件: java Test0201
* @author admin11
* @date 2018年2月26日
*/
public class Test0201 {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
(2) 写一个程序,打印出从命令行获取的三个自变量。
package com.test.c02;
/**
* 第二章 (2)
* 运行命令 java Test0202 自变量1 自变量2 自变量3
* @author admin11
* @date 2018年2月26日
*/
public class Test0202 {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println(i + " : " + args[i]);
}
}
}
运行结果:
(3) 找出Property.java 第二个版本的代码,这是一个简单的注释文档示例。请对文件执行javadoc,并在
自己的Web 浏览器里观看结果。
package com.test.c02;
//: Property.java
import java.util.*;
/**
* The first Thinking in Java example program
* Lists system information on current machine.
* @author Bruce Eckel
* @author http://www.BruceEckel.com
* @version 1.0
*/
public class Property {
/**
* Sole entry point to class & application
* @param args array of string arguments
* @return No return value
* @exception exceptions No exceptions thrown
*/
public static void main(String[] args) {
System.out.println(new Date());
Properties p = System.getProperties();
p.list(System.out);
System.out.println("---Memory Usage:");
Runtime rt = Runtime.getRuntime();
System.out.println("Total Memory = " + rt.totalMemory() + " Free Memory = " + rt.freeMemory());
}
} ///:~
使用Javadoc生成注释文档,可以参考:https://jingyan.baidu.com/article/597a0643485c11312b5243c7.html (eclipse)
https://jingyan.baidu.com/article/295430f1fb649f0c7e005035.html (命令)
效果截图:
(4) 以练习(1)的程序为基础,向其中加入注释文档。利用javadoc,将这个注释文档提取为一个HTML 文
件,并用Web浏览器观看。
package com.test.c02;
//: Test0204.java
/**
* 输出print "Hello, world"
* @author admin11
* @version 1.0
*/
public class Test0204 {
/**
* 测试返回值
* @return String
* @exception exceptions 没有抛出异常
*/
public String test() {
return "test";
}
/**
* main method, print "Hello, world"
* @param args array of string
* @exception exceptions 没有抛出异常
*/
public static void main(String[] args) {
System.out.println("Hello, World");
}
} ///:~
效果截图: