以下是您提供的 Java 程序清单的执行结果及核心要点解析。
正则表达式示例 (Listing 1-9)
| 程序清单 | 核心功能 | 正则表达式 | 输入字符串 | 输出结果 | 关键方法/行为 |
|---|---|---|---|---|---|
| Listing 1 | 全字符串匹配 | Java |
Java |
Matches |
mat.matches()要求整个输入字符串与模式完全匹配。 |
Java |
Java 9 |
No Match |
matches() 方法检查整个字符串,Java 9 包含空格,不匹配 Java。 |
||
| Listing 2 | 查找子序列 | Java |
Java 9 |
subsequence found |
mat.find() 在输入序列中查找下一个匹配的子序列。 |
| Listing 3 | 查找多个子序列 | test |
test 1 2 3 test |
test found at index 0 test found at index 11 |
while(mat.find()) 循环查找所有匹配项,mat.start() 返回匹配起始索引。 |
| Listing 4 | 使用量词 + |
W+ |
W WW WWW |
Match: W Match: WW Match: WWW |
+表示匹配前一个字符一次或多次,mat.group() 返回匹配到的完整字符串。 |
| Listing 5 | 贪婪匹配 | e.+d |
extend cup end table |
Match: extend cup end |
. 匹配任意字符,+ 是贪婪量词,会匹配尽可能长的字符串。 |
| Listing 6 | 勉强匹配 | e.+?d |
extend cup end table |
Match: extend Match: end |
+? 是勉强量词,会匹配尽可能短的字符串。 |
| Listing 7 | 字符类 | [a-z]+ |
this is a test. |
Match: this Match: is Match: a Match: test |
[a-z]+ 匹配一个或多个小写字母,遇到非字母字符(空格、句点)则停止。 |
| Listing 8 | 替换所有匹配项 | Jon.*? |
Jon Jonathan Frank Ken Todd |
Original sequence: Jon Jonathan Frank Ken Todd Modified sequence: Eric Eric Frank Ken Todd |
mat.replaceAll("Eric ") 将所有匹配 Jon 后跟任意字符(勉强模式)直到空格的子串替换为 Eric 。 |
| Listing 9 | 分割字符串 | [ ,.!] |
one two,alpha9 12!done. |
Next token: one Next token: two Next token: alpha9 Next token: 12 Next token: done Next token: |
pat.split() 使用正则表达式作为分隔符进行分割,末尾的空字符串也会被输出。 |
反射示例 (Listing 10-11)
Listing 10 通过反射获取 java.awt.Dimension 类的构造方法、字段和公共方法信息。其输出类似于:
java
Constructors:
public java.awt.Dimension()
public java.awt.Dimension(java.awt.Dimension)
public java.awt.Dimension(int,int)
Fields:
public int width
public int height
Methods:
(会列出大量从 Object 和 Component 等父类继承的公共方法,如 `wait`, `notify`, `getClass`, `toString` 等)
Listing 11 演示如何获取并筛选一个自定义类 A 的公共方法。其输出为:
java
Public Methods:
a1
a2
代码通过 getDeclaredMethods() 获取类声明的所有方法,再使用 Modifier.isPublic() 判断修饰符是否为 public。
RMI 示例 (Listing 12-15)
这是一个简单的远程方法调用 (RMI) 示例,由客户端调用服务器端的加法服务。
-
Listing 12 (接口): 定义了远程接口
AddServerIntf,声明了add方法。 -
Listing 13 (实现):
AddServerImpl类实现了远程接口,提供了add方法的具体逻辑。 -
Listing 14 (服务器):
AddServer类创建远程对象实例并将其绑定到 RMI 注册表(名称为"AddServer")。 -
Listing 15 (客户端):
AddClient类通过 RMI 注册表查找远程对象,并调用其add方法。执行客户端时需要传递服务器主机名和两个加数作为参数 ,例如:输出将类似于:
The first number is: 10.5 The second number is: 20.3 The sum is: 30.8 ```
日期时间格式化示例 (Listing 16-22)
| 程序清单 | 使用的 API | 核心功能 | 示例输出 (可能因运行环境而异) |
|---|---|---|---|
| Listing 16 | DateFormat |
不同地区、不同风格的日期格式化。 | Japan: 2017/06/21 Korea: 2017. 6. 21. United Kingdom: 21 June 2017 United States: Wednesday, June 21, 2017 |
| Listing 17 | DateFormat |
不同地区、不同风格的时间格式化。 | Japan: 13:05 United Kingdom: 13:05:09 BST Canada: 1:05:09 p.m. BST |
| Listing 18 | SimpleDateFormat |
使用自定义模式格式化日期时间。 | ` |
01:05:09<br> |
|||
21 Jun 2017 01:05:09 BST<br>Wed Jun 21 2017` |
|||
| Listing 19 | java.time (LocalDate/LocalTime) |
获取当前日期和时间。 | ` |
2017-06-21<br> |
|||
| 13:05:09.123` | |||
| Listing 20 | java.time.format.DateTimeFormatter |
使用预定义的本地化格式。 | Wednesday, 21 June 2017 ` |
| 13:05` | |||
| Listing 21 | java.time.format.DateTimeFormatter |
使用自定义模式格式化日期时间。 | June 21, 2017 1:05 PM |
| Listing 22 | java.time.format.DateTimeFormatter |
将字符串解析为日期时间对象,再格式化输出。 | June 21, 2017 12:01 AM |