-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsection.java.class.xml
More file actions
424 lines (346 loc) · 10.4 KB
/
section.java.class.xml
File metadata and controls
424 lines (346 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?xml version="1.0" encoding="UTF-8"?>
<chapter id="index"><?dbhtml dir="class" ?>
<title>面向对象</title>
<para>获取 Class 名称</para>
<programlisting>
<![CDATA[
Log.d(TAG, this.getClass().getName());
]]>
</programlisting>
<section id="Java面向对象">
<title>可变参数</title>
<para>修饰符 返回值类型 方法名(参数类型…变量名){}</para>
<programlisting>
<![CDATA[
package cn.netkiller.test;
public class Test {
public static void main(String[] args) {
Test test = new Test();
int total = test.sum(2, 4, 6, 8);
System.out.println(total);
test.print(1, 3, 5, 7);
}
public int sum(int... number) {
int total = 0;
for (int i = 0; i < number.length; i++) {
total += number[i];
}
return total;
}
public void print(Object... obj) {
for (int i = 0; i < obj.length; i++) {
System.out.printf("%s,", obj[i]);
}
}
}
]]>
</programlisting>
</section>
<section id="class.generics">
<title>泛型</title>
<para>java 中泛型标记符:</para>
<screen>
<![CDATA[
E - Element (在集合中使用,因为集合中存放的是元素)
T - Type(Java 类)
K - Key(键)
V - Value(值)
N - Number(数值类型)
? - 表示不确定的 java 类型
S 和 U 基本上与 T 相同,即表示任意的一个Java类型。
]]>
</screen>
<para>其实我们可以使用 A-Z 之间的任何一个 字母,并不会影响程序的正常运行,但是如果换成 T,E,K,V,?,S,U 字母,在可读性上更好一些。</para>
<section>
<title>范型演示</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.List;
public class Test {
public <T> String method(String name, T t) {
System.out.println("普通泛型参数 : " + t + " 数据类型: " + t.getClass().getName());
String str = name + ": " + t;
return str;
}
public static void main(String[] args) {
Test test = new Test();
System.out.println(test.method("001", "bb"));
System.out.println(test.method("002", true));
System.out.println(test.method("003", 100));
System.out.println(test.method("003", 100L));
System.out.println(test.method("003", 100.00));
System.out.println(test.method("003", 'c'));
System.out.println(test.method("003", List.of("Neo")));
}
}
]]>
</programlisting>
<para></para>
<screen>
<![CDATA[
通泛型参数 : bb 数据类型: java.lang.String
001: bb
普通泛型参数 : true 数据类型: java.lang.Boolean
002: true
普通泛型参数 : 100 数据类型: java.lang.Integer
003: 100
普通泛型参数 : 100 数据类型: java.lang.Long
003: 100
普通泛型参数 : 100.0 数据类型: java.lang.Double
003: 100.0
普通泛型参数 : c 数据类型: java.lang.Character
003: c
普通泛型参数 : [Neo] 数据类型: java.util.ImmutableCollections$List12
003: [Neo]
]]>
</screen>
</section>
<section>
<title>数组泛型方法</title>
<para>下面的例子演示了如何使用泛型方法打印不同类型的数组元素:</para>
<programlisting>
<![CDATA[
package cn.netkiller.test;
public class GenericsTest {
// 泛型方法 printArray
public static <E> void printArray(E[] inputArray) {
// 输出数组元素
for (E element : inputArray) {
System.out.printf("%s ", element);
}
System.out.println();
}
public static void main(String[] args) {
System.out.println(Thread.currentThread());
// 创建不同类型数组: Integer, Double 和 Character, String
Integer[] intArray = {1, 2, 3, 4, 5};
Double[] doubleArray = {1.1, 2.2, 3.3, 4.4};
Character[] charArray = {'H', 'E', 'L', 'L', 'O'};
String[] stringArray = {"Neo", "Chen"};
System.out.println("整型数组元素为:");
printArray(intArray); // 传递一个整型数组
System.out.println("\n双精度型数组元素为:");
printArray(doubleArray); // 传递一个双精度型数组
System.out.println("\n字符型数组元素为:");
printArray(charArray); // 传递一个字符型数组
System.out.println("\n字符串数组元素为:");
printArray(stringArray); // 传递一个字符型数组
}
}
]]>
</programlisting>
</section>
<section>
<title>静态方法</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.List;
public class Test {
public static <T, E> String staticMethod(T t, E e) {
String str = "静态泛型方法 参数:" + t + ", " + e + " 数据类型:" + e.getClass().getName();
return str;
}
public static void main(String[] args) {
System.out.println(Test.staticMethod("001", "bb"));
System.out.println(Test.staticMethod("002", true));
System.out.println(Test.staticMethod("003", 100));
System.out.println(Test.staticMethod("003", 100L));
System.out.println(Test.staticMethod("003", 100.00));
System.out.println(Test.staticMethod("003", 'c'));
System.out.println(Test.staticMethod("003", List.of("Neo")));
}
}
]]>
</programlisting>
</section>
<section>
<title>可变参数的泛型方法</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.List;
public class Test {
public <A> void argsMethod(A... args) {
for (A arg : args) {
System.out.print(arg);
System.out.print(" - ");
}
System.out.println();
}
public static void main(String[] args) {
Test test = new Test();
System.out.println("静态泛型方法");
test.argsMethod("aaa", "bbb", "ccc");
test.argsMethod(1, 2, 3);
test.argsMethod(true, false, true);
test.argsMethod(73.5, 100.00, 88.8);
test.argsMethod('a', 'b', 'c');
test.argsMethod(1L, 3L, 5L);
}
}
]]>
</programlisting>
</section>
<section>
<title>返回泛型值</title>
<programlisting>
<![CDATA[
package cn.netkiller.test;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class Test {
public <T> T method(T t) {
System.out.println("类型:" + t.getClass().getName() + " 返回值:" + t);
return t;
}
public static void main(String[] args) {
Test test = new Test();
System.out.println("返回泛型方法");
String s = test.method("aaa");
System.out.println(s);
int n = test.method(13579);
System.out.println(n);
boolean b = test.method(false);
System.out.println(b);
List<String> l = test.method(Arrays.asList("neo", "chen", "netkiller"));
System.out.println(l);
Object m = test.method(Map.of("name", "neo", "nickname", "netkiller"));
System.out.println(m);
}
}
]]>
</programlisting>
<para></para>
<screen>
<![CDATA[
返回泛型方法
类型:java.lang.String 返回值:aaa
aaa
类型:java.lang.Integer 返回值:13579
13579
类型:java.lang.Boolean 返回值:false
false
类型:java.util.Arrays$ArrayList 返回值:[neo, chen, netkiller]
[neo, chen, netkiller]
类型:java.util.ImmutableCollections$MapN 返回值:{nickname=netkiller, name=neo}
{nickname=netkiller, name=neo}
]]>
</screen>
</section>
<section>
<title>泛型类</title>
<programlisting>
<![CDATA[
public class Generics<T> {
private T t;
public void add(T t) {
this.t = t;
}
public T get() {
return t;
}
public static void main(String[] args) {
Generics<Integer> integerGenerics = new Generics<Integer>();
Generics<String> stringGenerics = new Generics<String>();
integerGenerics.add(new Integer(10));
stringGenerics.add(new String("Netkiller"));
System.out.printf("整型值为 :%d\n\n", integerGenerics.get());
System.out.printf("字符串为 :%s\n", stringGenerics.get());
}
}
]]>
</programlisting>
</section>
</section>
<section id="class.record">
<title>record</title>
<programlisting>
<![CDATA[
package cn.netkiller.demo;
record User(String name, Integer age) {
}
public class RecordDemo {
public RecordDemo() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
User user = new User("Neo", 35);
System.out.println(user.toString());
}
}
]]>
</programlisting>
<section>
<title>在 Record 中定义范型</title>
<programlisting>
<![CDATA[
package cn.netkiller.record;
import java.util.Date;
public record WebsocketMessage<T>(String device, String event, T data, Date date) {
}
]]>
</programlisting>
</section>
</section>
<section id="class.callback">
<title>Callback 回调</title>
<programlisting>
<![CDATA[
package cn.netkiller;
interface Callback {
void call();
}
abstract class Task {
public final void executeWith(Callback callback) {
this.execute();
if (callback != null)
callback.call();
}
protected abstract void execute();
}
class SimpleTask extends Task {
protected void execute() {
System.out.println("Do some tasks before the callback method.");
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Task task = new SimpleTask();
Callback callback = new Callback() {
public void call() {
System.out.println("The callback method has been called!");
}
};
task.executeWith(callback);
}
}
]]>
</programlisting>
</section>
<section id="class.sealed">
<title>密封类</title>
<para>Java 17 之前,如果限制一个类可以被其他类继承,只能使用 final 关键字限制被集成。Java 17 提供了密封类,可以控制谁可以集成。</para>
<programlisting>
<![CDATA[
package cn.netkiller.test;
public class Test {
public static void main(String[] args) {
}
public abstract sealed class Furit permits Apple, Pear {
}
public non-sealed class Apple extends Furit {
}
public final class Pear extends Furit {
}
private final class Vegetables extends Furit {
}
}
]]>
</programlisting>
<section>例如上面例子中,Furit 可以被 Apple, Pear 继承,但是不能被 Vegetables 继承。</section>
</section>
</chapter>