-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
446 lines (214 loc) · 615 KB
/
search.xml
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>数梦工厂笔试题</title>
<link href="post/63731958/"/>
<url>post/63731958/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><h1 id="一、单选题"><a href="#一、单选题" class="headerlink" title="一、单选题"></a>一、单选题</h1><h2 id="1-编译和运行以下代码的结果是什么(本题3分)"><a href="#1-编译和运行以下代码的结果是什么(本题3分)" class="headerlink" title="1. 编译和运行以下代码的结果是什么(本题3分)"></a>1. 编译和运行以下代码的结果是什么(本题3分)</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StreamTest</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> String a<span class="token operator">=</span><span class="token string">"apple"</span><span class="token punctuation">;</span> String b<span class="token operator">=</span><span class="token string">"apple"</span><span class="token punctuation">;</span> String c<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"apple"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String d<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"apple"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token operator">==</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token operator">==</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>c<span class="token operator">==</span>d<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>c<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>解析</strong>:<code>true</code> ,<code>false</code> , <code>false</code> , <code>true</code> ,<code>true</code> </p><ul><li><p>用<code>String</code>定义字符串变量时,会先在常量池里面找有没有字符串<code>apple</code>,如果没有就把字符串<code>apple</code>放到常量池,变量名指向常量池中的地址,如果字符串已存在那么就不再重复放字符串到常量池了,变量名直接指向常量池中的地址</p><p>用<code>new String()</code>定义变量时,会和普通的引用类型一样,在堆中新建字符串<code>apple</code>,变量名指向堆中的地址</p><p><img src="/post/63731958/image-20210526220505980.png"></p></li><li><p><code>==</code>对于基本类型来说是值比较,对于引用类型来说是引用的比较;而<code>equals()</code>默认情况下是引用比较,只是很多类重写了<code>equals()</code>方法,比如<code>String</code>、<code>Integer</code>等把它变成了值比较,所以一般情况下equals比较的是值是否相等</p></li></ul><h2 id="2-下列链表的描述,下列语句正确的有(本题3分)"><a href="#2-下列链表的描述,下列语句正确的有(本题3分)" class="headerlink" title="2. 下列链表的描述,下列语句正确的有(本题3分)"></a>2. 下列链表的描述,下列语句正确的有(本题3分)</h2><p>A. 在操作<code>new ArrryList(20);</code>中,链表扩充了一次。</p><p>B. 对于随机访问<code>get</code>和<code>set</code>操作时,<code>ArrayList</code>比<code>LinkedList</code>更合适。</p><p>C. <code>ArrayList</code>是线程非安全的,<code>LinkedList</code>是线程安全的。</p><p>D. 在链表初始化时,如果像<code>new ArrayList(20)</code>;一样指定了链表长度时,则在插入第21个元素时,会抛出异常。</p><p><strong>解析:</strong></p><ul><li><p>A. <code>ArrayList list=new ArrayList();</code> 这种是默认创建大小为<strong>10</strong>的数组,每次扩容大小为<strong>1.5</strong>倍; <code>ArrayList list=new ArrayList(20);</code> 这种是指定数组大小的创建,创建时直接分配其大小,没有扩充。所以,扩充为0次</p></li><li><p>B. 如果集合数据是对于集合随机访问 get 和 set,<code>ArrayList</code> 绝对优于 <code>LinkedList</code>,因为 <code>LinkedList</code> 要移动指针,耽误时间。如果集合数据是对于集合新增和删除操作 <code>add</code> 和 <code>remove</code>,<code>LinedList</code> 比较占优势,因为 <code>ArrayList</code> 要移动数据。所以 B 正确 </p></li><li><p>C. <code>ArrayList</code> 和 <code>LinkedList</code> 都是线程非安全的</p></li><li><p>D. 不会抛出异常,而是会自动扩容为原来长度的<strong>1.5</strong>倍。</p></li></ul><h2 id="3-根据下述代码,关于编译描述正确的为(本题3分)"><a href="#3-根据下述代码,关于编译描述正确的为(本题3分)" class="headerlink" title="3. 根据下述代码,关于编译描述正确的为(本题3分)"></a>3. 根据下述代码,关于编译描述正确的为(本题3分)</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> c05<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ab</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">int</span> a<span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token keyword">int</span> b<span class="token punctuation">;</span> <span class="token keyword">protected</span> <span class="token keyword">int</span> c<span class="token punctuation">;</span> <span class="token keyword">int</span> d<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">f1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">f2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">f3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">void</span> <span class="token function">f4</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> c06<span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">a</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> co5<span class="token punctuation">.</span>ab t<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">co5<span class="token punctuation">.</span>ab</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t<span class="token punctuation">.</span>a<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span> t<span class="token punctuation">.</span>b<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//编译错误</span> t<span class="token punctuation">.</span>c<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//编译错误</span> t<span class="token punctuation">.</span>d<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//编译错误</span> t<span class="token punctuation">.</span><span class="token function">f1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t<span class="token punctuation">.</span><span class="token function">f2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//编译错误</span> t<span class="token punctuation">.</span><span class="token function">f3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//编译错误</span> t<span class="token punctuation">.</span><span class="token function">f4</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//编译错误</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>解析:</strong></p><p> <code>private</code> : 在同一类内可见。使用对象:变量、方法。 注意:不能修饰类(外部类)</p><p> <code>default</code> (即缺省,什么也不写,不使用任何关键字): 在同一包内可见,不使用任何修饰符。使用对象:类、接口、变量、方法。</p><p> <code>protected</code> : 对同一包内的类和所有子类可见。使用对象:变量、方法。 注意:不能修饰类(外部类)。</p><p> <code>public</code> : 对所有类可见。使用对象:类、接口、变量、方法</p><p>如下图:</p><p> <img src="/post/63731958/image-20210601160942406.png"></p><h2 id="4-执行如下程序后,a的值是(本题3分)"><a href="#4-执行如下程序后,a的值是(本题3分)" class="headerlink" title="4. 执行如下程序后,a的值是(本题3分)"></a>4. 执行如下程序后,a的值是(本题3分)</h2><pre class="line-numbers language-java"><code class="language-java"> <span class="token keyword">int</span> a<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span> <span class="token keyword">do</span><span class="token punctuation">{</span> a<span class="token operator">--</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">while</span> <span class="token punctuation">(</span>a<span class="token operator">--</span><span class="token operator">></span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>A. 0</p><p>B. -1</p><p>C. 1</p><p>D. 死循环</p><p>E. -2</p><p><strong>解析:</strong>选 E. 因为,<code>while(a-->0)</code> 自减条件在<code>while</code>循环中,即使条件不符,后减的<code>a</code>仍会执行;但如果<code>a--</code>放在了循环体中的话,后减的<code>a</code>就不会执行了</p><h2 id="5-编译和运行以下类的结果是什么?(本题3分)"><a href="#5-编译和运行以下类的结果是什么?(本题3分)" class="headerlink" title="5. 编译和运行以下类的结果是什么?(本题3分)"></a>5. 编译和运行以下类的结果是什么?(本题3分)</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Test</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">methodA</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">methodA</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> Test X<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> X<span class="token punctuation">.</span><span class="token function">methodA</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>A. 该程序编译并运行打印5。</p><p>B. 该程序编译错误。</p><p>C. 该程序运行时异常,因为它找不到方法<code>Test.methodA(int)</code>。</p><p>D. 该程序编译并运行打印6。</p><p><strong>解析:</strong> 选 B </p><p> 该题考的是方法的重载:即发生在同一个类中,方法名相同参数列表不同(参数类型不同、个数不同、顺序不同),与<strong>方法返回值</strong>和访问修饰符无关,即重载的方法不能根据返回类型进行区分。故本题的<code>methodA(int i)</code>被定义了两次,造成编译错误</p><blockquote><p><strong>引申:</strong> </p><p>方法的重写:发生在父子类中,方法名、参数列表必须相同,返回值小于等于父类,抛出的异常小于等于父类,访问修饰符大于等于父类(里氏代换原则);如果父类方法访问修饰符为private则子类中就不是重写。</p></blockquote><h2 id="6-根据下述代码,输出的结果是(本题3分)"><a href="#6-根据下述代码,输出的结果是(本题3分)" class="headerlink" title="6. 根据下述代码,输出的结果是(本题3分)"></a>6. 根据下述代码,输出的结果是(本题3分)</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">listTest</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> List<span class="token operator"><</span>Integer<span class="token operator">></span> list <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> list<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>Integer item<span class="token operator">:</span>list<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>item<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> list<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>list<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>A. 打印“[1,2]”</p><p>B. 运行时发生异常</p><p>C. 编译错误</p><p>D. 打印“[1]”</p><p><strong>解析:</strong> 选 B 。因为 <code>remove(int index)</code> 是删除链表中指定位置的元素,而链表中只有0,1位置有元素。</p><h2 id="7-根据下述代码,正确的输出值为(本题3分)"><a href="#7-根据下述代码,正确的输出值为(本题3分)" class="headerlink" title="7.根据下述代码,正确的输出值为(本题3分)"></a>7.根据下述代码,正确的输出值为(本题3分)</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Demo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> StringBuilder a<span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuilder</span><span class="token punctuation">(</span><span class="token string">"A"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> StringBuilder b<span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuilder</span><span class="token punctuation">(</span><span class="token string">"B"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">operate</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token operator">+</span><span class="token string">","</span><span class="token operator">+</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">operate</span><span class="token punctuation">(</span>StringBuilder x<span class="token punctuation">,</span> StringBuilder y<span class="token punctuation">)</span> <span class="token punctuation">{</span> x<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span> y<span class="token operator">=</span>x<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p> A. A,AB</p><p> B. A,B</p><p> C. AB,B</p><p> D. 编译错误</p><p> E. AB,AB</p><p><strong>解析:</strong> 选 C 。 </p><p>a指向A,b指向B, <code>operate()</code>:x指向A,y指向B—————-调用方法:x指向AB,y指向了X为AB</p><p>之后:a指向的值变成了AB,b指向的还是B。</p><p> 如下图:</p><p><img src="/post/63731958/image-20210601165938271.png"></p><h2 id="8-根据下列代码,正确的输出值为(本题3分)"><a href="#8-根据下列代码,正确的输出值为(本题3分)" class="headerlink" title="8. 根据下列代码,正确的输出值为(本题3分)"></a>8. 根据下列代码,正确的输出值为(本题3分)</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Start</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">new</span> <span class="token class-name">Apple</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">class</span> <span class="token class-name">Fruit</span> <span class="token punctuation">{</span> <span class="token function">Fruit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"init a fruit"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">class</span> <span class="token class-name">Apple</span> <span class="token keyword">extends</span> <span class="token class-name">Fruit</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token keyword">double</span> w<span class="token operator">=</span><span class="token function">initW</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token keyword">static</span> String color <span class="token operator">=</span><span class="token function">initC</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">Apple</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"init an apple"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">static</span> String <span class="token function">initC</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"init c"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token string">""</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">double</span> <span class="token function">initW</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"init w"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">1.0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>解析:</strong> 输出顺序为:<code>init c</code> <code>init a fruit</code> <code>init w</code> <code>init an apple</code></p><p>java代码的执行顺序:</p><ol><li><p> 先执行父类的静态变量和代码块,在执行子类的静态变量和代码块</p></li><li><p> 接着执行父类的变量和构造方法里面的方法</p></li><li><p>接着执行子类的变量和构造方法里面的方法 </p></li></ol><h2 id="9-下列程序的输出内容为(本题3分)"><a href="#9-下列程序的输出内容为(本题3分)" class="headerlink" title="9.下列程序的输出内容为(本题3分)"></a>9.下列程序的输出内容为(本题3分)</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Start</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> Set<span class="token operator"><</span>Integer<span class="token operator">></span> set<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>set<span class="token punctuation">)</span><span class="token punctuation">;</span> set<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>set<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>解析:</strong> 输出内容为:[0, 1, 2, 3]<br> [0, 1, 3]</p><p><code>Set</code>:一个无序(存入和取出顺序又可能不一致)容器,不可以存储重复元素,只允许存入一个null元素。<code>set.remove(Object o</code>) 方法用于从Set中删除特定元素。</p><blockquote><p>存入和取出顺序又可能不一致的原因:</p><p><code>HashSet</code>:哈希表里面存放的是哈希值,HashSet存储元素的顺序并不是按照存入时的顺序(和List显然不同) 是按照哈希值来存的所以取数据也是按照哈希值取得。</p></blockquote><h2 id="10-若要求”查询选修了3门以上课程的学生的学生号“,正确的SQL语句是(本题3分)"><a href="#10-若要求”查询选修了3门以上课程的学生的学生号“,正确的SQL语句是(本题3分)" class="headerlink" title="10. 若要求”查询选修了3门以上课程的学生的学生号“,正确的SQL语句是(本题3分)"></a>10. 若要求”查询选修了3门以上课程的学生的学生号“,正确的SQL语句是(本题3分)</h2><pre class="line-numbers language-sql"><code class="language-sql">A<span class="token punctuation">.</span> <span class="token keyword">SELECT</span> Sno <span class="token keyword">FROM</span> SC <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> Sno <span class="token keyword">WHERE</span> <span class="token function">COUNT</span> <span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">3</span>B<span class="token punctuation">.</span> <span class="token keyword">SELECT</span> Sno <span class="token keyword">FROM</span> SC <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> Sno <span class="token keyword">HAVING</span> <span class="token function">COUNT</span> <span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token operator">>=</span> <span class="token number">3</span>C<span class="token punctuation">.</span> <span class="token keyword">SELECT</span> Sno <span class="token keyword">FROM</span> SC <span class="token keyword">GROUP</span> <span class="token keyword">BY</span> Sno <span class="token keyword">HAVING</span> <span class="token punctuation">(</span><span class="token function">COUNT</span> <span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">3</span><span class="token punctuation">)</span>D<span class="token punctuation">.</span> <span class="token keyword">SELECT</span> Sno <span class="token keyword">FROM</span> SC <span class="token keyword">GROUP</span> <span class="token keyword">BY</span> Sno <span class="token keyword">WHERE</span> <span class="token function">COUNT</span> <span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">3</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>解析:</strong> 选 C .</p><ul><li><p><code>order by</code>的用法</p><p>使用<code>order by</code>,一般是用来,依照查询结果的某一列(或多列)属性,进行排序(升序:<code>ASC</code>;序:<code>DESC</code>;默认为升序)。</p><p>当排序列含空值时:</p><p><code>ASC</code>:排序列为空值的元组最后显示。</p><p><code>DESC</code>:排序列为空值的元组最先显示。</p><ul><li><p>单一列属性排序</p></li><li><p>多列属性排序</p><p>选择多个列属性进行排序,排序的顺序是,<strong>从左到右</strong>,依次排序。如果前面列属性有些是一样的话,再按后面的列属性排序。(前提一定要满足前面的属性排序,因为在前面的优先级高)</p></li></ul></li><li><p><code>group by</code> 的用法</p><p>group by按照查询结果集中的某一列(或多列),进行分组,值相等的为一组。</p><ul><li><p>细化集函数(<code>count</code>,<code>sum</code>,<code>avg</code>,<code>max</code>,<code>min</code>)的作用对象:</p><p>未对查询结果分组,集函数将作用于整个查询结果。</p><p>例子:求各个课程号及相应的选课人数:</p><pre class="line-numbers language-sql"><code class="language-sql"><span class="token keyword">SELECT</span> cno<span class="token punctuation">,</span><span class="token function">count</span><span class="token punctuation">(</span>sno<span class="token punctuation">)</span> <span class="token keyword">from</span> sc<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p><img src="/post/63731958/20150901020802381.png"></p><p>对查询结果分组后,集函数将分别作用于每个组</p><pre class="line-numbers language-sql"><code class="language-sql"><span class="token keyword">SELECT</span> cno<span class="token punctuation">,</span><span class="token function">count</span><span class="token punctuation">(</span>sno<span class="token punctuation">)</span> <span class="token keyword">from</span> sc <span class="token keyword">group</span> <span class="token keyword">by</span> cno<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p><img src="/post/63731958/20150901021021720.png"></p></li><li><p><code>GROUP BY</code>子句的作用对象是查询的中间结果表</p><p>分组方法:按指定的一列或多列值分组,值相等的为一组。</p><p>使用<code>GROUP BY</code>子句后,<code>SELECT</code>子句的列名列表中只能出现分组属性(比如:<code>sno</code>)和集函数(比如:<code>count()</code>)。</p><pre class="line-numbers language-sql"><code class="language-sql"><span class="token keyword">select</span> sno<span class="token punctuation">,</span><span class="token function">count</span><span class="token punctuation">(</span>cno<span class="token punctuation">)</span> <span class="token keyword">from</span> sc <span class="token keyword">group</span> <span class="token keyword">by</span> sno<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li></ul><p><img src="/post/63731958/20150901021443417.png"></p><ul><li><p>多个列属性进行分组 </p><p>举例:</p><pre class="line-numbers language-SQL"><code class="language-SQL">select cno,grade,count(cno) from sc group by cno,grade<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p> <img src="/post/63731958/1.png"></p></li><li><p>使用<code>HAVING</code>短语筛选最终输出结果</p><p>只有满足<code>HAVING</code>短语指定条件的组才输出。</p><p><code>HAVING</code>短语与<code>WHERE</code>子句的<strong>区别</strong>:作用对象不同。</p><ul><li><p><code>WHERE</code>子句作用于<strong>基表或视图</strong>,从中选择满足条件的<strong>元组</strong>。</p></li><li><p><code>HAVING</code>短语作用于<strong>组</strong>,从中选择满足条件的<strong>组</strong>。</p></li></ul><p>举例:</p><p> 查询选修了3门以上课程的学生学号:</p><pre class="line-numbers language-SQL"><code class="language-SQL">select sno from sc group by sno having count(cno)>3<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p> <img src="/post/63731958/2.png"></p><p> 举例:</p><p> 查询选修了3门以上课程,且<strong>所有课程</strong>成绩都高于60分的学生学号及课程数</p><pre class="line-numbers language-sql"><code class="language-sql"><span class="token keyword">select</span> sno <span class="token punctuation">,</span> <span class="token function">count</span><span class="token punctuation">(</span>cno<span class="token punctuation">)</span><span class="token keyword">from</span> sc <span class="token keyword">where</span> grade <span class="token operator">></span> <span class="token number">60</span> <span class="token keyword">group</span> <span class="token keyword">by</span> sno <span class="token keyword">having</span> <span class="token function">count</span><span class="token punctuation">(</span>cno<span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">3</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p> <img src="/post/63731958/1-1622560362720.png"></p></li></ul></li></ul><blockquote><p>小贴士:</p><ol><li><p><code>group by</code> 字句也和<code>where</code>条件语句结合在一起使用。当结合在一起时,<code>where</code>在<strong>前</strong>,<code>group by</code> 在<strong>后</strong>。即先对<code>select xx from xx</code>的记录集合用<code>where</code>进行筛选,然后再使用<code>group by</code> 对筛选后的结果进行分组 使用<code>having</code>字句对分组后的结果进行筛选。</p></li><li><p><code>having</code>只能用在<code>group by</code>之后,对分组后的结果进行筛选(即使用<code>having</code>的前提条件是<strong>分组</strong>)</p></li><li><p><code>where</code>后的条件表达式里不允许使用聚合函数,而<code>having</code>可以</p></li><li><p>当一个查询语句同时出现了where,group by,having,order by的时候,执行顺序和编写顺序是:</p><p> (1). 执行where xx对全表数据做筛选,返回第1个结果集。</p><p> (2). 针对第1个结果集使用group by分组,返回第2个结果集。</p><p> (3). 针对第2个结果集中的每1组数据执行select xx,有几组就执行几次,返回第3个结果集。</p><p> (4). 针对第3个结集执行having xx进行筛选,返回第4个结果集。</p><p> (5). 针对第4个结果集排序。</p><p>也就是说其实where不能和聚合函数一起使用,因为select语句执行在where之后。</p></li></ol></blockquote><h2 id="11-正确使用索引可以提高查询效率,productLine列已经添加索引,如下哪些表达式会使用索引?(本题3分)"><a href="#11-正确使用索引可以提高查询效率,productLine列已经添加索引,如下哪些表达式会使用索引?(本题3分)" class="headerlink" title="11. 正确使用索引可以提高查询效率,productLine列已经添加索引,如下哪些表达式会使用索引?(本题3分)"></a>11. 正确使用索引可以提高查询效率,productLine列已经添加索引,如下哪些表达式会使用索引?(本题3分)</h2><pre class="line-numbers language-sql"><code class="language-sql">A<span class="token punctuation">.</span> <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> products <span class="token keyword">where</span> productLine <span class="token operator">LIKE</span> <span class="token string">"%abc%"</span>B<span class="token punctuation">.</span> <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> products <span class="token keyword">where</span> substr<span class="token punctuation">(</span>productLine<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token string">"abc"</span>C<span class="token punctuation">.</span> <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> products <span class="token keyword">where</span> productLine <span class="token operator">LIKE</span> <span class="token string">"%abc"</span>D<span class="token punctuation">.</span> <span class="token keyword">select</span> <span class="token operator">*</span> <span class="token keyword">from</span> products <span class="token keyword">where</span> productLine <span class="token operator">LIKE</span> <span class="token string">"abc%"</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>解析:</strong>选 D。</p><ul><li>使用索引情景<ul><li>字段的数值有唯一性的限制,比如用户名</li><li>频繁作为 <code>WHERE</code> 查询条件的字段,尤其在数据表大的情况下</li><li> 需要经常 <code>GROUP BY</code> 和 <code>ORDER BY</code> 的列</li><li><code>UPDATE</code>、<code>DELETE</code> 的 <code>WHERE</code> 条件列,一般也需要创建索引</li><li><code>DISTINCT</code> 字段需要创建索引</li></ul></li><li>索引失效的情况<ul><li> 索引进行了表达式计算,则会失效</li><li>对索引使用函数,也会造成失效</li><li>在 <code>WHERE</code> 子句中,如果在 <code>OR</code> 前的条件列进行了索引,而在 <code>OR</code> 后的条件列没有进行索引,那么索引会失效</li><li><code>like</code> 以通配符开头(<code>%abc</code>)会导致索引失效</li><li>索引列与 <code>NULL</code> 或者 <code>NOT NULL</code> 进行判断的时候也会失效。</li></ul></li></ul><h2 id="12-在SQL语句中,与表达式-name-NOT-IN-“wh1”-”wh2”-功能相同的表达式是(本题3分)"><a href="#12-在SQL语句中,与表达式-name-NOT-IN-“wh1”-”wh2”-功能相同的表达式是(本题3分)" class="headerlink" title="12. 在SQL语句中,与表达式 name NOT IN (“wh1”,”wh2”) 功能相同的表达式是(本题3分)"></a>12. 在SQL语句中,与表达式 name NOT IN (“wh1”,”wh2”) 功能相同的表达式是(本题3分)</h2><pre class="line-numbers language-SQL"><code class="language-SQL">A. name !=“wh1” AND name != "wh2"B. name !=“wh1” OR name != "wh2"C. name =“wh1” AND name = "wh2"D. name =“wh1” OR name = "wh2"<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>解析:</strong> 选 A.</p><h1 id="二、-多选题"><a href="#二、-多选题" class="headerlink" title="二、 多选题"></a>二、 多选题</h1><h2 id="13-下列有关于public-protected-private的描述不正确的是(本题5分)"><a href="#13-下列有关于public-protected-private的描述不正确的是(本题5分)" class="headerlink" title="13. 下列有关于public,protected,private的描述不正确的是(本题5分)"></a>13. 下列有关于public,protected,private的描述不正确的是(本题5分)</h2><p>A. 如果不加上述三个描述词,只能被同一个包内的类访问。</p><p>B. 内部类的private类成员不能被外部类访问。</p><p>C. public类成员,能够被其他所有类访问。</p><p>D. protected类成员,只能被子类访问。</p><p>E. private类成员,不能被其他类访问,包括父类和子类。</p><p><strong>解析:</strong>B、 D、E</p><p> <img src="/post/63731958/image-20210601160942406.png"></p><p>B. 对于外部类来说 内部类相当于它的一个属性 内部类中的private也相当于它本身的private属性 所以根据类内可见原则 内部类private是可以被外部类访问的 </p><p>E. 子类对象拥有父类中的所有属性和方法,则子类当然也继承了父类的私有属性和方法。但是它不能****<em>直接*</em>***访问,之所以说不能直接访问,而不是不能访问,那是因为一旦父类提供了其私有成员的公共访问方法,那么子类就可以访问其私有成员</p><h2 id="14-下面选项中定义正确的有(本题5分)"><a href="#14-下面选项中定义正确的有(本题5分)" class="headerlink" title="14. 下面选项中定义正确的有(本题5分)"></a>14. 下面选项中定义正确的有(本题5分)</h2><pre class="line-numbers language-java"><code class="language-java">A<span class="token punctuation">.</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> strs <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">{</span><span class="token string">"a"</span><span class="token punctuation">,</span><span class="token string">"b"</span><span class="token punctuation">,</span><span class="token string">"c"</span><span class="token punctuation">}</span>B<span class="token punctuation">.</span> String strs<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span><span class="token string">"a"</span><span class="token punctuation">,</span><span class="token string">"b"</span><span class="token punctuation">,</span><span class="token string">"c"</span><span class="token punctuation">}</span>C<span class="token punctuation">.</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> strs <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"a"</span><span class="token punctuation">,</span><span class="token string">"b"</span><span class="token punctuation">,</span><span class="token string">"c"</span><span class="token punctuation">}</span>D<span class="token punctuation">.</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> strs <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">{</span><span class="token string">"a"</span><span class="token punctuation">,</span><span class="token string">"b"</span><span class="token punctuation">,</span><span class="token string">"c"</span><span class="token punctuation">}</span>E<span class="token punctuation">.</span> String strs<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">"a"</span><span class="token punctuation">,</span><span class="token string">"b"</span><span class="token punctuation">,</span><span class="token string">"c"</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>解析:</strong> B、C、E</p><p> string数组的定义有三种:</p><pre class="line-numbers language-java"><code class="language-java">String str<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//创建一个长度为10的String 类型数组。 </span>String str<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">"a"</span><span class="token punctuation">,</span><span class="token string">"b"</span><span class="token punctuation">}</span><span class="token punctuation">;</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> str <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span><span class="token string">"a"</span><span class="token punctuation">,</span><span class="token string">"b"</span><span class="token punctuation">,</span><span class="token string">"c"</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h2 id="15-下列选项中定义或者描述正确的有(本题5分)"><a href="#15-下列选项中定义或者描述正确的有(本题5分)" class="headerlink" title="15.下列选项中定义或者描述正确的有(本题5分)"></a>15.下列选项中定义或者描述正确的有(本题5分)</h2><p>A. 类可以实现多个接口,也可以继承多个父类</p><p>B. byte类型的取值范围是-128~127</p><p>C. unsigned int =1</p><p>D. char类型的长度为2 个字节</p><p><strong>解析:</strong>B、D</p><p>A. Java中一个类可以实现多个接口,但只能继承一个类。因为Java中的多继承有可能会造成二义性。</p><p>C. Java 中没有无符号整数的数据类型</p><h2 id="16-关于接口和抽象类,下面选项中说法正确的有(本题6分)"><a href="#16-关于接口和抽象类,下面选项中说法正确的有(本题6分)" class="headerlink" title="16. 关于接口和抽象类,下面选项中说法正确的有(本题6分)"></a>16. 关于接口和抽象类,下面选项中说法正确的有(本题6分)</h2><p>A. 接口支持多继承,一个接口可以继承多个接口</p><p>B. 接口中只能有public static final常量,抽象类中既可以有常量,也可以有变量</p><p>C. 接口不能实例化,抽象类可以实例化</p><p>D.包含抽象方法的类一定是抽象类,但是抽象类不一定含有抽象方法</p><p><strong>解析:</strong>A、B、D</p><p>接口(interface)可以说成是抽象类的一种特例,接口中的所有方法都必须是抽象的。接口中的方法定义默认为public abstract类型,接口中的成员变量类型默认为public static final。</p><ul><li><p>接口和抽象类共同点:</p><ul><li><strong>都不能实例化</strong>,都位于继承树的顶端,用于被其他类实现和实现</li><li>都可以包含抽象方法,实现接口或者继承抽象类的普通子类都必须重写这些抽象方法</li></ul></li><li><p>接口和抽象类不同点:</p><ul><li><strong>接口里只含有默认方法和抽象方法;抽象类中可以含有普通方法</strong></li><li>接口不能定义静态方法;抽象类可以定义静态方法</li><li><strong>接口只能定义静态常量,不能定义普通成员变量,但接口中定义的变量只能是public static final类型,并且默认即为public static final类型;抽象类既可以定义普通成员变量,也可以定义静态成员变量</strong></li><li>接口中不含构造器;抽象类中含有构造器</li><li>抽象方法可以有 public,protected,default 这些修饰符,接口默认修饰符为 public</li><li><strong>一个类可以实现多个接口,但只能继承一个抽象类</strong></li></ul></li></ul><h2 id="17-下列哪些函数能保证线程将离开运行状态?(本题6分)"><a href="#17-下列哪些函数能保证线程将离开运行状态?(本题6分)" class="headerlink" title="17. 下列哪些函数能保证线程将离开运行状态?(本题6分)"></a>17. 下列哪些函数能保证线程将离开运行状态?(本题6分)</h2><pre class="line-numbers language-java"><code class="language-java">A<span class="token punctuation">.</span> Thread<span class="token punctuation">.</span><span class="token function">killThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span> B<span class="token punctuation">.</span> <span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span>C<span class="token punctuation">.</span> <span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span>D<span class="token punctuation">.</span> <span class="token function">yield</span><span class="token punctuation">(</span><span class="token punctuation">)</span>E<span class="token punctuation">.</span> aLiveThread<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token punctuation">)</span>F<span class="token punctuation">.</span> <span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">)</span>G<span class="token punctuation">.</span> <span class="token function">notifyAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>解析:</strong> B、C、D、E</p><p><strong>线程离开运行状态的方法:</strong></p><ol><li>调用<code>Thread.sleep()</code>:使当前线程睡眠至少多少毫秒(尽管它可能在指定的时间之前被中断)</li><li>调用<code>Thread.yield()</code>:不能保障太多事情,尽管通常它会让当前运行线程回到可运行性状态,使得有相同优先级的线程有机会执行</li><li>调用j<code>oin()</code>方法:保证当前线程停止执行,直到调用join方法的线程完成为止。然而,如果调用join的线程没有存活,则当前线程不需要停止</li><li>线程的<code>run()</code>方法完成</li><li>在对象上调用<code>wait()</code>方法(不是在线程上调用)</li><li>线程不能在对象上获得锁定,它正试图运行该对象的方法代码。</li><li>线程调度程序可以决定将当前运行状态移动到可运行状态,以便让另一个线程获得运行机会,而不需要任何理由。</li></ol><h2 id="18-下列有关-try-语句的描述,正确的有(本题6分)"><a href="#18-下列有关-try-语句的描述,正确的有(本题6分)" class="headerlink" title="18. 下列有关 try 语句的描述,正确的有(本题6分)"></a>18. 下列有关 try 语句的描述,正确的有(本题6分)</h2><p>A. <code>try-with-resources</code> 语句中申明的实现 <code>AutoCloseable</code> 接口的资源可以不手动关闭</p><p>B. 在一个有多个 <code>catch</code> 的 <code>try-catch</code> 语句中,异常的顺序没有特殊的要求。</p><p>C. <code>try</code> 语句中至少有一个 <code>catch</code>。</p><p>D. 使用 <code>try-catch-finally</code> 语句手动对资源关闭时,必须按照创建资源时相反的顺序进行关闭。</p><p>E. 在程序正常运行的情况下,<code>try-catch-finally</code> 语句最终都会执行 <code>finally</code> 中的内容</p><p><strong>解析:</strong>A 、D、E</p><ul><li><p><strong>try-with-resources</strong></p><p>从<strong>jdk1.7</strong>开始,<strong>Java 7</strong>增强了 <code>try</code> 语句的功能——它允许在 <code>try</code> 关键字后跟一对圆括号,圆括号可以声明,初始化一个或多个资源,此处的资源指得是那些必须在程序结束时必须关闭的资源(比如数据库连接,网络连接等),<code>try</code> 语句在该语句结束时自动关闭这些资源。这种称为 <code>try-with-resources</code>语句。</p></li></ul><p><code>try (SqlSession sqlSession = sqlSessionFactory.openSession()) { //...... }</code><br>像这样的话,执行完 <code>sqlsession</code> 会自动关闭,不用我们在 <code>finally</code> 中关闭,也再也不用担心忘记关闭了。<br>那么为什么这样可以自动关闭资源呢?是不是所有的资源都可以这样关闭呢?<br><strong>实际上只要这些资源实现类实现了 <code>Closeable</code> 或 <code>AutoCloseable</code> 接口,就可以自动关闭</strong>。比如 <code>Sqlsession</code> 它就是 <code>extends Closeable</code>, <code>Closeable extends AutoCloseable</code>。</p><p>几乎所有的资源都可以用这种方式实现自动关闭资源,比如 <code>OutputStream</code>,<code>BufferedReader</code>,<code>PrintStream</code>,<code>InputStream</code> 等,都可以。据说到目前为止,只有<code>JavaMail Transport</code> 对象不能利用这种方式实现自动关闭。</p><blockquote><p><strong>注意</strong>:</p><ol><li><code>try-with-resources</code> 与 <code>AutoCloseable</code> 的 <code>close</code> 执行顺序 <strong>try-close-catch-finally</strong></li><li><strong>多资源用分号隔开,执行close的顺序为后声明的先执行;即按照创建资源时相反的顺序进行关闭</strong></li><li>多资源 close 异常,不论前面 close 是否出现异常,后面的 close 一定会执行,异常抛出只能捕获<strong>最先</strong>执行 close 错误的异常。</li></ol></blockquote><ul><li><strong>B.</strong> 在写异常处理的时候,一定要把异常范围小的放在前面,范围大的放在后面,Exception这个异常的根类一定要放在最后一个catch里面,如果放在前面或者中间,任何异常都会和Exception匹配的,就会报<strong>已捕获到…异常的错误</strong>。故 B 错误</li><li><strong>C.</strong> 在try块之后,我们可以使用 <strong>catch</strong> 块或 <strong>finally</strong> 块。</li></ul><h2 id="19-下面哪些是-NIO-包特点(本题6分)"><a href="#19-下面哪些是-NIO-包特点(本题6分)" class="headerlink" title="19. 下面哪些是 NIO 包特点(本题6分)"></a>19. 下面哪些是 NIO 包特点(本题6分)</h2><p>A. 文件锁定</p><p>B. 字符及编码</p><p>C. 内存映射技术</p><p>D. 阻塞I/O</p><p><strong>解析:</strong> A、B、C</p><p><strong>NIO</strong> 的特点:</p><ol><li><strong>内存映射</strong> 这个功能主要是为了提高大文件的读写速度而设计的</li><li><strong>字符及编码</strong></li><li><strong>非阻塞I/O</strong></li><li><strong>文件锁定</strong> 文件锁的出现解决了很多Java应用程序和非Java程序之间共享文件数据的问题</li></ol><h2 id="20-如下复合索引(a-b-c),实际生效的索引是?(本题5分)"><a href="#20-如下复合索引(a-b-c),实际生效的索引是?(本题5分)" class="headerlink" title="20. 如下复合索引(a,b,c),实际生效的索引是?(本题5分)"></a>20. 如下复合索引(a,b,c),实际生效的索引是?(本题5分)</h2><p>A. <strong>(a)</strong></p><p>B. <strong>(b,a)</strong></p><p>C. <strong>(a,c,b)</strong></p><p>D. <strong>(a,b)</strong></p><p><strong>解析:</strong> A、B、C、D</p><p>组合索引可以这样理解,比如(a,b,c),<strong>abc</strong> 都是排好序的,在任意一段 <strong>a</strong> 的下面 <strong>b</strong> 都是排好序的,任何一段 <strong>b</strong> 下面 <strong>c</strong> 都是排好序的;</p><p>组合索引的生效原则是 从前往后依次使用生效,如果中间某个索引没有使用,那么断点前面的索引部分起作用,断点后面的索引没有起作用;</p><p>比如:</p><pre class="line-numbers language-sql"><code class="language-sql"><span class="token keyword">where</span> <span class="token number">a</span><span class="token operator">=</span><span class="token number">3</span> <span class="token operator">and</span> <span class="token number">b</span><span class="token operator">=</span><span class="token number">4</span> <span class="token operator">and</span> <span class="token number">c</span><span class="token operator">=</span><span class="token number">5</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> 这种三个索引顺序使用中间没有断点,全部发挥作用;<span class="token keyword">where</span> <span class="token number">a</span><span class="token operator">=</span><span class="token number">3</span> <span class="token operator">and</span> <span class="token number">c</span><span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> 这种情况下<span class="token number">b</span>就是断点,<span class="token number">a</span>发挥了效果,<span class="token number">c</span>没有效果<span class="token keyword">where</span> <span class="token number">b</span><span class="token operator">=</span><span class="token number">3</span> <span class="token operator">and</span> <span class="token number">c</span><span class="token operator">=</span><span class="token number">4</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> 这种情况下<span class="token number">a</span>就是断点,在<span class="token number">a</span>后面的索引都没有发挥作用,这种写法联合索引没有发挥任 何效果;<span class="token keyword">where</span> <span class="token number">b</span><span class="token operator">=</span><span class="token number">4</span> <span class="token operator">and</span> <span class="token number">a</span><span class="token operator">=</span><span class="token number">3</span> <span class="token operator">and</span> <span class="token number">c</span><span class="token operator">=</span><span class="token number">5</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> 这个跟第一个一样<span class="token punctuation">(</span>原因:mysql自动优化<span class="token punctuation">)</span>,全部发挥作用,<span class="token number">abc</span>只要 用上了就行,跟写的顺序无关<span class="token keyword">where</span> <span class="token number">a</span><span class="token operator">=</span><span class="token number">3</span> <span class="token operator">and</span> <span class="token number">b</span><span class="token operator">></span><span class="token number">7</span> <span class="token operator">and</span> <span class="token number">c</span><span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">---- b范围值,断点,阻塞了c的索引</span> <span class="token number">a</span>用到了,<span class="token number">b</span>也用到了,<span class="token number">c</span>没有用到,这个地方<span class="token number">b</span>是范围值,也算断点,只不过自身用到了索引<span class="token keyword">where</span> <span class="token number">a</span><span class="token operator">></span><span class="token number">4</span> <span class="token operator">and</span> <span class="token number">b</span><span class="token operator">=</span><span class="token number">7</span> <span class="token operator">and</span> <span class="token number">c</span><span class="token operator">=</span><span class="token number">9</span><span class="token punctuation">;</span> <span class="token number">a</span>用到了 <span class="token number">b</span>没有使用,<span class="token number">c</span>没有使用(<span class="token number">a</span>用了范围所以,相当于断点,之后的 <span class="token number">b</span>,<span class="token number">c</span>都没有用到索引) <span class="token keyword">where</span> <span class="token number">a</span><span class="token operator">=</span><span class="token number">3</span> <span class="token keyword">order</span> <span class="token keyword">by</span> <span class="token number">b</span><span class="token punctuation">;</span> <span class="token number">a</span>用到了索引,<span class="token number">b</span>在结果排序中也用到了索引的效果,<span class="token number">a</span>下面任意一段的<span class="token number">b</span>是 排好序的<span class="token keyword">where</span> <span class="token number">a</span><span class="token operator">=</span><span class="token number">3</span> <span class="token keyword">order</span> <span class="token keyword">by</span> <span class="token number">c</span><span class="token punctuation">;</span> <span class="token number">a</span>用到了索引,但是这个地方<span class="token number">c</span>没有发挥排序效果,因为中间断点了,使 用<span class="token keyword">explain</span> 可以看到 filesort<span class="token keyword">where</span> <span class="token number">b</span><span class="token operator">=</span><span class="token number">3</span> <span class="token keyword">order</span> <span class="token keyword">by</span> <span class="token number">a</span><span class="token punctuation">;</span> <span class="token number">b</span>没有用到索引,排序中<span class="token number">a</span>也没有发挥索引效果 <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h1 id="三、判断题(每题2分)"><a href="#三、判断题(每题2分)" class="headerlink" title="三、判断题(每题2分)"></a>三、判断题(每题2分)</h1><h2 id="21-HashMap-和-TreeMap都允许-key-或-value-的值为-null。"><a href="#21-HashMap-和-TreeMap都允许-key-或-value-的值为-null。" class="headerlink" title="21. HashMap 和 TreeMap都允许 key 或 value 的值为 null。"></a>21. HashMap 和 TreeMap都允许 key 或 value 的值为 null。</h2><p><strong>解析:</strong> <strong>错误</strong></p><p>HashMap 支持 null 键和 null 值</p><p>TreeMap 不支持 null 键,但是支持 null 值</p><h2 id="22-Hashtable-和-ConcurrentHashMap-都不允许-key-或-value-的值为-null。"><a href="#22-Hashtable-和-ConcurrentHashMap-都不允许-key-或-value-的值为-null。" class="headerlink" title="22. Hashtable 和 ConcurrentHashMap 都不允许 key 或 value 的值为 null。"></a>22. Hashtable 和 ConcurrentHashMap 都不允许 key 或 value 的值为 null。</h2><p><strong>解析:****正确</strong></p><p>ConcurrentHashMap 和 Hashtable 都是支持并发的,二者规定 key,value 均不能为 null,null的话,会抛出空指针异常。</p><h2 id="23-程序使用-Thread-sleep-0-语句时,可以删除该行代码,程序运行效果时一样的。"><a href="#23-程序使用-Thread-sleep-0-语句时,可以删除该行代码,程序运行效果时一样的。" class="headerlink" title="23. 程序使用 Thread.sleep(0) 语句时,可以删除该行代码,程序运行效果时一样的。"></a>23. 程序使用 Thread.sleep(0) 语句时,可以删除该行代码,程序运行效果时一样的。</h2><p><strong>解析:</strong> <strong>错误</strong></p><p>执行 Thread.sleep(0) 后,当前线程不一定会被唤醒,虽然休眠了0秒,但是执行 sleep 方法后,不仅会休眠,还会让 CPU 重新分配。因此,Thread.Sleep(0) 的作用,就是“触发操作系统立刻重新进行一次 CPU竞争”。竞争的结果也许是当前线程仍然获得 CPU 控制权,也许会换成别的线程获得 CPU 控制权。这也是我们在大循环里面经常会写一句 Thread.Sleep(0) ,因为这样就给了其他线程获得 CPU 控制权的权力,这样一些功能就不会假死在那里</p><h2 id="24-wait-方法必须用在同步代码块或方法中。"><a href="#24-wait-方法必须用在同步代码块或方法中。" class="headerlink" title="24. wait() 方法必须用在同步代码块或方法中。"></a>24. wait() 方法必须用在同步代码块或方法中。</h2><p><strong>解析:</strong> <strong>正确</strong></p><p>Java强制我们的wait()/notify()调用必须要在一个同步块中,就是不想让我们在不经意间出现这种 lost wake up 问题。</p><p>不仅仅是这两个方法,包括 java.util.concurrent.locks.Condition 的await()/signal()也必须要在同步块中</p><h2 id="25-sleep-和-wait-都会释放持有的锁资源。"><a href="#25-sleep-和-wait-都会释放持有的锁资源。" class="headerlink" title="25. sleep 和 wait 都会释放持有的锁资源。"></a>25. sleep 和 wait 都会释放持有的锁资源。</h2><p><strong>解析:</strong> <strong>错误</strong></p><p>sleep,wait 调用后都会暂停当前线程并让出 cpu 的执行时间,但不同的是 <strong>sleep</strong> 不会释放当前持有的对象的锁资源,到时间后会继续执行,而 wait 会放弃所有锁并需要 notify/notifyAll 后重新获取到对象锁资源后才能继续执行。</p><h2 id="26-可以通过-for-循环遍历删除集合中的元素。"><a href="#26-可以通过-for-循环遍历删除集合中的元素。" class="headerlink" title="26. 可以通过 for 循环遍历删除集合中的元素。"></a>26. 可以通过 for 循环遍历删除集合中的元素。</h2><p><strong>解析:</strong> <strong>错误</strong></p><p>for循环只能用来遍历集合,不可以在遍历的同时进行修改和删除</p><h2 id="27-变量定义-String-1abc-“abc”,是可以编译通过的。"><a href="#27-变量定义-String-1abc-“abc”,是可以编译通过的。" class="headerlink" title="27. 变量定义 String 1abc =“abc”,是可以编译通过的。"></a>27. 变量定义 String 1abc =“abc”,是可以编译通过的。</h2><p><strong>解析:</strong> <strong>错误</strong></p><p><strong>变量的命名要求符合标识符的语法要求:</strong><br> a. 变量名可以有字母、数字、_、$组成<br> b. 变量名不能以数字开头<br> c. Java中变量名的大小写是敏感的<br> d. 不能使用Java中的保留字作为变量名(保留字:Java中规定好的,一些有特殊含义的词)<br> e. 中文可以作为变量名,但不提倡使用<br> f. 变量的命名要见名知意,命名需要遵循驼峰命令规则(如果变量的名字由多个单词组成,除第一个单词以外,其它单词的首字母大写,其余字母小写,尽量不要用拼音来命名变量)</p><h2 id="28-volatile-关键字可以保证变量的可见性、有序性、原子性。"><a href="#28-volatile-关键字可以保证变量的可见性、有序性、原子性。" class="headerlink" title="28. volatile 关键字可以保证变量的可见性、有序性、原子性。"></a>28. volatile 关键字可以保证变量的可见性、有序性、原子性。</h2><p><strong>解析:</strong> <strong>错误</strong></p><p>volatile 不能保证变量的原子性。</p><h2 id="29-static-修饰的方法不能调用非-static-修饰的方法或变量。"><a href="#29-static-修饰的方法不能调用非-static-修饰的方法或变量。" class="headerlink" title="29. static 修饰的方法不能调用非 static 修饰的方法或变量。"></a>29. static 修饰的方法不能调用非 static 修饰的方法或变量。</h2><p><strong>解析:</strong> <strong>正确</strong></p><h2 id="30-抽象类不可以使用-final-关键字来修饰。"><a href="#30-抽象类不可以使用-final-关键字来修饰。" class="headerlink" title="30. 抽象类不可以使用 final 关键字来修饰。"></a>30. 抽象类不可以使用 final 关键字来修饰。</h2><p><strong>解析:</strong> <strong>正确</strong></p><p>抽象类的就是要子类继承然后实现内部方法的。但是final修饰的类是不能再被继承和修改的。所以不能用final修饰。</p><h1 id="注意:"><a href="#注意:" class="headerlink" title="注意:"></a>注意:</h1><p>以上答案 仅个人意见,如果有错误,欢迎指正,谢谢。</p>]]></content>
<categories>
<category> 笔试题 </category>
</categories>
<tags>
<tag> Java </tag>
</tags>
</entry>
<entry>
<title>多线程</title>
<link href="post/5729df21/"/>
<url>post/5729df21/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><h1 id="1、程序-进程-线程"><a href="#1、程序-进程-线程" class="headerlink" title="1、程序.进程.线程"></a>1、程序.进程.线程</h1><ul><li>程序是指令和数据的有序集合,其本身没有任何运行的含义,是一个静态的概念。</li><li>进程是执行程序的一次执行过程,它是一个动态的概念。是系统资源分配的单位。</li><li>通常在一个进程中可以包含若干个线程,当然一个进程中至少有一个线程,不然没有存在的意义。线程是CPU调度和执行的单位。</li></ul><blockquote><p>小贴士:</p><p> 很多多线程是模拟出来的,真正的多线程是指有多个CPU,即多核,如服务器。如果是模拟出来的多线程,即在一个CPU的情况下,在同一个时间点,CPU只能执行一个代码,因为切换的很快,所以就有同时执行的错觉。</p></blockquote><p>总结:</p><ul><li>线程就是独立的执行路径;</li><li>在程序运行时,即使没有自己创建线程,后台也会有多个线程,如主线程,gc线程;</li><li>main()称之为主线程,为系统的入口,用于执行整个线程;</li><li>在一个进程中,如果开辟了多个线程,线程的运行由调度器安排调度,调度器是与操作系统紧密相关的,先后顺序是不能人为干预的。</li><li>对同一份资源操作时,会存在资源抢夺的问题,需要加入并发控制;</li><li>线程会带来额外的开销,如CPU调度时间,并发控制开销;</li><li>每个线程在自己的工作内存交互,内存控制不当会造成数据不一致。</li></ul><h1 id="2、线程创建"><a href="#2、线程创建" class="headerlink" title="2、线程创建"></a>2、线程创建</h1><p><code>Thread</code>、<code>Runnable</code>、<code>Callable</code></p><p><strong>继承<code>Thread</code>类</strong>和 <strong>实现<code>Runnable</code>接口</strong> 为重点,实现<code>Callable</code>接口仅作了解</p><h1 id="3、Thread"><a href="#3、Thread" class="headerlink" title="3、Thread"></a>3、Thread</h1><ul><li>自定义线程类继承<code>Thread</code>类</li><li>重写<code>run()</code>方法,编写线程执行体</li><li>创建线程对象,调用<code>start()</code>方法启动线程</li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestThread1</span> <span class="token keyword">extends</span> <span class="token class-name">Thread</span><span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// run方法线程体</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">200</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"正在执行线程----"</span> <span class="token operator">+</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// main主线程</span> <span class="token comment" spellcheck="true">// 创建一个线程对象</span> TestThread1 testThread1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TestThread1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//调用start()方法开启线程</span> testThread1<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">2000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"正在执行主方法******"</span> <span class="token operator">+</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>小贴士:</p><p> 线程开启不一定立即执行,由CPU进行调度执行</p></blockquote><h2 id="3-1-案例-多线程同步下载图片"><a href="#3-1-案例-多线程同步下载图片" class="headerlink" title="3.1 案例-多线程同步下载图片"></a>3.1 案例-多线程同步下载图片</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>jiangqi<span class="token punctuation">.</span>demo<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>commons<span class="token punctuation">.</span>io<span class="token punctuation">.</span>FileUtils<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>File<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>IOException<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>net<span class="token punctuation">.</span>URL<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p>练习Thread,实现多线程同步下载图片</p> * */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestThread2</span> <span class="token keyword">extends</span> <span class="token class-name">Thread</span><span class="token punctuation">{</span> <span class="token keyword">private</span> String url<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 网络图片地址</span> <span class="token keyword">private</span> String name<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 保存的文件名</span> <span class="token keyword">public</span> <span class="token function">TestThread2</span><span class="token punctuation">(</span>String url<span class="token punctuation">,</span> String name<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>url <span class="token operator">=</span> url<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> WebDownloader webDownloader<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">WebDownloader</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> webDownloader<span class="token punctuation">.</span><span class="token function">downloader</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"下载了文件名为:"</span><span class="token operator">+</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> TestThread2 t1<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">TestThread2</span><span class="token punctuation">(</span><span class="token string">"http://img.netbian.com/file/2021/0502/17d449ebe5f05a7d0e026773b1601ef7.jpg"</span><span class="token punctuation">,</span><span class="token string">"1.jpg"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> TestThread2 t2<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">TestThread2</span><span class="token punctuation">(</span><span class="token string">"https://www.4kbizhi.com/d/file/2021/04/28/small123244uA7An1619584364.jpg"</span><span class="token punctuation">,</span><span class="token string">"2.jpg"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> TestThread2 t3<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">TestThread2</span><span class="token punctuation">(</span><span class="token string">"https://www.4kbizhi.com/d/file/2020/09/21/small174423ZTNBA1600681463.jpg"</span><span class="token punctuation">,</span><span class="token string">"3.jpg"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t1<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t2<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t3<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// 下载器</span><span class="token keyword">class</span> <span class="token class-name">WebDownloader</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 下载方法</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">downloader</span><span class="token punctuation">(</span>String url<span class="token punctuation">,</span>String name<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> FileUtils<span class="token punctuation">.</span><span class="token function">copyURLToFile</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">URL</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">catch</span><span class="token punctuation">(</span>IOException e<span class="token punctuation">)</span><span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"IO异常,downloader方法出现问题"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h1 id="4、Runnable"><a href="#4、Runnable" class="headerlink" title="4、Runnable"></a>4、Runnable</h1><ul><li>定义MyRunnable类实现<code>Runnable</code>接口</li><li>实现<code>run()</code>方法,编写线程执行体</li><li>创建线程对象,调用<code>start()</code>方法启动线程</li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestThread3</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span><span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// run方法线程体</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">200</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"正在执行线程----"</span> <span class="token operator">+</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// main主线程</span> <span class="token comment" spellcheck="true">// 创建Runnable接口的实现类对象</span> TestThread3 testThread3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TestThread3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 创建线程对象,通过线程对象来开启我们的线程,代理</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>testThread3<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">2000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"正在执行主方法******"</span> <span class="token operator">+</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h1 id="5、Runnable-与-Thread-比较"><a href="#5、Runnable-与-Thread-比较" class="headerlink" title="5、Runnable 与 Thread 比较"></a>5、Runnable 与 Thread 比较</h1><table><thead><tr><th align="center">继承Thread类</th><th align="center">实现Runnable接口</th></tr></thead><tbody><tr><td align="center">子类继承<code>Thread</code>类具备多线程能力</td><td align="center">实现接口<code>Runnable</code>具有多线程能力</td></tr><tr><td align="center">启动线程:子类对象<code>.start()</code>;</td><td align="center">启动线程:传入目标对象+<code>Thread</code>对象<code>.start()</code>;</td></tr><tr><td align="center">不建议使用:避免<code>OOP</code>单继承局限性</td><td align="center">推荐使用:避免单继承局限性,灵活方便,方便同一个对象被多个线程使用</td></tr></tbody></table><h1 id="6、案例"><a href="#6、案例" class="headerlink" title="6、案例"></a>6、案例</h1><h2 id="6-1-案例1-购买火车票"><a href="#6-1-案例1-购买火车票" class="headerlink" title="6.1 案例1-购买火车票"></a>6.1 案例1-购买火车票</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>jiangqi<span class="token punctuation">.</span>demo<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p> * 多个线程同时操作同一个对象 * 买火车票的例子 * </p> * */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestThread4</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 票数</span> <span class="token keyword">private</span> <span class="token keyword">int</span> ticketNums<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ticketNums <span class="token operator"><=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 模拟延时</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// Thread.currentThread()获得线程本身</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">" --> 拿到了第"</span> <span class="token operator">+</span> ticketNums<span class="token operator">--</span> <span class="token operator">+</span> <span class="token string">"票"</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> TestThread4 ts <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TestThread4</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>ts<span class="token punctuation">,</span> <span class="token string">"stu"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>ts<span class="token punctuation">,</span> <span class="token string">"teacher"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>ts<span class="token punctuation">,</span> <span class="token string">"gangster"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>总结:</p><p>通过本例子发现问题:多个线程操作同一个资源的情况下,线程不安全,数据紊乱</p></blockquote><h2 id="6-2-案例2-龟兔赛跑"><a href="#6-2-案例2-龟兔赛跑" class="headerlink" title="6.2 案例2-龟兔赛跑"></a>6.2 案例2-龟兔赛跑</h2><ol><li>首先来个赛道距离,然后要离重点越来越近</li><li>判断比赛是否结束</li><li>打印出胜利者</li><li>龟兔赛跑开始</li><li>故事中是乌龟赢得,兔子需要睡觉,所以我们模拟兔子睡觉</li><li>终于乌龟赢得比赛</li></ol><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>jiangqi<span class="token punctuation">.</span>demo<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p>模拟龟兔赛跑</p> * */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Race</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 胜利者</span> <span class="token keyword">private</span> <span class="token keyword">static</span> String winner<span class="token punctuation">;</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> <span class="token number">100</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 模拟兔子休息</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"兔子"</span><span class="token punctuation">)</span> <span class="token operator">&&</span> i <span class="token operator">%</span> <span class="token number">10</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 判断比赛是否结束</span> <span class="token keyword">boolean</span> flag <span class="token operator">=</span> <span class="token function">gameOver</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 如果比赛结束 停止程序</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>flag<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"-->跑了"</span> <span class="token operator">+</span> i <span class="token operator">+</span> <span class="token string">"步"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 判断是否完成比赛</span> <span class="token keyword">private</span> <span class="token keyword">boolean</span> <span class="token function">gameOver</span><span class="token punctuation">(</span><span class="token keyword">int</span> steps<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 判断是否有胜利者</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>winner <span class="token operator">!=</span> null<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 已经存在胜利者</span> <span class="token punctuation">}</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>steps <span class="token operator">>=</span> <span class="token number">100</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> winner <span class="token operator">=</span> Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"winner is "</span> <span class="token operator">+</span> winner<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> Race r1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Race</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>r1<span class="token punctuation">,</span> <span class="token string">"兔子"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>r1<span class="token punctuation">,</span> <span class="token string">"乌龟"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h1 id="7、实现Callable接口-(了解即可)"><a href="#7、实现Callable接口-(了解即可)" class="headerlink" title="7、实现Callable接口 (了解即可)"></a>7、实现Callable接口 (了解即可)</h1><ol><li>实现Callable接口,需要返回值类型</li><li>重写call方法,需要抛出异常</li><li>创建目标对象</li><li>创建执行服务 <code>ExecutorService ser = Executors.newFixedThreadPool(1);</code></li><li>提交执行 <code>Future<Boolean> result = ser.submit(t1);</code></li><li>获取结果 <code>boolean r1 = result.get();</code></li><li>关闭服务 <code>ser.shutdownNow();</code></li></ol><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>jiangqi<span class="token punctuation">.</span>demo2<span class="token punctuation">;</span><span class="token keyword">import</span> org<span class="token punctuation">.</span>apache<span class="token punctuation">.</span>commons<span class="token punctuation">.</span>io<span class="token punctuation">.</span>FileUtils<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>File<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>IOException<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>net<span class="token punctuation">.</span>URL<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>concurrent<span class="token punctuation">.</span>*<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p>练习Callable,实现多线程同步下载图片</p> * */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestCallable</span> <span class="token keyword">implements</span> <span class="token class-name">Callable</span><span class="token operator"><</span>Boolean<span class="token operator">></span> <span class="token punctuation">{</span> <span class="token keyword">private</span> String url<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 网络图片地址</span> <span class="token keyword">private</span> String name<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 保存的文件名</span> <span class="token keyword">public</span> <span class="token function">TestCallable</span><span class="token punctuation">(</span>String url<span class="token punctuation">,</span> String name<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>url <span class="token operator">=</span> url<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> Boolean <span class="token function">call</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> WebDownloader webDownloader<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">WebDownloader</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> webDownloader<span class="token punctuation">.</span><span class="token function">downloader</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"下载了文件名为:"</span><span class="token operator">+</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> ExecutionException<span class="token punctuation">,</span> InterruptedException <span class="token punctuation">{</span> TestCallable t1<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">TestCallable</span><span class="token punctuation">(</span><span class="token string">"http://img.netbian.com/file/2021/0502/17d449ebe5f05a7d0e026773b1601ef7.jpg"</span><span class="token punctuation">,</span><span class="token string">"1.jpg"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> TestCallable t2<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">TestCallable</span><span class="token punctuation">(</span><span class="token string">"https://www.4kbizhi.com/d/file/2021/04/28/small123244uA7An1619584364.jpg"</span><span class="token punctuation">,</span><span class="token string">"2.jpg"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> TestCallable t3<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">TestCallable</span><span class="token punctuation">(</span><span class="token string">"https://www.4kbizhi.com/d/file/2020/09/21/small174423ZTNBA1600681463.jpg"</span><span class="token punctuation">,</span><span class="token string">"3.jpg"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 1. 创建执行服务</span> ExecutorService ser <span class="token operator">=</span> Executors<span class="token punctuation">.</span><span class="token function">newFixedThreadPool</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 2. 提交执行</span> Future<span class="token operator"><</span>Boolean<span class="token operator">></span> r1 <span class="token operator">=</span> ser<span class="token punctuation">.</span><span class="token function">submit</span><span class="token punctuation">(</span>t1<span class="token punctuation">)</span><span class="token punctuation">;</span> Future<span class="token operator"><</span>Boolean<span class="token operator">></span> r2 <span class="token operator">=</span> ser<span class="token punctuation">.</span><span class="token function">submit</span><span class="token punctuation">(</span>t2<span class="token punctuation">)</span><span class="token punctuation">;</span> Future<span class="token operator"><</span>Boolean<span class="token operator">></span> r3 <span class="token operator">=</span> ser<span class="token punctuation">.</span><span class="token function">submit</span><span class="token punctuation">(</span>t3<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 3. 获取结果</span> <span class="token keyword">boolean</span> rs1 <span class="token operator">=</span> r1<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">boolean</span> rs2 <span class="token operator">=</span> r2<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">boolean</span> rs3 <span class="token operator">=</span> r3<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 4. 关闭服务</span> ser<span class="token punctuation">.</span><span class="token function">shutdownNow</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// 下载器</span><span class="token keyword">class</span> <span class="token class-name">WebDownloader</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 下载方法</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">downloader</span><span class="token punctuation">(</span>String url<span class="token punctuation">,</span>String name<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> FileUtils<span class="token punctuation">.</span><span class="token function">copyURLToFile</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">URL</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">catch</span><span class="token punctuation">(</span>IOException e<span class="token punctuation">)</span><span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"IO异常,downloader方法出现问题"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p><code>Callable</code> 的好处</p><ol><li>可以定义返回值</li><li>可以抛出异常</li></ol></blockquote><h1 id="8、静态代理"><a href="#8、静态代理" class="headerlink" title="8、静态代理"></a>8、静态代理</h1><h2 id="8-1-个人结婚与婚庆公司的例子"><a href="#8-1-个人结婚与婚庆公司的例子" class="headerlink" title="8.1 个人结婚与婚庆公司的例子"></a>8.1 个人结婚与婚庆公司的例子</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">/** * <p>静态代理</p> * 总结: * 真实对象和代理对象都要实现同一个接口 * 代理对象要代理真实角色 * 好处: * 代理对象可以做很多真实对象做不了的事情 * 真实对象专注做自己的事情 */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StaticProxy</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"我爱你"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// lambda表达式</span> <span class="token keyword">new</span> <span class="token class-name">WeddingCompany</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">You</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">happyMarry</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">interface</span> <span class="token class-name">Marry</span><span class="token punctuation">{</span> <span class="token keyword">void</span> <span class="token function">happyMarry</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// 真实角色,你去结婚</span><span class="token keyword">class</span> <span class="token class-name">You</span> <span class="token keyword">implements</span> <span class="token class-name">Marry</span><span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">happyMarry</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"要结婚了,超开心啊"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// 代理角色,帮助你结婚</span><span class="token keyword">class</span> <span class="token class-name">WeddingCompany</span> <span class="token keyword">implements</span> <span class="token class-name">Marry</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 代理谁--》真实目标角色</span> <span class="token keyword">private</span> Marry target<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token function">WeddingCompany</span><span class="token punctuation">(</span>Marry target<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>target <span class="token operator">=</span> target<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">happyMarry</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">before</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>target<span class="token punctuation">.</span><span class="token function">happyMarry</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 这就是真实对象</span> <span class="token function">after</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">after</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"结婚之后,收尾款"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">before</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"结婚之前,布置现场"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="8-2-总结"><a href="#8-2-总结" class="headerlink" title="8.2 总结"></a>8.2 总结</h2><p>以上可得出:线程的<strong>底层</strong>实现原理之一:<strong>静态代理模式</strong></p><h1 id="9、Lambda-表达式"><a href="#9、Lambda-表达式" class="headerlink" title="9、Lambda 表达式"></a>9、Lambda 表达式</h1><ul><li><p> 为什么要使用lambda表达式</p></li><li><p>避免匿名内部类定义过多</p></li><li><p>可以让代码看起来更简洁</p></li><li><p>去掉一堆无意义代码,只留下核心逻辑</p></li><li><p>理解<strong>Functional Interface</strong>(函数式接口) 是学习<strong>Java8 lambda</strong>表达式的关键所在。</p></li><li><p>函数式接口的定义:</p><p>任何接口,如果只包含唯一一个抽象方法,那么它就是一个函数式接口。例子如下:</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">interface</span> <span class="token class-name">ILike</span><span class="token punctuation">{</span> <span class="token keyword">void</span> <span class="token function">lambda</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p>对于函数式接口,我们可以通过lambda表达式来创建该接口的对象。</p><pre class="line-numbers language-java"><code class="language-java"><span class="token punctuation">(</span>params<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> expression<span class="token punctuation">[</span>表达式<span class="token punctuation">]</span><span class="token punctuation">(</span>params<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> statement<span class="token punctuation">[</span>语句<span class="token punctuation">]</span><span class="token punctuation">(</span>params<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span>statements<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></li></ul><h2 id="9-1-无参lambda实现"><a href="#9-1-无参lambda实现" class="headerlink" title="9.1 无参lambda实现"></a>9.1 无参lambda实现</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>jiangqi<span class="token punctuation">.</span>lambda<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p>推导lambda表达式</p> * * @author 江七 * @date 2021/5/23 16:13. */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestLambda1</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 3.静态内部类</span> <span class="token keyword">static</span> <span class="token keyword">class</span> <span class="token class-name">Like2</span> <span class="token keyword">implements</span> <span class="token class-name">ILike</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">lambda</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"I like lambda2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> ILike like <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Like</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> like<span class="token punctuation">.</span><span class="token function">lambda</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> like <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Like2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> like<span class="token punctuation">.</span><span class="token function">lambda</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 4.局部内部类</span> <span class="token keyword">class</span> <span class="token class-name">Like3</span> <span class="token keyword">implements</span> <span class="token class-name">ILike</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">lambda</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"I like lambda3"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> like <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Like3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> like<span class="token punctuation">.</span><span class="token function">lambda</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 5.匿名内部类</span> like <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ILike</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">lambda</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"I like lambda4"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> like<span class="token punctuation">.</span><span class="token function">lambda</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 6.用lambda 简化</span> like <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"I like lambda5"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> like<span class="token punctuation">.</span><span class="token function">lambda</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// 1.定义一个函数式接口</span><span class="token keyword">interface</span> <span class="token class-name">ILike</span> <span class="token punctuation">{</span> <span class="token keyword">void</span> <span class="token function">lambda</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// 2.实现类</span><span class="token keyword">class</span> <span class="token class-name">Like</span> <span class="token keyword">implements</span> <span class="token class-name">ILike</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">lambda</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"I like lambda"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="9-2-有参lambda实现和简化"><a href="#9-2-有参lambda实现和简化" class="headerlink" title="9.2 有参lambda实现和简化"></a>9.2 有参lambda实现和简化</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>jiangqi<span class="token punctuation">.</span>lambda<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p>有参实现</p> */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestLambda2</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> ILove love<span class="token operator">=</span>null<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 1.lambda表达式简化</span><span class="token comment" spellcheck="true">// love=(int a)->{</span><span class="token comment" spellcheck="true">// System.out.println("I love you-->"+a);</span><span class="token comment" spellcheck="true">// };</span> love<span class="token operator">=</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span><span class="token keyword">int</span> b<span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"I love you-->"</span><span class="token operator">+</span>a<span class="token operator">+</span><span class="token string">","</span><span class="token operator">+</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> love<span class="token punctuation">.</span><span class="token function">love</span><span class="token punctuation">(</span><span class="token number">520</span><span class="token punctuation">,</span><span class="token number">502</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 简化 1.参数类型</span><span class="token comment" spellcheck="true">// love=(a)->{</span><span class="token comment" spellcheck="true">// System.out.println("I love you-->"+a);</span><span class="token comment" spellcheck="true">// };</span> love<span class="token operator">=</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"I love you-->"</span><span class="token operator">+</span>a<span class="token operator">+</span><span class="token string">","</span><span class="token operator">+</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> love<span class="token punctuation">.</span><span class="token function">love</span><span class="token punctuation">(</span><span class="token number">521</span><span class="token punctuation">,</span><span class="token number">502</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 简化 2.简化括号</span><span class="token comment" spellcheck="true">// love=a-> {</span><span class="token comment" spellcheck="true">// System.out.println("I love you-->"+a);</span><span class="token comment" spellcheck="true">// };</span><span class="token comment" spellcheck="true">// love.love(5201314);</span> <span class="token comment" spellcheck="true">// 简化 3.去掉花括号</span><span class="token comment" spellcheck="true">// love=a-> System.out.println("I love you-->"+a);</span> love<span class="token operator">=</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"I love you-->"</span><span class="token operator">+</span>a<span class="token operator">+</span><span class="token string">","</span><span class="token operator">+</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> love<span class="token punctuation">.</span><span class="token function">love</span><span class="token punctuation">(</span><span class="token number">5211314</span><span class="token punctuation">,</span><span class="token number">502</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">interface</span> <span class="token class-name">ILove</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// void love(int a);</span> <span class="token keyword">void</span> <span class="token function">love</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span><span class="token keyword">int</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>小贴士:</p><ol><li>lambda表达式只能有一行代码的情况下才能简化成为一行,如果有多行,那么就用代码块包裹; 2. 使用lambda表达式前提是接口为函数式接口; 3. 单个参数可以去掉括号 4. 多个参数也可以去掉参数类型,要去掉就都去掉,但必须加上括号</li></ol></blockquote><h1 id="10、线程状态"><a href="#10、线程状态" class="headerlink" title="10、线程状态"></a>10、线程状态</h1><ul><li><p>五大状态</p><p> 创建状态、就绪状态、阻塞状态、运行状态、死亡状态</p></li></ul><p><img src="/post/5729df21/%E7%BA%BF%E7%A8%8B%E7%8A%B6%E6%80%812.png" alt="线程状态"></p><ul><li><p>线程方法</p><table><thead><tr><th align="center">方 法</th><th align="center">说 明</th></tr></thead><tbody><tr><td align="center"><code>setPriority(int newPriority)</code></td><td align="center">更改线程的优先级</td></tr><tr><td align="center"><code>static void sleep(long millis)</code></td><td align="center">在指定的毫秒数内让当前正在执行的线程休眠</td></tr><tr><td align="center"><code>void join</code></td><td align="center">等待该线程终止</td></tr><tr><td align="center"><code>static void yield()</code></td><td align="center">暂停当前正在执行的线程对象,并执行其他线程</td></tr><tr><td align="center"><code>void interrupt()</code></td><td align="center">中断线程,别用这个方式</td></tr><tr><td align="center"><code>boolean isAlive</code></td><td align="center">测试线程是否处于活动状态</td></tr></tbody></table></li></ul><h2 id="10-1-停止线程-stop"><a href="#10-1-停止线程-stop" class="headerlink" title="10.1 停止线程(stop)"></a>10.1 停止线程(stop)</h2><ul><li>不推荐使用JDK提供的<code>stop(); destroy();</code>方法</li><li>推荐线程自己停止</li><li>建议使用一个标志位进行终止变量</li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> state<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p>测试stop</p> * 1. 建议线程正常停止-->利用次数,不建议死循环 * 2. 建议使用标志位-->设置一个标志位 * 3. 不要使用stop或者destroy等过时或者JDK不建议使用的方法 * @author 江七 * @date 2021/5/23 21:57. */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestStop</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1.设置一个标志位</span> <span class="token keyword">private</span> <span class="token keyword">boolean</span> flag<span class="token operator">=</span><span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>flag<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"run......Thread"</span><span class="token operator">+</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 2.设置一个公开的方法停止线程,转换标志位</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">stop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>flag<span class="token operator">=</span><span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> TestStop testStop<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">TestStop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>testStop<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// main 先执行</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator"><</span><span class="token number">1000</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"main"</span><span class="token operator">+</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i<span class="token operator">==</span><span class="token number">900</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 调用stop方法切换标志位,让线程停止</span> testStop<span class="token punctuation">.</span><span class="token function">stop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"线程该停止了"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="10-2-线程休眠-sleep"><a href="#10-2-线程休眠-sleep" class="headerlink" title="10.2 线程休眠(sleep)"></a>10.2 线程休眠(sleep)</h2><ul><li><code>sleep</code>(时间)指定当前线程阻塞的毫秒数</li><li><code>sleep</code>存在异常<code>InterruptedException</code></li><li><code>sleep</code>时间到达后线程进入就绪状态</li><li><code>sleep</code>可以模拟网络延时,倒计时等</li><li>每一个对象都有一个锁,sleep不会释放锁</li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> state<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>text<span class="token punctuation">.</span>SimpleDateFormat<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Date<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p>模拟倒计时</p> * * @author 江七 * @date 2021/5/23 22:15. */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestSleep2</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 打印当前系统时间</span> Date startTime <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>System<span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//获取系统当前时间</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">try</span><span class="token punctuation">{</span> Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SimpleDateFormat</span><span class="token punctuation">(</span><span class="token string">"HH:mm:ss"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>startTime<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> startTime<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>System<span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//更新系统当前时间</span> <span class="token punctuation">}</span><span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 模拟倒计时</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">tenDown</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> InterruptedException <span class="token punctuation">{</span> <span class="token keyword">int</span> num<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>num<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>num<span class="token operator"><=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="10-3-线程礼让-yield"><a href="#10-3-线程礼让-yield" class="headerlink" title="10.3 线程礼让(yield)"></a>10.3 线程礼让(yield)</h2><ul><li>礼让线程,让当前正在执行的线程暂停,但不阻塞</li><li>将线程从运行状态转为就绪状态</li><li><strong>让CPU重新调度,礼让不一定成功!看CPU心情</strong></li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> state<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p>测试礼让线程</p> * 礼让不一定成功,看CPU 心情 * @author 江七 * @date 2021/5/23 22:25. */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestYield</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> MyYield myYield<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">MyYield</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>myYield<span class="token punctuation">,</span><span class="token string">"a"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>myYield<span class="token punctuation">,</span><span class="token string">"b"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">class</span> <span class="token class-name">MyYield</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span><span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"线程开始执行"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Thread<span class="token punctuation">.</span><span class="token function">yield</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 礼让</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"线程停止执行"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="10-4-线程强制执行(join)"><a href="#10-4-线程强制执行(join)" class="headerlink" title="10.4 线程强制执行(join)"></a>10.4 线程强制执行(join)</h2><ul><li><code>join</code> 合并线程,待此线程执行完成后再执行其他线程,其他线程阻塞</li><li>可以想象成插队</li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> state<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p> * 测试Join方法 * 想象为插队 * </p> * * @author 江七 * @date 2021/5/23 22:32. */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestJoin</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span><span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span><span class="token number">1000</span> <span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"线程vip来了"</span><span class="token operator">+</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> InterruptedException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 启动我们的线程</span> TestJoin testJoin<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">TestJoin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Runnable target<span class="token punctuation">;</span> Thread thread<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>testJoin<span class="token punctuation">)</span><span class="token punctuation">;</span> thread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 主线程</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span><span class="token number">500</span> <span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>i<span class="token operator">==</span><span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">{</span> thread<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//插队</span> <span class="token punctuation">}</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"main"</span><span class="token operator">+</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>小贴士:</p><p> join是个很霸道的方法,强制执行,在线程里应少用该方法,否则会造成线程堵塞。</p></blockquote><h2 id="10-5-观测线程状态(state)"><a href="#10-5-观测线程状态(state)" class="headerlink" title="10.5 观测线程状态(state)"></a>10.5 观测线程状态(state)</h2><ul><li><p><code>Thread.State</code></p><p> 线程状态。线程可以处于以下状态之一:</p><ul><li><a href="../../java/lang/Thread.State.html#NEW"><code>NEW</code></a><br>尚未启动的线程处于此状态。 </li><li><a href="../../java/lang/Thread.State.html#RUNNABLE"><code>RUNNABLE</code></a><br>在Java虚拟机中执行的线程处于此状态。 </li><li><a href="../../java/lang/Thread.State.html#BLOCKED"><code>BLOCKED</code></a><br>被阻塞等待监视器锁定的线程处于此状态。 </li><li><a href="../../java/lang/Thread.State.html#WAITING"><code>WAITING</code></a><br>正在等待另一个线程执行特定动作的线程处于此状态。 </li><li><a href="../../java/lang/Thread.State.html#TIMED_WAITING"><code>TIMED_WAITING</code></a><br>正在等待另一个线程执行动作达到指定等待时间的线程处于此状态。 </li><li><a href="../../java/lang/Thread.State.html#TERMINATED"><code>TERMINATED</code></a><br>已退出的线程处于此状态。 </li></ul></li></ul><p>一个线程可以在给定时间点处于一个状态。 这些状态是不反映任何操作系统线程状态的虚拟机状态。 </p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> state<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p>观察测试线程状态</p> * * @author 江七 * @date 2021/5/23 22:46. */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestState</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> InterruptedException <span class="token punctuation">{</span> Thread thread<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span><span class="token number">5</span> <span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">try</span><span class="token punctuation">{</span> Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"////////////////"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 观察状态</span> Thread<span class="token punctuation">.</span>State state<span class="token operator">=</span>thread<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>state<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//NEW</span> <span class="token comment" spellcheck="true">// 观察启动后</span> thread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 启动线程</span> state<span class="token operator">=</span>thread<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>state<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//Runnable</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>state<span class="token operator">!=</span>Thread<span class="token punctuation">.</span>State<span class="token punctuation">.</span>TERMINATED<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 只要线程不终止,就一直输出状态</span> Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> state<span class="token operator">=</span>thread<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//更新线程状态</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>state<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//输出状态</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// thread.start(); // 死亡后的线程不能再次启动</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>小贴士:</p><p> 线程不能启动两次,死亡之后的线程就不能再次启动了。</p></blockquote><h2 id="10-6-线程优先级-priority"><a href="#10-6-线程优先级-priority" class="headerlink" title="10.6 线程优先级(priority)"></a>10.6 线程优先级(priority)</h2><ul><li><code>java</code>提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器按照优先级决定应该调度哪个线程来执行</li><li>线程的优先级用数字表示,范围从1~10 <ul><li><code>Thread.MIN_PRIORITY = 1;</code></li><li><code>Thread.MAX_PRIORITY = 10;</code></li><li><code>Thread.NORM_PRIORITY = 5;</code></li></ul></li><li>使用以下方式改变或获取优先级 <ul><li><code>getPriority().setPriority(int xxx);</code></li></ul></li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> state<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p>测试线程的优先级</p> * * @author 江七 * @date 2021/6/4 9:49. */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestPriority</span><span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 主线程默认优先级</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"-->"</span><span class="token operator">+</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getPriority</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> MyPriority myPriority<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">MyPriority</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Thread t1<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>myPriority<span class="token punctuation">)</span><span class="token punctuation">;</span> Thread t2<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>myPriority<span class="token punctuation">)</span><span class="token punctuation">;</span> Thread t3<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>myPriority<span class="token punctuation">)</span><span class="token punctuation">;</span> Thread t4<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>myPriority<span class="token punctuation">)</span><span class="token punctuation">;</span> Thread t5<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>myPriority<span class="token punctuation">)</span><span class="token punctuation">;</span> Thread t6<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>myPriority<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 先设置优先级,再启动</span> t1<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t2<span class="token punctuation">.</span><span class="token function">setPriority</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t2<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t3<span class="token punctuation">.</span><span class="token function">setPriority</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t3<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t4<span class="token punctuation">.</span><span class="token function">setPriority</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span>MAX_PRIORITY<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// MAX_PRIORITY=10</span> t4<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t5<span class="token punctuation">.</span><span class="token function">setPriority</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t5<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> t6<span class="token punctuation">.</span><span class="token function">setPriority</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span>MIN_PRIORITY<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// MIN_PRIORITY=1</span> t6<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">class</span> <span class="token class-name">MyPriority</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span><span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"-->"</span><span class="token operator">+</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getPriority</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><img src="/post/5729df21/image-20210604100503274.png"></p><blockquote><p>小贴士:</p><p> 优先级低只是意味着获得调度的概率低,并不是高优先级必然先被调用 (性能倒置问题)</p><p> <strong>优先级的设定建议在start()调度前</strong></p></blockquote><h2 id="10-7-守护线程(daemon)"><a href="#10-7-守护线程(daemon)" class="headerlink" title="10.7 守护线程(daemon)"></a>10.7 守护线程(daemon)</h2><ul><li>线程分为<strong>用户线程</strong>和<strong>守护线程</strong></li><li>虚拟机必须确保用户线程执行完毕</li><li>虚拟机<strong>不用等待</strong>守护线程执行完毕</li><li>如:后台记录操作日志、监控内存、垃圾回收 等等…</li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> state<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p>测试守护线程</p> * * @author 江七 * @date 2021/6/4 10:08. */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestDamemon</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> God god<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">God</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> You you<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">You</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Thread thread<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>god<span class="token punctuation">)</span><span class="token punctuation">;</span> thread<span class="token punctuation">.</span><span class="token function">setDaemon</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 默认是false 表示是用户线程,正常的线程都是用户线程。。。</span> thread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 神守护线程</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>you<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 你 用户线程启动</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// 神</span><span class="token keyword">class</span> <span class="token class-name">God</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span><span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 按理来说不会结束 但作为守护线程在用户线程结束后 随之结束(可能会伴随虚拟机关闭的一点点延迟)</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"神保佑着你"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// 你</span><span class="token keyword">class</span> <span class="token class-name">You</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span><span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span><span class="token number">36500</span> <span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"开心的活着"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"-============goodBye ! world!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>小贴士:</p><p> 守护线程在用户线程结束后会随之结束(可能会伴随着虚拟机关闭,出现延迟关闭的情况)</p></blockquote><h1 id="11、-线程同步"><a href="#11、-线程同步" class="headerlink" title="11、 线程同步"></a>11、 线程同步</h1><p>多个线程操作同一个资源。</p><ul><li><strong>并发</strong>:同一个对象被多个线程同时操作</li></ul><blockquote><p>处理多线程问题时,<strong>多个线程访问同一个对象</strong>,并且某些线程还想修改这个对象,这时候就需要线程同步。线程同步是一种等待机制,多个需要同时访问此对象的线程进入这个对象的<strong>等待池</strong>形成队列,等待前面线程使用完毕,再让下一个线程使用</p></blockquote><ul><li><p><strong>形成条件:</strong> <strong>队列+锁</strong></p></li><li><p>由于同一进程的多个线程共享同一块存储空间,在带来方便的同时,也带来了<strong>访问冲突问题</strong>﹐为了保证数据在方法中被访问时的正确性,在访问时加入锁机制 <strong>synchronized</strong> ,当一个线程获得对象的<strong>排它锁</strong>,独占资源﹐其他线程必须等待,使用后释放锁即可。但存在以下问题:</p><ul><li>一个线程持有锁会导致其他所有需要此锁的线程挂起﹔</li><li>在多线程竞争下, 加锁,释放锁会导致比较多的上下文切换和调度延时,引起性能问题;</li><li>如果一个优先级高的线程等待一个优先级低的线程释放锁 会导致优先级倒置,引起性能问题.</li></ul></li></ul><h2 id="11-1-线程不安全的两个例子"><a href="#11-1-线程不安全的两个例子" class="headerlink" title="11.1 线程不安全的两个例子"></a>11.1 线程不安全的两个例子</h2><h3 id="11-1-1-不安全的买票"><a href="#11-1-1-不安全的买票" class="headerlink" title="11.1.1 不安全的买票"></a>11.1.1 不安全的买票</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> syn<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p>不安全的买票</p> * 线程不安全:重复买票 * @author 江七 * @date 2021/6/4 10:32. */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UnsafeBuyTicket</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> BuyTicket station<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">BuyTicket</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>station<span class="token punctuation">,</span><span class="token string">"教师"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>station<span class="token punctuation">,</span><span class="token string">"学生"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>station<span class="token punctuation">,</span><span class="token string">"可恶的黄牛党"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">class</span> <span class="token class-name">BuyTicket</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 票</span> <span class="token keyword">private</span> <span class="token keyword">int</span> ticketNums <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span> <span class="token keyword">boolean</span> flag <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 外部停止方式</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 买票</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>flag<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token function">buy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">buy</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> InterruptedException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 判断是否有票</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>ticketNums <span class="token operator"><=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> flag <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 模拟延时</span> Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 买票</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"拿到"</span> <span class="token operator">+</span> ticketNums<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><img src="/post/5729df21/image-20210604104304566.png"></p><h3 id="11-1-2-不安全的取钱"><a href="#11-1-2-不安全的取钱" class="headerlink" title="11.1.2 不安全的取钱"></a>11.1.2 不安全的取钱</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> syn<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Date<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p>不安全的取钱</p> * * @author 江七 * @date 2021/6/4 10:44. */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UnsafeBank</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 账户</span> Account account <span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Account</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token string">"结婚基金"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Drawing you <span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Drawing</span><span class="token punctuation">(</span>account<span class="token punctuation">,</span><span class="token number">50</span><span class="token punctuation">,</span><span class="token string">"you"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Drawing girlFriend <span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Drawing</span><span class="token punctuation">(</span>account<span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token string">"girlFriend"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> you<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> girlFriend<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// 账户</span><span class="token keyword">class</span> <span class="token class-name">Account</span><span class="token punctuation">{</span> <span class="token keyword">int</span> money<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//余额</span> String name<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//卡名</span> <span class="token keyword">public</span> <span class="token function">Account</span><span class="token punctuation">(</span><span class="token keyword">int</span> money<span class="token punctuation">,</span> String name<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>money <span class="token operator">=</span> money<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// 银行:模拟取款</span><span class="token keyword">class</span> <span class="token class-name">Drawing</span> <span class="token keyword">extends</span> <span class="token class-name">Thread</span><span class="token punctuation">{</span> Account account<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 账户</span> <span class="token comment" spellcheck="true">// 取了多少钱</span> <span class="token keyword">int</span> drawingMoney<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 现在手里有多少钱</span> <span class="token keyword">int</span> nowMoney<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token function">Drawing</span><span class="token punctuation">(</span>Account account <span class="token punctuation">,</span><span class="token keyword">int</span> drawingMoney<span class="token punctuation">,</span>String name<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>account<span class="token operator">=</span>account<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>drawingMoney<span class="token operator">=</span>drawingMoney<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 取钱</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 判断有没有钱</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>account<span class="token punctuation">.</span>money<span class="token operator">-</span>drawingMoney<span class="token operator"><</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"钱不够,取不了"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// sleep 可以放大问题的发生性</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 卡内余额 = 余额 - 你取得钱</span> account<span class="token punctuation">.</span>money <span class="token operator">=</span> account<span class="token punctuation">.</span>money <span class="token operator">-</span> drawingMoney<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 你手里的钱</span> nowMoney <span class="token operator">=</span> nowMoney <span class="token operator">+</span> drawingMoney<span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>account<span class="token punctuation">.</span>name<span class="token operator">+</span><span class="token string">"余额为:"</span><span class="token operator">+</span>account<span class="token punctuation">.</span>money<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// Thread.currentThread().getName() =this.getName();</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"手里的钱"</span><span class="token operator">+</span>nowMoney<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><img src="/post/5729df21/image-20210604105821154.png"></p><blockquote><p>小贴士:</p><p> sleep 可以放大问题的发生性</p></blockquote><h2 id="11-2-同步方法"><a href="#11-2-同步方法" class="headerlink" title="11.2 同步方法"></a>11.2 同步方法</h2><ul><li><p>由于我们可以通过 <code>private</code> 关键字来保证数据对象只能被方法访问,所以我们只需要针对方法提出一套机制,这套机制就是 <code>synchronized</code> 关键字,它包括两种用法:<code>synchronized</code> 方法和 <code>synchronized</code> 块</p><p> <strong>同步方法:</strong> <code>public synchronized void method(int args){ }</code> </p></li><li><p>synchronized方法控制对“对象”的访问,每个对象对应一把锁,每个 <strong>synchronized</strong> 方法都必须获得调用该方法的对象的锁才能执行,否则线程会阻塞,方法一旦执行,就独占该锁,直到该方法返回才释放锁,后面被阻塞的线程才能获得这个锁,继续执行</p><p> <strong>缺陷:</strong> <strong>若将一个大的方法声明为 <code>synchronized</code> 将会影响效率</strong></p></li><li><p>同步块: <code>synchronized(Obj){}</code> <span name="同步块"></span></p><ul><li><strong>Obj</strong> 称之为 <strong>同步监视器</strong><ul><li>**Obj ** 可以是任何对象,但是推荐使用共享资源作为同步监视器</li><li>同步方法中无需指定同步监视器﹐因为同步方法的同步监视器就是 <strong>this</strong> ,就是这个对象本身,或者是class [反射中讲解]</li></ul></li><li>同步监视器的执行过程<ul><li>第一个线程访问,锁定同步监视器,执行其中代码.</li><li>第二个线程访问,发现同步监视器被锁定,无法访问</li><li>第一个线程访问完毕,解锁同步监视器.</li><li>第二个线程访问,发现同步监视器没有锁,然后锁定并访问</li></ul></li></ul></li></ul><h2 id="11-3-修改例子使其成为线程安全"><a href="#11-3-修改例子使其成为线程安全" class="headerlink" title="11.3 修改例子使其成为线程安全"></a>11.3 修改例子使其成为线程安全</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> syn<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p>修改不安全的买票,使其变成线程安全</p> * * @author 江七 * @date 2021/6/4 10:32. */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UnsafeBuyTicket</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> BuyTicket station<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">BuyTicket</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>station<span class="token punctuation">,</span><span class="token string">"教师"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>station<span class="token punctuation">,</span><span class="token string">"学生"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>station<span class="token punctuation">,</span><span class="token string">"可恶的黄牛党"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">class</span> <span class="token class-name">BuyTicket</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 票</span> <span class="token keyword">private</span> <span class="token keyword">int</span> ticketNums <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span> <span class="token keyword">boolean</span> flag <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 外部停止方式</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 买票</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>flag<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token function">buy</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// *********此处进行同步方法************</span> <span class="token comment" spellcheck="true">// synchronized 同步方法 ,锁的是 this</span> <span class="token keyword">private</span> <span class="token keyword">synchronized</span> <span class="token keyword">void</span> <span class="token function">buy</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> InterruptedException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 判断是否有票</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>ticketNums <span class="token operator"><=</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> flag <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 模拟延时</span> Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 买票</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"拿到"</span> <span class="token operator">+</span> ticketNums<span class="token operator">--</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><img src="/post/5729df21/image-20210604112346470.png"></p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> syn<span class="token punctuation">;</span><span class="token comment" spellcheck="true">/** * <p>修改不安全的取钱,使其变成线程安全</p> * * @author 江七 * @date 2021/6/4 10:44. */</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UnsafeBank</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 账户</span> Account account <span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Account</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token string">"结婚基金"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Drawing you <span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Drawing</span><span class="token punctuation">(</span>account<span class="token punctuation">,</span><span class="token number">50</span><span class="token punctuation">,</span><span class="token string">"you"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Drawing girlFriend <span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Drawing</span><span class="token punctuation">(</span>account<span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token string">"girlFriend"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> you<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> girlFriend<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// 账户</span><span class="token keyword">class</span> <span class="token class-name">Account</span><span class="token punctuation">{</span> <span class="token keyword">int</span> money<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//余额</span> String name<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//卡名</span> <span class="token keyword">public</span> <span class="token function">Account</span><span class="token punctuation">(</span><span class="token keyword">int</span> money<span class="token punctuation">,</span> String name<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>money <span class="token operator">=</span> money<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// 银行:模拟取款</span><span class="token keyword">class</span> <span class="token class-name">Drawing</span> <span class="token keyword">extends</span> <span class="token class-name">Thread</span><span class="token punctuation">{</span> Account account<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 账户</span> <span class="token comment" spellcheck="true">// 取了多少钱</span> <span class="token keyword">int</span> drawingMoney<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 现在手里有多少钱</span> <span class="token keyword">int</span> nowMoney<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token function">Drawing</span><span class="token punctuation">(</span>Account account <span class="token punctuation">,</span><span class="token keyword">int</span> drawingMoney<span class="token punctuation">,</span>String name<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>account<span class="token operator">=</span>account<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>drawingMoney<span class="token operator">=</span>drawingMoney<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 取钱</span> <span class="token comment" spellcheck="true">// synchronized 默认锁定的是 this</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 锁的对象就是变化的量,需要增删改的对象</span> <span class="token keyword">synchronized</span> <span class="token punctuation">(</span>account<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 判断有没有钱</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>account<span class="token punctuation">.</span>money<span class="token operator">-</span>drawingMoney<span class="token operator"><</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"钱不够,取不了"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// sleep 可以放大问题的发生性</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 卡内余额 = 余额 - 你取得钱</span> account<span class="token punctuation">.</span>money <span class="token operator">=</span> account<span class="token punctuation">.</span>money <span class="token operator">-</span> drawingMoney<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 你手里的钱</span> nowMoney <span class="token operator">=</span> nowMoney <span class="token operator">+</span> drawingMoney<span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>account<span class="token punctuation">.</span>name<span class="token operator">+</span><span class="token string">"余额为:"</span><span class="token operator">+</span>account<span class="token punctuation">.</span>money<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// Thread.currentThread().getName() =this.getName();</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"手里的钱"</span><span class="token operator">+</span>nowMoney<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><img src="/post/5729df21/image-20210604112439089.png"></p><blockquote><p>小贴士:</p><ol><li><strong>synchronized</strong> 同步方法 ,锁的是 this</li><li><strong>synchronized</strong> <a href="#%E5%90%8C%E6%AD%A5%E5%9D%97">同步块</a> ,锁的对象就是变化的量,需要增删改的对象</li></ol></blockquote><h1 id="12、锁"><a href="#12、锁" class="headerlink" title="12、锁"></a>12、锁</h1><h2 id="12-1-死锁"><a href="#12-1-死锁" class="headerlink" title="12.1 死锁"></a>12.1 死锁</h2><p>产生死锁的四个必要条件</p><ol><li>互斥条件:一个资源每次只能被一个进程使用</li><li>请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放</li><li>不剥夺条件:进程已获得的资源,在未用完之前,不能强行剥夺</li><li>循环等待条件:若干进程之间形成一种头尾相接的循环等待资源关系</li></ol><blockquote><p>小贴士:</p><p> 上面列出的死锁的四个必要条件,我们只要想办法破环其中的任意一个或多个条件就可以避免死锁的发生。</p></blockquote><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// 死锁:多个线程互相抱着对方需要的资源,然后形成僵持</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DealLock</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> Makeup g1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Makeup</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">"灰姑娘"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Makeup g2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Makeup</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">"白雪公主"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> g1<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> g2<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// 口红</span><span class="token keyword">class</span> <span class="token class-name">Lipstick</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// 镜子</span><span class="token keyword">class</span> <span class="token class-name">Mirror</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token keyword">class</span> <span class="token class-name">Makeup</span> <span class="token keyword">extends</span> <span class="token class-name">Thread</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 需要的资源只有一份,用 static 来保证只有一份</span> <span class="token keyword">static</span> Lipstick lipstick <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Lipstick</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">static</span> Mirror mirror <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Mirror</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> chioce<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 选择</span> String girlName<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 使用化妆品的人</span> <span class="token function">Makeup</span><span class="token punctuation">(</span><span class="token keyword">int</span> chioce<span class="token punctuation">,</span> String girlName<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>chioce <span class="token operator">=</span> chioce<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>girlName <span class="token operator">=</span> girlName<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 化妆</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> <span class="token function">makeup</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 化妆,互相持有对方的锁,就是需要拿到对方的资源</span> <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">makeup</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> InterruptedException <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>chioce <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">synchronized</span> <span class="token punctuation">(</span>lipstick<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 获得口红的锁</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>girlName <span class="token operator">+</span> <span class="token string">"获得口红的锁"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">synchronized</span> <span class="token punctuation">(</span>mirror<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 一秒后想获得镜子</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>girlName <span class="token operator">+</span> <span class="token string">"获得镜子的锁"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token keyword">synchronized</span> <span class="token punctuation">(</span>mirror<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 获得镜子的锁</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>girlName <span class="token operator">+</span> <span class="token string">"获得镜子的锁"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">synchronized</span> <span class="token punctuation">(</span>lipstick<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 两秒后想获得口红</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>girlName <span class="token operator">+</span> <span class="token string">"获得口红的锁"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="12-2-Lock-锁"><a href="#12-2-Lock-锁" class="headerlink" title="12.2 Lock(锁)"></a>12.2 Lock(锁)</h2><ul><li><p>从 JDK 5.0 开始,Java提供了更强大的线程同步机制——通过显式定义同步锁对象来实现同步。同步锁使用 Lock 对象充当</p></li><li><p><strong>java.util.concurrent.locks.Lock</strong> 接口是控制多个线程对共享资源进行访问的工具。锁提供了对共享资源的独占访问,每次只能有一个线程对 Lock 对象加锁,线程开始访问共享资源之前应先获得Lock对象</p></li><li><p><strong>ReentrantLock</strong> <strong>(可重入锁)</strong> 类实现了 Lock,它拥有与 <code>synchronized</code> 相同的并发性和内存语义,在实现线程安全的控制中,比较常用的是 <strong>ReentrantLock</strong>,可以显式加锁、释放锁。</p></li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">A</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 定义 lock 锁</span> <span class="token keyword">private</span> <span class="token keyword">final</span> ReentrantLock lock <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ReentrantLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">m</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> lock<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 加锁</span> <span class="token keyword">try</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 保证线程安全的代码</span> <span class="token punctuation">}</span> <span class="token keyword">finally</span><span class="token punctuation">{</span> lock<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 解锁</span> <span class="token comment" spellcheck="true">// 如果同步代码有异常,要将unlock()写入finally语句块 </span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="12-3-synchronized-与-Lock-的对比"><a href="#12-3-synchronized-与-Lock-的对比" class="headerlink" title="12.3 synchronized 与 Lock 的对比"></a>12.3 synchronized 与 Lock 的对比</h2><ul><li><code>Lock</code> 是显示锁,需要手动开启和关闭,<code>synchronized</code> 是隐式锁,出了作用域自动释放</li><li><code>Lock</code> 只有代码块锁,<code>synchronized</code> 有代码块锁和方法锁</li><li>使用 Lock 锁,JVM 将花费较少的时间来调度线程,性能更好。并且具有更好的扩展性(提供更多的子类)</li><li>优先使用顺序<ul><li>Lock > 同步代码快(已经进入方法体,分配了相应资源)> 同步方法(在方法体之外)</li></ul></li></ul><h1 id="13、线程协作——生产者消费者问题"><a href="#13、线程协作——生产者消费者问题" class="headerlink" title="13、线程协作——生产者消费者问题"></a>13、线程协作——生产者消费者问题</h1><ul><li><p>线程同步问题,生产者和消费者共享同一个资源,并且生产者和消费者之间相互依赖,互为条件</p><ul><li><p>对于生产者,没有生产产品之前,要通知消费者等待,而生产了产品之后,有需要马上通知消费者消费</p></li><li><p>对于消费者,在消费之后,要通知生产者已经结束消费,需要生产新的产品以供消费</p></li><li><p>在生产者消费者问题中,仅有 synchronized 是不够的</p><ul><li><p>synchronized 可阻止并发更新同一个共享资源,实现了同步</p></li><li><p>synchronized 不能用来实现不同线程之间的消息传递(通信)</p></li></ul></li></ul></li><li><p>Java提供了几个方法解决线程之间的通信问题</p></li></ul><table><thead><tr><th align="left"><code>方法名</code></th><th align="left"><code>作用</code></th></tr></thead><tbody><tr><td align="left"><code>wait()</code></td><td align="left">表示线程一直等待,直到其他线程通知,与 sleep()不同,会释放锁</td></tr><tr><td align="left"><code>wait(long timeout)</code></td><td align="left">指定等待的毫秒数</td></tr><tr><td align="left"><code>notify()</code></td><td align="left">唤醒一个处于等待状态的线程</td></tr><tr><td align="left"><code>notifyAll()</code></td><td align="left">唤醒同一个对象上所有调用 wait()方法的线程,优先级别高的线程优先调度</td></tr></tbody></table><blockquote><p>注意:**均是 Object 类的方法,都只能在同步方法或者同步代码快中使用,否则会抛出异常 <code>IllegalMonitorStateException</code> **</p></blockquote><h2 id="13-1-解决方式"><a href="#13-1-解决方式" class="headerlink" title="13.1 解决方式"></a>13.1 解决方式</h2><h3 id="13-1-1-并发协作模型“生产者-消费者模式”—-gt-管程法"><a href="#13-1-1-并发协作模型“生产者-消费者模式”—-gt-管程法" class="headerlink" title="13.1.1 并发协作模型“生产者/消费者模式”—>管程法"></a>13.1.1 并发协作模型“生产者/消费者模式”—>管程法</h3><ul><li><p>生产者:负责<strong>生产</strong>数据的模块(可能是方法、对象、线程、进程)</p></li><li><p>消费者:负责<strong>处理</strong>数据的模块(可能是方法、对象、线程、进程)</p></li><li><p>缓冲区:消费者<strong>不能</strong>直接使用生产者的数据,他们之间有个“缓冲区”</p><p> <strong>生产者将生产好的数据放入缓冲区,消费者从缓冲区拿出数据。</strong></p></li></ul><p><img src="/post/5729df21/image-20210604152907037.png"></p><h3 id="13-1-2-并发协作模型“生产者-消费者模式”—-gt-信号灯法"><a href="#13-1-2-并发协作模型“生产者-消费者模式”—-gt-信号灯法" class="headerlink" title="13.1.2 并发协作模型“生产者/消费者模式”—>信号灯法"></a>13.1.2 并发协作模型“生产者/消费者模式”—>信号灯法</h3>]]></content>
<categories>
<category> Java </category>
<category> 多线程 </category>
</categories>
<tags>
<tag> Java </tag>
<tag> 多线程 </tag>
</tags>
</entry>
<entry>
<title>企业信息化战略与实施</title>
<link href="post/198494b9/"/>
<url>post/198494b9/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><h1 id="企业信息化战略与实施"><a href="#企业信息化战略与实施" class="headerlink" title="企业信息化战略与实施"></a>企业信息化战略与实施</h1><h2 id="信息化基本概念(-)"><a href="#信息化基本概念(-)" class="headerlink" title="信息化基本概念(*)"></a>信息化基本概念(*)</h2><h3 id="信息与信息化概念"><a href="#信息与信息化概念" class="headerlink" title="信息与信息化概念"></a>信息与信息化概念</h3><blockquote><p>维纳:信息就是信息,既不是物质也不是能量,但信息可转换为物质或能量。</p><p><strong>香农</strong>:信息就是不确定性的减少。</p><p>哲学界:信息是事物普遍联系的方式。</p><p>其他:信息是事先不知道的报导。</p></blockquote><blockquote><p>信息化就是计算机、通信和网络技术的现代化。</p><p>信息化就是从物质生产占主导地位的社会信息产业占主导地位社会转变的发展过程。</p><p>信息化就是从工业社会向信息社会演进的过程</p></blockquote><h3 id="信息系统的概念"><a href="#信息系统的概念" class="headerlink" title="信息系统的概念"></a>信息系统的概念</h3><ul><li><p>应用层次分类</p><ul><li>战略级(企业最高管理层)</li><li>战术级(企业中层经理及其管理部门)</li><li>操作级(服务型企业的业务部门)</li><li>事务级(企业的管理业务人员)</li></ul></li><li><p>数据环境分类</p><ul><li>数据文件</li><li>应用数据库</li><li>主题数据库</li><li>信息检索系 ————>数据仓库</li></ul></li></ul><h2 id="信息化战略体系(-)"><a href="#信息化战略体系(-)" class="headerlink" title="信息化战略体系(****)"></a>信息化战略体系(****)</h2><blockquote><p>==企业战略==是对企业各种战略的统称,其中既包含竞争战略,也包括营销战略、发展战略、品牌战略、融资战略、技术开发战略、人才开发战略、资源开发战略等等。</p><p>==解读:确定企业未来发展的大方向。==</p><p>==企业信息化战略==是指企业为适应激烈的环境变化,通过集成聚合现代信息技术,开发应用信息资源,并能够聚合组织制度以期获取未来竞争优势的长远运作机制和体系。</p><p>==解读:为企业战略开发支撑系统==</p><p>==系统规划==是信息系统生命周期的第一个阶段,其任务是对企业的环境啊、目标及现有系统的状况进行初步调查,根据企业目标个发展战略,确定信息系统的发展战略,对建设新系统的需求做出分析和预测,同时考虑建设新系统所受的各种约束,研究建设新系统的必要性和可能性。</p><p>==解读:单个项目的立项分析==</p></blockquote><h3 id="信息系统战略规划-方法"><a href="#信息系统战略规划-方法" class="headerlink" title="信息系统战略规划 - 方法"></a>信息系统战略规划 - 方法</h3><ul><li><p>第一阶段</p><p><strong>以数据处理为核心</strong> 围绕职能部门需求</p><ul><li>方法<ul><li>企业系统规划法(BSP) CU矩阵(U:使用;C:创建)</li><li>关键成功因素法(CSF)</li><li>战略集合转化法(SST)</li></ul></li></ul></li><li><p>第二阶段</p><p><strong>以企业内部MIS为核心</strong> 围绕企业整体需求</p><ul><li>方法<ul><li><strong>战略数据规划法(SDP</strong>) 企业模型建立、主题数据库</li><li>信息工程法(IE)信息战略规划、业务领域分析、系统设计、系统构建</li><li>战略栅格法(SG)</li></ul></li></ul></li><li><p>第三阶段</p><p>综合考虑企业内外环境 <strong>以集成为核心</strong>,围绕企业战略需求</p><ul><li>方法<ul><li><strong>价值链分析法 (VCA)</strong></li><li>战略一致性模型(SAM)</li></ul></li></ul></li></ul><h3 id="信息系统开发方法"><a href="#信息系统开发方法" class="headerlink" title="信息系统开发方法"></a>信息系统开发方法</h3><ul><li>结构化法<ul><li>用户至上</li><li>严格区分工作阶段,每阶段有任务与成果(瀑布模型)</li><li>强调系统开发过程的整体性和全局性</li><li>系统开发过程工程化,文档资料标准化</li><li>自顶向下,逐步分解(求精)</li></ul></li><li>原型法<ul><li>适用于需求不明确的开发</li><li>按功能分:水平原型(界面)、垂直原型(复杂算法)</li><li>按最终结果分:抛弃式原型、演化式原型</li></ul></li><li>面向对象方法 ——> 自底向上 接近日常生活<ul><li>更好的复用性</li><li>关键在于建立一个全面、合理、统一的模型</li><li>分析、设计、实现三个阶段,界限不明确</li></ul></li><li>面向服务的方法 ————> 基于面向对象<ul><li>SO方法有三个主要的抽象级别:操作、服务、业务流程</li><li>SOAD分为三个层次:基础设计层(底层服务构件)、应用结构层(服务之间的接口和服务级协定)和业务组织层(业务流程建模和服务流程编排)</li><li>服务建模:分为服务发现(大量在需求阶段)、服务规约和服务实现三个阶段</li></ul></li></ul><h2 id="系统建模(-)"><a href="#系统建模(-)" class="headerlink" title="系统建模(*)"></a>系统建模(*)</h2><p><img src="/post/198494b9/%E8%BD%AF%E4%BB%B6%E5%BB%BA%E6%A8%A1.png"></p><h2 id="政府信息化与电子商务(-)"><a href="#政府信息化与电子商务(-)" class="headerlink" title="政府信息化与电子商务(*)"></a>政府信息化与电子商务(*)</h2><h2 id="企业信息化与电子商务(-)"><a href="#企业信息化与电子商务(-)" class="headerlink" title="企业信息化与电子商务(**)"></a>企业信息化与电子商务(**)</h2><h2 id="企业应用集成(-)"><a href="#企业应用集成(-)" class="headerlink" title="企业应用集成(***)"></a>企业应用集成(***)</h2><h2 id="企业门户(-)"><a href="#企业门户(-)" class="headerlink" title="企业门户(**)"></a>企业门户(**)</h2>]]></content>
<categories>
<category> 系统分析师 </category>
</categories>
<tags>
<tag> 信息 </tag>
</tags>
</entry>
<entry>
<title>CRM</title>
<link href="post/d7de870/"/>
<url>post/d7de870/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><h1 id="用户模块"><a href="#用户模块" class="headerlink" title="用户模块"></a>用户模块</h1><h2 id="一、用户登录"><a href="#一、用户登录" class="headerlink" title="一、用户登录"></a>一、用户登录</h2><ul><li><p>后端实现</p><ul><li><p>整体思路:</p><ol><li><p>参数判断</p><blockquote><p>用户姓名 非空判断<br>用户密码 非空判断</p></blockquote></li><li><p>通过用户名查询用户记录,返回用户对象</p></li><li><p>判断用户对象是否为空</p></li><li><p>如果用户对象不为空,则将前台传递的用户密码为数据库中的密码作比较</p></li><li><p>判断密码是否正确</p></li><li><p>如果密码正确,则登录成功,返回结果</p></li></ol></li><li><p>解耦</p><ul><li><p>Controller层 (控制层:接受请求、响应结果)</p><pre><code> 1. 通过形参接收客户端传递的参数 2. 调用业务逻辑层的登录方法,得到登录的结果 3. 响应数据给客户端</code></pre></li><li><p>Service层(业务逻辑层:非空判断、条件判断等业务逻辑处理)</p><pre><code>1. 参数判断,判断用户姓名、用户密码非空等,如果参数为空,抛出异常(异常被控制层捕获并处理)2. 调用数据访问层,通过用户名查询用户记录,返回用户对象3. 判断用户对象象是否为空,如果对象为空,抛出异常(异常被控制层捕获并处理)4. 判断密码是否正确,比较客户端传递的用户密码与数据库中查询的用户对象中的用户密码,如果密码不相等,抛出异常(异常被控制层捕获并处理)5. 如果密码正确,登录成功</code></pre></li><li><p>Dao层 (数据访问层:数据库中增删改查操作)</p><blockquote><p>通过用户名查询用户记录,返回用户对象</p></blockquote></li></ul></li></ul></li></ul><h2 id="二、修改密码"><a href="#二、修改密码" class="headerlink" title="二、修改密码"></a>二、修改密码</h2><ul><li><p>后端实现</p><ul><li><p>Controller层</p><ol><li>通过形参接收前端传递的参数 (原始密码、新密码、确认密码)</li><li>通过request对象,获取设置在cookie中的用户ID</li><li>调用Service层修改密码的功能,得到ResultInfo对象</li><li>返回ResultInfo对象</li></ol></li><li><p>Service层</p><ol><li><p>接收四个参数 (用户ID、原始密码、新密码、确认密码</p></li><li><p>通过用户ID查询用户记录,返回用户对象</p></li><li><p>参数校验</p><blockquote><p>待更新用户记录是否存在 (用户对象是否为空)<br>判断原始密码是否为空<br>判断原始密码是否正确(查询的用户对象中的用户密码是否原始密码一致)<br>判断新密码是否为空<br>判断确认密码是否为空<br>判断确认密码是否与新密码一致</p></blockquote></li><li><p>设置用户的新密码</p><blockquote><p>需要将新密码通过指定算法进行加密(md5加密)</p></blockquote></li><li><p>执行更新操作,判断受影响的行数</p></li></ol></li><li><p>Dao层</p><blockquote><p>通过用户ID修改用户密码</p></blockquote><p>难点: 单独处理 try catch 导致重复代码太多,就想要解决这个问题,所以采用了全局异常统一处理的方式,使用了接口HandlerExceptionResolver类</p><p> 然后发现方法的返回值:只有视图和数据(而数据上基本上都有@ResponseBody注解)</p><p>故可以采用反射来判断是否有该注解</p></li></ul></li></ul>]]></content>
<categories>
<category> 项目 </category>
</categories>
<tags>
<tag> Java </tag>
<tag> SpringBoot </tag>
<tag> SpringMVC </tag>
<tag> MyBatis </tag>
</tags>
</entry>
<entry>
<title>面试题</title>
<link href="post/5205ee75/"/>
<url>post/5205ee75/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><h1 id="java基础"><a href="#java基础" class="headerlink" title="java基础"></a>java基础</h1><h2 id="面向对象"><a href="#面向对象" class="headerlink" title="面向对象"></a>面向对象</h2><blockquote><p>什么是面向对象?</p></blockquote><blockquote><blockquote><p>对比面向过程,是两种不同的处理问题的角度</p><p>面向过程更注重事情的每一个步骤及顺序,面向对象更注重事情有哪些参与者(对象)、及各自需要做什么 ,比如:洗衣机洗衣服</p></blockquote></blockquote><blockquote><blockquote><p>面向过程会将任务拆解成一系列的步骤(函数), 1 、打开洗衣机—–>2、放衣服—–>3、放洗衣粉—->4、清洗—–>5、烘干</p></blockquote></blockquote><blockquote><blockquote><p>面向对象会拆出人和洗衣机两个对象:</p></blockquote></blockquote><blockquote><blockquote><blockquote><p>人:打开洗衣机 放衣服 放洗衣粉</p><p>洗衣机:清洗 烘干</p></blockquote></blockquote></blockquote><p> 从以上例子能看出,面向过程比较直接高效,而面向对象更易于复用、扩展和维护</p><p> 面向对象</p><p> <strong>封装 :</strong>封装的意义,在于明确标识出允许外部使用的所有成员函数和数据项</p><p> 内部细节对外部调用透明,外部调用无需修改或者关心内部实现</p><p>1 、<code>javabean</code>的属性私有,提供getset对外访问,因为属性的赋值或者获取逻辑只能由<code>javabean</code>本身决<br>定。而不能由外部胡乱修改</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">private</span> String name<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span>String name<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">"tuling_"</span><span class="token operator">+</span>name<span class="token punctuation">;</span><span class="token punctuation">}</span>该name有自己的命名规则,明显不能由外部直接赋值<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>2 、<code>orm</code>框架</p><p>操作数据库,我们不需要关心链接是如何建立的、<code>sql</code>是如何执行的,只需要引入<code>mybatis</code>,调方法即可</p><p> <strong>继承 :</strong>继承基类的方法,并做出自己的改变和/或扩展</p><p> 子类共性的方法或者属性直接使用父类的,而不需要自己再定义,只需扩展自己个性化的</p><p><strong>多态 :</strong>基于对象所属类的不同,外部对同一个方法的调用,实际执行的逻辑不同。</p><p>继承,方法重写,父类引用指向子类对象</p><pre><code> 父类类型 变量名 = new 子类对象; 变量名.方法名();</code></pre><p> 无法调用子类特有的功能</p><h2 id="JDK-、JRE-和JVM"><a href="#JDK-、JRE-和JVM" class="headerlink" title="JDK 、JRE 和JVM"></a>JDK 、JRE 和JVM</h2><p> JDK:</p><p> Java Develpment Kit java 开发工具</p><p>JRE:</p><p> Java Runtime Environment java运行时环境</p><p>JVM:</p><p> java Virtual Machine java 虚拟机</p><p><img src="/post/5205ee75/JJJ.png"></p><h2 id="和equals比较"><a href="#和equals比较" class="headerlink" title="==和equals比较"></a>==和equals比较</h2>]]></content>
<categories>
<category> Java </category>
<category> 面试题 </category>
</categories>
<tags>
<tag> Java </tag>
<tag> 面试题 </tag>
</tags>
</entry>
<entry>
<title>Java IO</title>
<link href="post/b4224212/"/>
<url>post/b4224212/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><h1 id="IO-框架"><a href="#IO-框架" class="headerlink" title="IO 框架"></a>IO 框架</h1><h2 id="1、流的概念"><a href="#1、流的概念" class="headerlink" title="1、流的概念"></a>1、流的概念</h2><p>内存与存储设备之间传输数据的通道</p><p><img src="/post/b4224212/%E6%B5%81%E7%9A%84%E6%A6%82%E5%BF%B5.png"></p><blockquote><p>小贴士:</p><p> 水借助管道传输; 数据借助流传输</p></blockquote><h2 id="2、流的分类"><a href="#2、流的分类" class="headerlink" title="2、流的分类"></a>2、流的分类</h2><h3 id="2-1-按方向【重点】"><a href="#2-1-按方向【重点】" class="headerlink" title="2.1 按方向【重点】"></a>2.1 按方向【重点】</h3><ul><li>输入流:将<**存储设备**>中的内容读到<**内存**>中</li><li>输出流:将<**内存**>中的内容写到<**存储设备**>中</li></ul><h3 id="2-2-按单位"><a href="#2-2-按单位" class="headerlink" title="2.2 按单位"></a>2.2 按单位</h3><ul><li>字节流:以<strong>字节</strong>为单位,可以读写<strong>所有数据</strong></li><li>字符流:以<strong>字符</strong>为单位,<strong>只</strong>能<strong>读写文本数据</strong></li></ul><h3 id="2-3-按功能"><a href="#2-3-按功能" class="headerlink" title="2.3 按功能"></a>2.3 按功能</h3><ul><li>节点流:具有实际传输数据的读写功能</li><li>过滤流:在节点流的基础之上增强功能</li></ul><h2 id="3、字节流"><a href="#3、字节流" class="headerlink" title="3、字节流"></a>3、字节流</h2><h3 id="3-1-字节流的父类(抽象类)"><a href="#3-1-字节流的父类(抽象类)" class="headerlink" title="3.1 字节流的父类(抽象类)"></a>3.1 字节流的父类(抽象类)</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// InputStream 字节输入流</span><span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//从输入流中读取一个字节返回int型变量,若到达文件末尾,则返回-1</span><span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> b<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//从输入流中读取b.length个字节到字节数组中,返回读入缓冲区的总</span><span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> b<span class="token punctuation">,</span> <span class="token keyword">int</span> off<span class="token punctuation">,</span> <span class="token keyword">int</span> len<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//从输入流中读取最多len个字节到字节数组中 // (从数组的off位置开始存储字节)</span><span class="token comment" spellcheck="true">// OutputStream 字节输出流</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">write</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">write</span><span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> b<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">write</span><span class="token punctuation">(</span><span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> b<span class="token punctuation">,</span> <span class="token keyword">int</span> off<span class="token punctuation">,</span> <span class="token keyword">int</span> len<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="3-2-文件字节流"><a href="#3-2-文件字节流" class="headerlink" title="3.2 文件字节流"></a>3.2 文件字节流</h3><h4 id="3-2-1-文件输入流"><a href="#3-2-1-文件输入流" class="headerlink" title="3.2.1 文件输入流"></a>3.2.1 文件输入流</h4><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1 创建FileInputStream,并指定文件路径</span> File file<span class="token punctuation">;</span> FileInputStream fis <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\字节流\\aaa.txt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 2 读取文件</span> <span class="token comment" spellcheck="true">// fis.read();</span> <span class="token comment" spellcheck="true">// 2.1 单个字节读取</span> <span class="token keyword">int</span> data <span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token punctuation">(</span>data<span class="token operator">=</span>fis<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">!=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">)</span> data<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 2.2 一次读取多个字节</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> buf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">byte</span><span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 大小为3的缓存区</span> <span class="token comment" spellcheck="true">// int count = fis.read(buf); // 一次读3个</span><span class="token comment" spellcheck="true">// System.out.println(new String(buf));</span><span class="token comment" spellcheck="true">// System.out.println(count);</span><span class="token comment" spellcheck="true">// int count2 = fis.read(buf); // 再读3个</span><span class="token comment" spellcheck="true">// System.out.println(new String(buf));</span><span class="token comment" spellcheck="true">// System.out.println(count2);</span> <span class="token comment" spellcheck="true">//上述优化后</span> <span class="token keyword">int</span> count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token punctuation">(</span>count <span class="token operator">=</span> fis<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// -1 表示读完</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>buf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> count<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 3 关闭</span> fis<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="3-2-2-文件输出流"><a href="#3-2-2-文件输出流" class="headerlink" title="3.2.2 文件输出流"></a>3.2.2 文件输出流</h4><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1 创建文件字节输出流</span> FileOutputStream fos <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\字节流\\bbb.txt"</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">// true表示不覆盖 接着写</span> <span class="token comment" spellcheck="true">// 2 写入文件</span> fos<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token number">97</span><span class="token punctuation">)</span><span class="token punctuation">;</span> fos<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String string <span class="token operator">=</span> <span class="token string">"hello world"</span><span class="token punctuation">;</span> fos<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>string<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 3 关闭</span> fos<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="3-2-3-图片复制案例"><a href="#3-2-3-图片复制案例" class="headerlink" title="3.2.3 图片复制案例"></a>3.2.3 图片复制案例</h4><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1 创建流</span> <span class="token comment" spellcheck="true">// 1.1 文件字节输入流</span> FileInputStream fis <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"001.jpg"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 1.2 文件字节输出流</span> FileOutputStream fos <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span><span class="token string">"002.jpg"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 2 边读边写</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> buf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">byte</span><span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>count <span class="token operator">=</span> fis<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> fos<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>buf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> count<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 3 关闭</span> fis<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> fos<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="3-3-字节缓冲流"><a href="#3-3-字节缓冲流" class="headerlink" title="3.3 字节缓冲流"></a>3.3 字节缓冲流</h3><p>缓冲流:<code>BufferedInputStream</code>/ <code>BufferedOutputStream</code></p><ul><li>提高IO效率,减少访问磁盘次数</li><li>数据存储在缓冲区中,flush是将缓冲区的内容写入文件中,也可以直接close</li></ul><h4 id="3-3-1-使用字节缓冲流-读取-文件"><a href="#3-3-1-使用字节缓冲流-读取-文件" class="headerlink" title="3.3.1 使用字节缓冲流 读取 文件"></a>3.3.1 使用字节缓冲流 读取 文件</h4><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// 使用字节缓冲流 读取 文件</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1 创建BufferedInputStream</span> FileInputStream fis <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\字节流\\aaa.txt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> BufferedInputStream bis <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedInputStream</span><span class="token punctuation">(</span>fis<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 2 读取</span> <span class="token comment" spellcheck="true">// int data = 0;</span> <span class="token comment" spellcheck="true">// while((data = bis.read()) != -1){</span> <span class="token comment" spellcheck="true">// System.out.print((char)data);</span> <span class="token comment" spellcheck="true">// }</span> <span class="token comment" spellcheck="true">// 用自己创建的缓冲流</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> buf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">byte</span><span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token punctuation">(</span>count <span class="token operator">=</span> bis<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>buf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> count<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 3 关闭</span> bis<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="3-3-2-使用字节缓冲流-写入-文件"><a href="#3-3-2-使用字节缓冲流-写入-文件" class="headerlink" title="3.3.2 使用字节缓冲流 写入 文件"></a>3.3.2 使用字节缓冲流 写入 文件</h4><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// 使用字节缓冲流 写入 文件</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1 创建BufferedInputStream</span> FileOutputStream fos <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\字节流\\buffer.txt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> BufferedOutputStream bos <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedOutputStream</span><span class="token punctuation">(</span>fos<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 2 写入文件</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">10</span><span class="token punctuation">;</span> i <span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> bos<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">"hello"</span><span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">// 写入8k缓冲区</span> bos<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 刷新到硬盘(虽然会造成效率不高但会防止数据丢失)</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 3 关闭</span> bos<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="4、对象流"><a href="#4、对象流" class="headerlink" title="4、对象流"></a>4、对象流</h2><p><code>ObjectOutputStream</code> / <code>ObjectInputStream</code></p><ul><li>增强了缓冲区功能</li><li>增强了读写8种基本数据类型和字符串的功能</li><li>增强了读写对象的功能<ul><li><code>readObject()</code> 从流中读取一个对象</li><li><code>writeObject(Object obj)</code> 向流中写入一个对象</li></ul></li></ul><p>使用流传输对象的过程称为序列化、反序列化</p><h2 id="5、序列化与反序列化"><a href="#5、序列化与反序列化" class="headerlink" title="5、序列化与反序列化"></a>5、序列化与反序列化</h2><p>将对象通过流写入到文件(序列化),或将对象通过流读取到内存(反序列化),必须实现<code>Serializable</code>接口</p><h3 id="5-1-序列化"><a href="#5-1-序列化" class="headerlink" title="5.1 序列化"></a>5.1 序列化</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// 学生类</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token keyword">implements</span> <span class="token class-name">Serializable</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">long</span> serialVersionUID<span class="token operator">=</span>100L<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 序列化号</span> <span class="token keyword">private</span> String name<span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token keyword">int</span> age<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token function">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token function">Student</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span> <span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> String <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span>String name<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> age<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string">"Student{"</span> <span class="token operator">+</span> <span class="token string">"name='"</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span> <span class="token string">", age="</span> <span class="token operator">+</span> age <span class="token operator">+</span> <span class="token string">'}'</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// 使用ObjectOutputStream实现对象的序列化</span><span class="token comment" spellcheck="true">// 要求:序列化类必须要实现Serializable接口</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1. 创建对象流</span> FileOutputStream fos <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\字节流\\st.bin"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ObjectOutputStream oos <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ObjectOutputStream</span><span class="token punctuation">(</span>fos<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 2. 序列化(写入操作)</span> Student zhangsan <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token string">"zs"</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span> oos<span class="token punctuation">.</span><span class="token function">writeObject</span><span class="token punctuation">(</span>zhangsan<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 3. 关闭</span> oos<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"序列化完毕"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="5-2-反序列化"><a href="#5-2-反序列化" class="headerlink" title="5.2 反序列化"></a>5.2 反序列化</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// 使用MyObjectInputStream实现反序列化(读取重构成对象)</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException<span class="token punctuation">,</span> ClassNotFoundException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1. 创建对象流</span> FileInputStream fis <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\字节流\\st.bin"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> ObjectInputStream ois <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ObjectInputStream</span><span class="token punctuation">(</span>fis<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 2. 读取文件(反序列化)</span> Student s <span class="token operator">=</span> <span class="token punctuation">(</span>Student<span class="token punctuation">)</span>ois<span class="token punctuation">.</span><span class="token function">readObject</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 3. 关闭</span> ois<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"执行完毕"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="5-3-注意事项"><a href="#5-3-注意事项" class="headerlink" title="5.3 注意事项"></a>5.3 注意事项</h3><ol><li>某个类要想序列化必须实现<code>Serializable</code>接口</li><li>序列化类中对象属性要求实现<code>Serializable</code>接口</li><li>序列化版本号ID,保证序列化的类和反序列化的类是同一个类</li><li>使用<code>transient</code>(瞬间的)修饰属性,这个属性就不能序列化</li><li>静态属性不能序列化</li><li>序列化多个对象,可以借助集合来实现</li></ol><h2 id="6、字符编码"><a href="#6、字符编码" class="headerlink" title="6、字符编码"></a>6、字符编码</h2><p>使用 UTF-8 即可 </p><blockquote><p>小贴士:</p><p> 当编码方式和解码方式不一致时,会出现乱码</p></blockquote><h2 id="7、字符流"><a href="#7、字符流" class="headerlink" title="7、字符流"></a>7、字符流</h2><h3 id="7-1-字符流的父类(抽象类)"><a href="#7-1-字符流的父类(抽象类)" class="headerlink" title="7.1 字符流的父类(抽象类)"></a>7.1 字符流的父类(抽象类)</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// reader 字符输入流</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> c<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> b<span class="token punctuation">,</span> <span class="token keyword">int</span> off<span class="token punctuation">,</span> <span class="token keyword">int</span> len<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// Writer 字符输出流</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">write</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">write</span><span class="token punctuation">(</span>String str<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">write</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> c<span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="7-2-文件字符流"><a href="#7-2-文件字符流" class="headerlink" title="7.2 文件字符流"></a>7.2 文件字符流</h3><h4 id="7-2-1-字符输入流"><a href="#7-2-1-字符输入流" class="headerlink" title="7.2.1 字符输入流"></a>7.2.1 字符输入流</h4><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// 使用FileReader读取文件 </span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1. 创建FileReader 文件字符输入流</span> FileReader fr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileReader</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\字符流\\hello.txt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 2. 读取</span> <span class="token comment" spellcheck="true">// 2.1 单个字符读取</span><span class="token comment" spellcheck="true">// int data = 0;</span><span class="token comment" spellcheck="true">// while((data = fr.read()) != -1){</span><span class="token comment" spellcheck="true">// System.out.println((char)data);// 读取一个字符</span><span class="token comment" spellcheck="true">// }</span> <span class="token comment" spellcheck="true">// 字符缓冲区读取</span> <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> buf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">char</span><span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> count <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token punctuation">(</span>count <span class="token operator">=</span> fr<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">!=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>buf<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> count<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 3. 关闭</span> fr<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="7-2-2-字符写入流"><a href="#7-2-2-字符写入流" class="headerlink" title="7.2.2 字符写入流"></a>7.2.2 字符写入流</h4><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// 使用FileWriter写入文件</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1. 创建FileWriter对象</span> FileWriter fw <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileWriter</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\字符流\\write.txt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 2. 写入</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">10</span><span class="token punctuation">;</span> i <span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> fw<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">"写入的内容\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 换行\r\n</span> fw<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 3. 关闭</span> fw<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"执行完毕"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="7-2-3-文本文件复制案例"><a href="#7-2-3-文本文件复制案例" class="headerlink" title="7.2.3 文本文件复制案例"></a>7.2.3 文本文件复制案例</h4><p><strong>不能复制图片或二进制文件,使用字节流可以复制任意文件</strong></p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1. 创建</span> FileReader fr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileReader</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\字符流\\write.txt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> FileWriter fw <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileWriter</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\字符流\\write2.txt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 2. 读写</span> <span class="token keyword">int</span> data <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token punctuation">(</span>data <span class="token operator">=</span> fr<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span> fw<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span> fw<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 3. 关闭</span> fw<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> fr<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="7-3-字符缓冲流"><a href="#7-3-字符缓冲流" class="headerlink" title="7.3 字符缓冲流"></a>7.3 字符缓冲流</h3><p><code>BufferedReader</code> / <code>BufferedWriter</code></p><p>高效读写、支持输入换行符、可一次写一行读一行</p><h4 id="7-3-1-使用字符缓冲流读取文件"><a href="#7-3-1-使用字符缓冲流读取文件" class="headerlink" title="7.3.1 使用字符缓冲流读取文件"></a>7.3.1 使用字符缓冲流读取文件</h4><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// 使用字符缓冲流读取文件</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 创建缓冲流</span> FileReader fr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileReader</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\字符流\\write.txt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> BufferedReader br <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedReader</span><span class="token punctuation">(</span>fr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 读取</span> <span class="token comment" spellcheck="true">// 1. 第一种方式</span><span class="token comment" spellcheck="true">// char[] buf = new char[1024];</span><span class="token comment" spellcheck="true">// int count = 0;</span><span class="token comment" spellcheck="true">// while((count = br.read(buf)) != -1){</span><span class="token comment" spellcheck="true">// System.out.println(new String(buf, 0, count));</span><span class="token comment" spellcheck="true">// }</span> <span class="token comment" spellcheck="true">// 2. 第二种方式 一行一行读取</span> String line <span class="token operator">=</span> null<span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token punctuation">(</span>line <span class="token operator">=</span> br<span class="token punctuation">.</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> null<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 关闭</span> br<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="7-3-2-使用字符缓冲流写入文件"><a href="#7-3-2-使用字符缓冲流写入文件" class="headerlink" title="7.3.2 使用字符缓冲流写入文件"></a>7.3.2 使用字符缓冲流写入文件</h4><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// 使用字符缓冲流写入文件</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1. 创建BufferedWriter对象</span> FileWriter fw <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileWriter</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\字符流\\buffer.txt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> BufferedWriter bw <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedWriter</span><span class="token punctuation">(</span>fw<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 2. 写入</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> bw<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">"写入的内容"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> bw<span class="token punctuation">.</span><span class="token function">newLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 写入一个换行符 windows \r\n linux \n</span> bw<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 3. 关闭</span> bw<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 此时会自动关闭fw</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="8、打印流(PrintWriter)"><a href="#8、打印流(PrintWriter)" class="headerlink" title="8、打印流(PrintWriter)"></a>8、打印流(PrintWriter)</h2><p>封装了<code>print() / println()</code> 方法 支持写入后换行</p><p>支持数据原样打印</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> FileNotFoundException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1 创建打印流</span> PrintWriter pw <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PrintWriter</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\打印流\\print.txt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 2 打印</span> pw<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">;</span> pw<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> pw<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token number">3.14</span><span class="token punctuation">)</span><span class="token punctuation">;</span> pw<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 3 关闭</span> pw<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="9、转换流"><a href="#9、转换流" class="headerlink" title="9、转换流"></a>9、转换流</h2><ul><li>桥转换流 <code>InputStreamReader</code> / <code>OutputStreamWriter</code><ul><li>可将字节流转换为字符流</li><li>可设置字符的编码方式</li></ul></li></ul><h3 id="9-1-InputStreamReader-读取文件"><a href="#9-1-InputStreamReader-读取文件" class="headerlink" title="9.1 InputStreamReader 读取文件"></a>9.1 InputStreamReader 读取文件</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// 使用InputStreamReader读取文件,指定使用的编码</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1 创建InputStreamReader对象</span> FileInputStream fis <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\字符流\\write.txt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> InputStreamReader isr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">InputStreamReader</span><span class="token punctuation">(</span>fis<span class="token punctuation">,</span> StandardCharsets<span class="token punctuation">.</span>UTF_8<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 2 读取文件</span> <span class="token keyword">int</span> data <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token punctuation">(</span>data <span class="token operator">=</span> isr<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">)</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 3 关闭</span> isr<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="9-2-OutputStreamWriter-写入文件"><a href="#9-2-OutputStreamWriter-写入文件" class="headerlink" title="9.2 OutputStreamWriter 写入文件"></a>9.2 OutputStreamWriter 写入文件</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// 使用OutputStreamWriter写入文件,使用指定的编码格式</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1 创建OutputStreamReader对象</span> FileOutputStream fos <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\转换流\\print.txt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> OutputStreamWriter osw <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">OutputStreamWriter</span><span class="token punctuation">(</span>fos<span class="token punctuation">,</span> <span class="token string">"utf-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 2 写入</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">10</span><span class="token punctuation">;</span> i <span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> osw<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token string">"写入内容\r\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> osw<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 3 关闭</span> osw<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="10、File-类"><a href="#10、File-类" class="headerlink" title="10、File 类"></a>10、File 类</h2><ul><li><p>概念:代表物理盘符中的一个文件或者文件夹</p></li><li><p>方法:</p><pre class="line-numbers language-java"><code class="language-java"> <span class="token function">createNewFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 创建一个新文件</span> <span class="token function">mkdir</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 创建一个新目录</span> <span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 删除文件或空目录</span> <span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 判断</span> <span class="token function">getAbsolutePath</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 获取文件的绝对路径 </span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 获取文件名</span> <span class="token function">getParent</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 获取文件/目录所在的目录</span> <span class="token function">isDirectory</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 是否是目录</span> <span class="token function">isFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">//是否是文件</span> <span class="token function">lenth</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 获得文件的长度</span> <span class="token function">listFiles</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 列出目录中的所有内容</span> <span class="token function">renameTo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 修改文件名为</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li></ul><h3 id="10-1-文件的操作"><a href="#10-1-文件的操作" class="headerlink" title="10.1 文件的操作"></a>10.1 文件的操作</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// 使用方法 </span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> Exception <span class="token punctuation">{</span> <span class="token function">separator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">fileOpen</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// (1). 分隔符</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">separator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"路径分隔符:"</span> <span class="token operator">+</span> File<span class="token punctuation">.</span>pathSeparator<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"名称分隔符:"</span> <span class="token operator">+</span> File<span class="token punctuation">.</span>separator<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">// (2). 文件操作</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">fileOpen</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> Exception <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1. 创建文件</span> File file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\文件类\\file.txt"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>file<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 是否存在</span> <span class="token keyword">boolean</span> b <span class="token operator">=</span> file<span class="token punctuation">.</span><span class="token function">createNewFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"创建结果:"</span><span class="token operator">+</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 2. 删除文件</span> <span class="token comment" spellcheck="true">// 2.1 直接删除</span> file<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 成功true</span> <span class="token comment" spellcheck="true">// 2.2 使用jvm退出时删除</span> file<span class="token punctuation">.</span><span class="token function">deleteOnExit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 3. 获取文件信息</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"获取绝对路径"</span> <span class="token operator">+</span> file<span class="token punctuation">.</span><span class="token function">getAbsolutePath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"获取路径"</span> <span class="token operator">+</span> file<span class="token punctuation">.</span><span class="token function">getPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"获取文件名称"</span> <span class="token operator">+</span> file<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"获取夫目录"</span> <span class="token operator">+</span> file<span class="token punctuation">.</span><span class="token function">getParent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"获取文件长度"</span> <span class="token operator">+</span> file<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"文件创建时间"</span> <span class="token operator">+</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>file<span class="token punctuation">.</span><span class="token function">lastModified</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 4. 判断</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"是否可写"</span> <span class="token operator">+</span> file<span class="token punctuation">.</span><span class="token function">canWrite</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"是否是文件"</span> <span class="token operator">+</span> file<span class="token punctuation">.</span><span class="token function">isFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"是否隐藏"</span> <span class="token operator">+</span> file<span class="token punctuation">.</span><span class="token function">isHidden</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// (3). 文件夹操作</span> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">directoryOpe</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 1. 创建文件夹</span> File dir <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string">"F:\\IDEA_Workspace\\Java IO框架\\文件类\\aaa\\bbb\\ccc"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>dir<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>dir<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//dir.mkdir(); // 只能创建单级目录</span> dir<span class="token punctuation">.</span><span class="token function">mkdirs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 创建多级目录</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">// 2. 删除文件夹</span> <span class="token comment" spellcheck="true">// 2.1 直接删除</span> dir<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 只能删除最底层空目录</span> <span class="token comment" spellcheck="true">// 2.2 使用jvm删除</span> dir<span class="token punctuation">.</span><span class="token function">deleteOnExit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 3. 获取文件夹信息</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"获取绝对路径"</span> <span class="token operator">+</span> dir<span class="token punctuation">.</span><span class="token function">getAbsolutePath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"获取路径"</span> <span class="token operator">+</span> dir<span class="token punctuation">.</span><span class="token function">getPath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"获取文件夹名称"</span> <span class="token operator">+</span> dir<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 最里层目录名称</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"获取父目录"</span> <span class="token operator">+</span> dir<span class="token punctuation">.</span><span class="token function">getParent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"文件夹创建时间"</span> <span class="token operator">+</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>dir<span class="token punctuation">.</span><span class="token function">lastModified</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLocaleString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 4. 判断</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"是否是文件夹"</span> <span class="token operator">+</span> dir<span class="token punctuation">.</span><span class="token function">isFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"是否隐藏"</span> <span class="token operator">+</span> dir<span class="token punctuation">.</span><span class="token function">isHidden</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 5. 遍历文件夹</span> File dir2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string">"D:\\图片"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> files <span class="token operator">=</span> dir2<span class="token punctuation">.</span><span class="token function">list</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span>String string <span class="token operator">:</span> files<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>string<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="10-2-FileFilter-接口"><a href="#10-2-FileFilter-接口" class="headerlink" title="10.2 FileFilter 接口"></a>10.2 FileFilter 接口</h3><ul><li><code>boolean accept(File pathname)</code>: 测试<code>pathname</code>是否应该包含在当前的<code>File</code>目录中,如果符合返回<code>true</code></li><li>当调用<code>File</code>类中的<code>listFiles()</code>方法时,支持传入<code>FileFilter</code> 接口接口实现类,对获取文件进行过滤,只有满足条件的文件才可出现在<code>listFiles()</code>的返回值中</li></ul><pre><code> // FileFilter接口的使用 File dir2 = new File("D:\\图片"); File[] files2 = dir2.listFiles(new FileFilter(){ @Override public boolean accept(File pathname){ if(pathname.getName().endsWith(".jpg")){ return true; } return false; } }); for(File file : files2){ sout(file.getName()); } }</code></pre><h3 id="10-3-递归遍历文件夹"><a href="#10-3-递归遍历文件夹" class="headerlink" title="10.3 递归遍历文件夹"></a>10.3 递归遍历文件夹</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">listDir</span><span class="token punctuation">(</span>File dir<span class="token punctuation">)</span> <span class="token punctuation">{</span> File<span class="token punctuation">[</span><span class="token punctuation">]</span> files <span class="token operator">=</span> dir<span class="token punctuation">.</span><span class="token function">listFiles</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>dir<span class="token punctuation">.</span><span class="token function">getAbsolutePath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>files <span class="token operator">!=</span> null <span class="token operator">&&</span> files<span class="token punctuation">.</span>length <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>File file <span class="token operator">:</span> files<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>file<span class="token punctuation">.</span><span class="token function">isDirectory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">listDir</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 递归</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>file<span class="token punctuation">.</span><span class="token function">getAbsolutePath</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="10-4-递归删除文件夹"><a href="#10-4-递归删除文件夹" class="headerlink" title="10.4 递归删除文件夹"></a>10.4 递归删除文件夹</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">deleteDir</span><span class="token punctuation">(</span>File dir<span class="token punctuation">)</span> <span class="token punctuation">{</span> File<span class="token punctuation">[</span><span class="token punctuation">]</span> files <span class="token operator">=</span> dir<span class="token punctuation">.</span><span class="token function">listFiles</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>files <span class="token operator">!=</span> null <span class="token operator">&&</span> files<span class="token punctuation">.</span>length <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>File file <span class="token operator">:</span> files<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>file<span class="token punctuation">.</span><span class="token function">isDirectory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">deleteDir</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 递归</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// 删除文件</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>file<span class="token punctuation">.</span><span class="token function">getAbsolutePath</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"删除"</span> <span class="token operator">+</span> file<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>dir<span class="token punctuation">.</span><span class="token function">getAbsolutePath</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">"删除"</span> <span class="token operator">+</span> dir<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> Java基础 </category>
</categories>
<tags>
<tag> Java基础 </tag>
</tags>
</entry>
<entry>
<title>Spring框架</title>
<link href="post/62d904b9/"/>
<url>post/62d904b9/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><h1 id="什么是Spring"><a href="#什么是Spring" class="headerlink" title="什么是Spring"></a>什么是Spring</h1><ul><li>Spring官方网址:<a href="http://spring.io/">http://spring.io/</a></li><li>我们经常说的Spring其实指的是Spring Framework(spring 框架)。</li></ul><h1 id="为什么要学习Spring"><a href="#为什么要学习Spring" class="headerlink" title="为什么要学习Spring"></a>为什么要学习Spring</h1><ul><li><p>特点</p><pre><code>1. 方便解耦,简化开发 2. AOP编程的支持 3. 声明事务的支持 4. 方便程序的测试 5. 方便集成各种优秀的框架 6. 降低了Java EE API 的使用难度 7. Java 源码是经典学习范例</code></pre></li><li><p>好处</p><p> 总结起来,Spring有如下优点:</p><ol><li>底侵入式设计、代码污染极低</li><li>独立于各种应用服务器,基于Spring框架的应用,可以真正实现Write Once,Run Anywhere 的承诺</li><li>Spring的DI机制降低了业务对象替换的复杂性,提高了组件之间的解耦</li><li>Spring的AOP支持允许将一些通用任务如安全、事务、日志等进行集中式管理,从而提供了更好的复用</li><li>Spring的ORM和DAO提供了与第三方持久层框架的良好整合,并简化了底层的数据库访问</li><li>Spring并不强制应用完全依赖于Spring,开发者可自由选用Spring框架的部分或全部</li></ol></li></ul><h1 id="什么是耦合和内聚"><a href="#什么是耦合和内聚" class="headerlink" title="什么是耦合和内聚"></a>什么是耦合和内聚</h1><ul><li><p>**耦合性(Coupling)**,也叫耦合度,是对模块间关联程度的度量。</p><pre><code>在软件工程中,耦合指的就是就是对象之间的依赖性。对象之间的耦合越高,维护成本越高。因此对象的设计应使类和构件之间的耦合最小。软件设计中通常用耦合度和内聚度作为衡量模块独立程度的标准。划分模块的一个准则就是高内聚低耦合。</code></pre></li><li><p> 内聚标志一个模块内各个元素彼此结合的紧密程度,它是信息隐蔽和局部化概念的自然扩展。</p></li></ul><pre><code>内聚是从功能角度来度量模块内的联系,一个好的内聚模块应当恰好做一件事。内聚和耦合是密切相关的,同其他模块存在高耦合的模块意味着低内聚,而高内聚的模块意味着该模块同其他模块之间是低耦合。在进行软件设计时,应力争做到高内聚,低耦合。</code></pre><h1 id="Spring核心概念介绍"><a href="#Spring核心概念介绍" class="headerlink" title="Spring核心概念介绍"></a>Spring核心概念介绍</h1><ul><li><strong>IoC(核心中的核心):Inverse of Control ,控制反转</strong>。对象的创造权力由程序反转给Spring框架。</li><li><strong>DI:Dependency Injection,依赖注入。</strong> 在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件中!!!</li><li><strong>AOP:Aspect Oriented Programming,面向切面编程。</strong>早不修改目标对象的源代码的情况下,增强IoC容器中Bean的功能</li><li><strong>Spring容器:指的就是IoC容器,底层也就是一个BeanFactory。</strong></li></ul><h1 id="核心基础篇"><a href="#核心基础篇" class="headerlink" title="核心基础篇"></a>核心基础篇</h1><h2 id="基于XML的使用"><a href="#基于XML的使用" class="headerlink" title="基于XML的使用"></a>基于XML的使用</h2><h3 id="IoC配置"><a href="#IoC配置" class="headerlink" title="IoC配置"></a>IoC配置</h3><p>在spring的XML文件中通过一个<strong>bean</strong>标签,完成IoC的配置。</p><h4 id="bean标签"><a href="#bean标签" class="headerlink" title="bean标签"></a>bean标签</h4><ul><li><p><strong>bean标签作用</strong></p><blockquote><p>用于配置被spring容器管理的bean的信息。</p><p>默认情况下它调用的是类中的【无参构造函数】。如果没有无参构造则不能创建成功。</p></blockquote></li><li><p><strong>bean标签属性</strong></p><ul><li>id:给对象在容器中提供一个唯一标识。用于获取对象。</li><li>class:指定类的全限定名。用于反射创建对象。默认情况下调用无参构造函数。</li><li><strong>init-method</strong>:指定类中的初始化方法名称。</li><li><strong>destroy-method</strong>:指定类中销毁方法名称。比如DataSource的配置中一般需要指定destroy-method=“close”。</li><li>scope:指定对象的作用范围。<ul><li>singleton :<strong>默认值</strong>,单例的(在整个容器中只有一个对象),生命周期如下:<ul><li>对象出生:当应用加载,创建容器时,对象就被创建了。</li><li>对象活着:只要容器在,对象一直活着。</li><li>对象死亡:当应用卸载,销毁容器时,对象就被销毁了。</li></ul></li><li>prototype :多例的.每次访问对象时,都会重新创建对象实例。生命周期如下:<ul><li>对象出生:当使用对象时,创建新的对象实例。</li><li>对象活着:只要对象在使用中,就一直活着。</li><li>对象死亡:当对象长时间不用时,被 java 的垃圾回收器回收了。</li></ul></li><li>request :将Spring 创建的 Bean 对象存入到 request 域中.</li><li>session :将Spring 创建的 Bean 对象存入到 session 域中.</li><li>global session :WEB 项目中,应用在 Portlet 环境.如果没有 Portlet 环境那么<br>globalSession 相当于 session.</li></ul></li></ul></li></ul><h4 id="bean实例化的三种方式"><a href="#bean实例化的三种方式" class="headerlink" title="bean实例化的三种方式"></a>bean实例化的三种方式</h4><ul><li> <strong>第一种:使用默认无参构造函数(重点)</strong></li></ul><p> 在默认情况下:它会根据默认无参构造函数来创建类对象。</p><p> 如果 bean 中没有默认无参构造函数,将会创建失败。</p><pre class="line-numbers language-xml"><code class="language-xml"> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>userService<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.jiangqi.spring.service.UserServiceImpl<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li><p>第二种:静态工厂(了解)</p><p>使用 StaticFactory 类中的静态方法 createUserService 创建对象,并存入spring 容器</p><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">/*** 模拟一个静态工厂,创建业务层实现类*/</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StaticFactory</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> UserService <span class="token function">createUserService</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">UserServiceImpl</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>userService<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.jiangqi.spring.factory.StaticFactory<span class="token punctuation">"</span></span> <span class="token attr-name">factory-method</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>CreateUserService<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><p>第三种:实例工厂(了解)</p><pre class="line-numbers language-java"><code class="language-java"> <span class="token comment" spellcheck="true">/*** 模拟一个实例工厂,创建业务层实现类* 此工厂创建对象,必须现有工厂实例对象,再调用方法*/</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">InstanceFactory</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> UserService <span class="token function">createUserService</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">UserServiceImpl</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>instancFactory<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.jiangqi.factory.InstanceFactory<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>userService<span class="token punctuation">"</span></span> <span class="token attr-name">factory-bean</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>instancFactory<span class="token punctuation">"</span></span> <span class="token attr-name">factory-</span><span class="token attr-name">method</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>createUserService<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-java"><code class="language-java"> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 此种方式是: <span class="token operator">*</span> 先把工厂的创建交给 spring 来管理。 <span class="token operator">*</span> 然后在使用工厂的 bean 来调用里面的方法 factory<span class="token operator">-</span>bean 属性:用于指定实例工厂 bean 的 id。 factory<span class="token operator">-</span>method 属性:用于指定实例工厂中创建对象的方法。<span class="token operator">--</span><span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li></ul><h3 id="DI配置"><a href="#DI配置" class="headerlink" title="DI配置"></a>DI配置</h3><h4 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h4><ul><li><p>什么是依赖</p><pre><code>依赖指的就是Bean实例中的属性依赖(属性)分为:简单类型( 8 种基本类型和String类型)的属性、POJO类型的属性、集合数组类型的属性。</code></pre></li><li><p>什么是依赖注入</p><pre><code> 依赖注入:Dependency Injection。它是 spring 框架核心 IoC 的具体实现。</code></pre></li><li><p>为什么要进行依赖注入</p><pre><code>我们的程序在编写时,通过控制反转,把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。那如果一个bean中包含了一些属性,那么spring帮我们实例化了bean对象之后,也需要将对应的属性信息进行赋值操作,这种属性赋值操作,就是所谓的依赖注入(获取值、注入属性)</code></pre></li></ul><h4 id="依赖注入的方式"><a href="#依赖注入的方式" class="headerlink" title="依赖注入的方式"></a>依赖注入的方式</h4><h5 id="构造函数注入"><a href="#构造函数注入" class="headerlink" title="构造函数注入"></a>构造函数注入</h5><p>顾名思义,就是使用类中的构造函数,给成员变量赋值。注意:赋值的操作不是我们自己做的,而是通过配置的方式,让 spring 框架来为我们注入。</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">UserServiceImpl</span> <span class="token keyword">implements</span> <span class="token class-name">UserService</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token keyword">int</span> id<span class="token punctuation">;</span> <span class="token keyword">private</span> String name<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token function">UserServiceImpl</span><span class="token punctuation">(</span><span class="token keyword">int</span> id<span class="token punctuation">,</span> String name<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">saveUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"保存用户:id为"</span><span class="token operator">+</span>id<span class="token operator">+</span><span class="token string">",name为"</span><span class="token operator">+</span>name<span class="token operator">+</span><span class="token string">" Service实现"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>userService<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.jaingqi.spring.service.UserServiceImpl<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>constructor-arg</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>id<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>1<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>constructor-arg</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>constructor-arg</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>name<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>zhangsan<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>constructor-arg</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>!--</span> <span class="token attr-name">使用构造函数的方式,给</span> <span class="token attr-name">service</span> <span class="token attr-name">中的属性传值要求:类中需要提供一个对应参数列表的构造函数。</span><span class="token attr-name">涉及的标签:constructor-arg</span> <span class="token attr-name">*</span> <span class="token attr-name"><span class="token namespace">index:</span>指定参数在构造函数参数列表的索引位置</span> <span class="token attr-name">*</span> <span class="token attr-name"><span class="token namespace">name:</span>指定参数在构造函数中的名称</span> <span class="token attr-name">*</span> <span class="token attr-name"><span class="token namespace">value:</span>它能赋的值是基本数据类型和</span> <span class="token attr-name">String</span> <span class="token attr-name">类型</span> <span class="token attr-name">*</span> <span class="token attr-name"><span class="token namespace">ref:</span>它能赋的值是其他</span> <span class="token attr-name">bean</span> <span class="token attr-name">类型,也就是说,必须得是在配置文件中配置过的</span> <span class="token attr-name">bean</span><span class="token attr-name">--!</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h5 id="set方法注入"><a href="#set方法注入" class="headerlink" title="set方法注入"></a>set方法注入</h5><ul><li><p>手动装配方式(XML方式)</p><ul><li>需要配置bean标签的子标签property</li><li>需要配置的bean中指定setter方法。</li></ul></li><li><p> 自动装配方式(注解方式,后面讲解)</p></li></ul><pre><code>@Autowired: - 作用一:查找实例,从spring容器中根据Bean的类型(byType)获取实例。 - 作用二:赋值,将找到的实例,装配给另一个实例的属性值。 - 注意事项:一个java类型在同一个spring容器中,只能有一个实例@Resource: - 作用一:查找实例,从spring容器中根据Bean的名称(byName)获取实例。 - 作用二:赋值,将找到的实例,装配给另一个实例的属性值。@Inject:</code></pre><h5 id="使用p名称空间注入数据"><a href="#使用p名称空间注入数据" class="headerlink" title="使用p名称空间注入数据"></a>使用p名称空间注入数据</h5><p><strong>本质上还是调用set方法,自行了解</strong></p><ol><li><p>步骤一:需要先引入 p 名称空间</p><pre class="line-numbers language-xml"><code class="language-xml">在schema的名称空间中加入该行:xmlns:p="http://www.springframework.org/schema/p"<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><p>步骤二:使用p名称空间的语法</p><pre class="line-numbers language-xml"><code class="language-xml">p:属性名 = ""p:属性名-ref = ""<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></li><li><p>步骤三:测试</p><pre class="line-numbers language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>person<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.jiangqi.spring.demo.Person<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">p:</span>pname</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>老王<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">p:</span>car2-ref</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>car2<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>car2<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.kkb.spring.demo.Car2<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></li></ol><h4 id="依赖注入不同类型的属性"><a href="#依赖注入不同类型的属性" class="headerlink" title="依赖注入不同类型的属性"></a>依赖注入不同类型的属性</h4><h5 id="简单类型(value)"><a href="#简单类型(value)" class="headerlink" title="简单类型(value)"></a>简单类型(value)</h5><pre class="line-numbers language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>userService<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.jiangqi.spring.service.UserServiceImpl<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>id<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>1<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>name<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>zhangsan<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h5 id="引用类型(ref)"><a href="#引用类型(ref)" class="headerlink" title="引用类型(ref)"></a>引用类型(ref)</h5><p>ref就是reference的缩写,是引用的意思。</p><pre class="line-numbers language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>userService<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.jiangqi.spring.service.UserServiceImpl<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>userDao<span class="token punctuation">"</span></span> <span class="token attr-name">ref</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>userDao<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>constructor-arg</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>userDao<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.jiangqi.spring.dao.UserDaoImpl<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h5 id="集合类型(数组)"><a href="#集合类型(数组)" class="headerlink" title="集合类型(数组)"></a>集合类型(数组)</h5><ol><li><p>如果是数组或者List集合,注入配置文件的方式是一样的</p><pre class="line-numbers language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>collectionBean<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.kkb.demo5.CollectionBean<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>arrs<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>list</span><span class="token punctuation">></span></span> <span class="token comment" spellcheck="true"><!-- 如果集合内是简单类型,使用value子标签,如果是POJO类型,则使用bean标签 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>美美<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>小风<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>list</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li><li><p>如果是Set集合,注入的配置文件方式如下:</p><pre><code><property name="sets"> <set> <!-- 如果集合内是简单类型,使用value子标签,如果是POJO类型,则使用bean标签 --> <value>哈哈</value> <value>呵呵</value> </set></property></code></pre></li><li><p>如果是Map集合,注入的配置方式如下:</p><pre class="line-numbers language-xml"><code class="language-xml"> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>map<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>map</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>entry</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>老王2<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>38<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>entry</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>凤姐<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>38<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>entry</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>如花<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>29<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>map</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li><li><p>如果是Properties集合的方式,注入的配置如下:</p><pre class="line-numbers language-XML"><code class="language-XML"> <property name="pro"> <props> <prop key="uname">root</prop> <prop key="pass">123</prop> </props> </property><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li></ol><h2 id="基于注解和XML混合方式的使用"><a href="#基于注解和XML混合方式的使用" class="headerlink" title="基于注解和XML混合方式的使用"></a>基于注解和XML混合方式的使用</h2><h4 id="IoC注解使用方法"><a href="#IoC注解使用方法" class="headerlink" title="IoC注解使用方法"></a>IoC注解使用方法</h4><ul><li>第一步:spring配置文件中,配置context : component-scan标签</li></ul>]]></content>
<categories>
<category> Spring </category>
</categories>
<tags>
<tag> Spring </tag>
</tags>
</entry>
<entry>
<title>JVM快速入门篇</title>
<link href="post/701edcc3/"/>
<url>post/701edcc3/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script>]]></content>
<categories>
<category> JVM </category>
</categories>
<tags>
<tag> Java </tag>
<tag> JVM </tag>
</tags>
</entry>
<entry>
<title>day-02-数据类型转换、运算符、方法入门</title>
<link href="post/16c2569a/"/>
<url>post/16c2569a/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><h1 id="第02天【数据类型转换,运算符,方法入门】"><a href="#第02天【数据类型转换,运算符,方法入门】" class="headerlink" title="第02天【数据类型转换,运算符,方法入门】"></a>第02天【数据类型转换,运算符,方法入门】</h1><h1 id="第一章数据类型转换"><a href="#第一章数据类型转换" class="headerlink" title="第一章数据类型转换"></a>第一章数据类型转换</h1><p>Java程序中要求参与的计算的数据,必须要保证数据类型的一致性,如果数据类型不一致将发生类型的转换。</p><h2 id="1-1自动转换"><a href="#1-1自动转换" class="headerlink" title="1. 1自动转换"></a>1. 1自动转换</h2><p>一个<code>int</code>类型变量和一个<code>byte</code>类型变量进行加法运算,结果会是什么数据类型?</p><pre><code>int i = 1 ;byte b = 2 ;</code></pre><p>运算结果,变量的类型将是<code>int</code>类型,这就是出现了数据类型的自动类型转换现象。</p><ul><li>自动转换:将<strong>取值范围小的类型</strong>自动提升为<strong>取值范围大的类型</strong> 。</li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span> <span class="token punctuation">;</span> <span class="token keyword">byte</span> b <span class="token operator">=</span> <span class="token number">2</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// byte x = b + i; // 报错</span> <span class="token comment" spellcheck="true">//int类型和byte类型运算,结果是int类型</span> <span class="token keyword">int</span> j <span class="token operator">=</span> b <span class="token operator">+</span> i<span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>j<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="转换原理图解"><a href="#转换原理图解" class="headerlink" title="转换原理图解"></a>转换原理图解</h3><p><code>byte</code>类型内存占有1个字节,在和<code>int</code>类型运算时会提升为<code>int</code>类型,自动补充3个字节,因此计算后的结果还是<code>int</code>类<br>型。</p><p><img src="/post/16c2569a/%E8%BD%AC%E6%8D%A2%E5%8E%9F%E7%90%86.png"></p><p>同样道理,当一个<code>int</code>类型变量和一个<code>double</code>变量运算时,<code>int</code>类型将会自动提升为<code>double</code>类型进行运算。</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span> <span class="token punctuation">;</span> <span class="token keyword">double</span> d <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">.</span> <span class="token number">5</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//int类型和double类型运算,结果是double类型</span> <span class="token comment" spellcheck="true">//int类型会提升为double类型</span> <span class="token keyword">double</span> e <span class="token operator">=</span> d<span class="token operator">+</span>i<span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="转换规则"><a href="#转换规则" class="headerlink" title="转换规则"></a>转换规则</h3><p>范围小的类型向范围大的类型提升,byte,short,char运算时直接提升为int。</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">byte</span>、<span class="token keyword">short</span>、<span class="token keyword">char</span>‐‐<span class="token operator">></span><span class="token keyword">int</span>‐‐<span class="token operator">></span><span class="token keyword">long</span>‐‐<span class="token operator">></span><span class="token keyword">float</span>‐‐<span class="token operator">></span><span class="token keyword">double</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h2 id="1-2强制转换"><a href="#1-2强制转换" class="headerlink" title="1. 2强制转换"></a>1. 2强制转换</h2><p>将<code>1. 5</code>赋值到<code>int</code>类型变量会发生什么?产生编译失败,肯定无法赋值。</p><pre><code>int i = 1. 5 ; // 错误</code></pre><p><code>double</code>类型的内存8个字节,<code>int</code>类型的内存4个字节。<code>1. 5</code>是<code>double</code>类型,取值范围大于<code>int</code>。可以理解为<code>double</code>是8升的水壶,<code>int</code>是4升的水壶,不能把大水壶中的水直接放进小水壶去。</p><p>想要赋值成功,只有通过强制类型转换,将<code>double</code>类型强制转换成<code>int</code>类型才能赋值。</p><ul><li>强制类型转换:将取值范围大的类型强制转换成取值范围小的类型。</li></ul><p>比较而言,自动转换是Java自动执行的,而强制转换需要我们自己手动执行。</p><p>转换格式:</p><p><code>数据类型变量名=(数据类型)被转数据值;</code></p><p>将1. 5赋值到int类型,代码修改为:</p><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// double类型数据强制转成int类型,直接去掉小数点。</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token number">1</span><span class="token punctuation">.</span> <span class="token number">5</span> <span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>同样道理,当一个<code>short</code>类型与<code>1</code>相加,我们知道会类型提升,但是还想给结果赋值给<code>short</code>类型变量,就需要强制转换。</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//short类型变量,内存中 2 个字节</span> <span class="token keyword">short</span> s <span class="token operator">=</span> <span class="token number">1</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/* 出现编译失败 s和 1 做运算的时候, 1 是int类型,s会被提升为int类型 s+ 1 后的结果是int类型,将结果在赋值会short类型时发生错误 short内存 2 个字节,int类型 4 个字节 必须将int强制转成short才能完成赋值 */</span> s <span class="token operator">=</span> s <span class="token operator">+</span> <span class="token number">1</span> ;<span class="token comment" spellcheck="true">//编译失败</span> s <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">short</span><span class="token punctuation">)</span><span class="token punctuation">(</span>s<span class="token operator">+</span> <span class="token number">1</span> <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//编译成功</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="转换原理图解-1"><a href="#转换原理图解-1" class="headerlink" title="转换原理图解"></a>转换原理图解</h3><p><img src="/post/16c2569a/short%E8%BD%AC%E6%8D%A2%E5%8E%9F%E7%90%86.png"></p><h3 id="强烈注意"><a href="#强烈注意" class="headerlink" title="强烈注意"></a>强烈注意</h3><ul><li>浮点转成整数,直接取消小数点,可能造成数据损失精度。</li><li><code>int</code> 强制转成<code>short</code> 砍掉 2 个字节,可能造成数据丢失。</li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">// 定义s为short范围内最大值</span><span class="token keyword">short</span> s <span class="token operator">=</span> <span class="token number">32767</span> <span class="token punctuation">;</span><span class="token comment" spellcheck="true">// 运算后,强制转换,砍掉 2 个字节后会出现不确定的结果</span>s <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">short</span><span class="token punctuation">)</span><span class="token punctuation">(</span>s <span class="token operator">+</span> <span class="token number">10</span> <span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h2 id="1-3-ASCII编码表"><a href="#1-3-ASCII编码表" class="headerlink" title="1. 3 ASCII编码表"></a>1. 3 ASCII编码表</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//字符类型变量</span> <span class="token keyword">char</span> c <span class="token operator">=</span> <span class="token string">'a'</span><span class="token punctuation">;</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span> <span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//字符类型和int类型计算</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>c<span class="token operator">+</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//输出结果是 98</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>在计算机的内部都是二进制的0,1数据,如何让计算机可以直接识别人类文字的问题呢?就产生出了编码表的概念。</p><ul><li><p><strong>编码表</strong>:就是将人类的文字和一个十进制数进行对应起来组成一张表格。</p><p>人们就规定:</p><table><thead><tr><th align="center">字符</th><th align="center">数值</th></tr></thead><tbody><tr><td align="center">0</td><td align="center">48</td></tr><tr><td align="center">9</td><td align="center">57</td></tr><tr><td align="center">A</td><td align="center">65</td></tr><tr><td align="center">Z</td><td align="center">90</td></tr><tr><td align="center">a</td><td align="center">97</td></tr><tr><td align="center">z</td><td align="center">122</td></tr></tbody></table><p>将所有英文字母,数字,符号都和十进制进行了对应,因此产生了世界上第一张编码表ASCII(American Standard Code for Information Interchange 美国标准信息交换码)。</p><div class="row"> <embed src="./02-ASCII码表.pdf" width="100%" height="550" type="application/pdf"></div><blockquote><p>小贴士:<br>在char类型和int类型计算的过程中,char类型的字符先查询编码表,得到 97 ,再和 1 求和,结果为 98 。char类型提升<br>为了int类型。char类型内存 2 个字节,int类型内存 4 个字节。</p></blockquote></li></ul><h1 id="第二章运算符"><a href="#第二章运算符" class="headerlink" title="第二章运算符"></a>第二章运算符</h1><h2 id="2-1算数运算符"><a href="#2-1算数运算符" class="headerlink" title="2. 1算数运算符"></a>2. 1算数运算符</h2><table><thead><tr><th align="center">算数运算符包括:</th><th align="center"></th></tr></thead><tbody><tr><td align="center"><code>+</code></td><td align="center">加法运算,字符串连接运算</td></tr><tr><td align="center"><code>-</code></td><td align="center">减法运算</td></tr><tr><td align="center"><code>*</code></td><td align="center">乘法运算</td></tr><tr><td align="center"><code>/</code></td><td align="center">除法运算</td></tr><tr><td align="center"><code>%</code></td><td align="center">取模运算,两个数字相除取余数</td></tr><tr><td align="center"><code>++、--</code></td><td align="center">自增自减运算</td></tr></tbody></table><p>Java中,整体使用以上运算符,无论如何计算,也不会得到小数。</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1234</span> <span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>i<span class="token operator">/</span> <span class="token number">1000</span> <span class="token operator">*</span> <span class="token number">1000</span> <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//计算结果是 1000</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><ul><li><p><code>++</code>运算,变量自己增长1。反之,<code>--</code>计算,变量自己减少1,使用与<code>++</code>一致。</p><ul><li><p>独立运算:</p><ul><li>变量在独立运算时,<code>前++</code>和<code>后++</code>没有区别。</li><li>变量<code>前++</code> :例如:<code>++i</code></li><li>变量后++ :例如:<code>--i</code></li></ul></li><li><p>混合运算:</p><ul><li><p>和其他变量放在一起,<code>前++</code>和<code>后++</code>就产生了不同。</p></li><li><p>变量前++ :变量a自己加 1 ,将加 1 后的结果赋值给b,也就是说a先计算。a和b的结果都是 2 。</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token number">1</span> <span class="token punctuation">;</span> <span class="token keyword">int</span> b <span class="token operator">=</span> <span class="token operator">++</span>a<span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//计算结果是 2</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//计算结果是 2</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li><li><p>变量后++ :变量a先把自己的值 1 ,赋值给变量b,此时变量b的值就是 1 ,变量a自己再加 1 。a的结果是 2 ,b的结果是 1 。</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> a <span class="token operator">=</span> <span class="token number">1</span> <span class="token punctuation">;</span> <span class="token keyword">int</span> b <span class="token operator">=</span> a<span class="token operator">++</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//计算结果是 2</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//计算结果是 1</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li></ul></li></ul></li><li><p><code>+</code>符号在字符串中的操作:</p><ul><li><code>+</code>符号在遇到字符串的时候,表示<strong>连接,拼接</strong>的含义。</li><li>“a”+”b”的结果是“ab”,连接含义</li></ul></li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">" 5 + 5 ="</span><span class="token operator">+</span> <span class="token number">5</span> <span class="token operator">+</span> <span class="token number">5</span> <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//输出 5 + 5 = 55</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h2 id="2-2赋值运算符"><a href="#2-2赋值运算符" class="headerlink" title="2. 2赋值运算符"></a>2. 2赋值运算符</h2><table><thead><tr><th align="center">赋值运算符包括:</th><th align="center"></th></tr></thead><tbody><tr><td align="center"><code>=</code></td><td align="center">等于号</td></tr><tr><td align="center"><code>+=</code></td><td align="center">加等于</td></tr><tr><td align="center"><code>-=</code></td><td align="center">减等于</td></tr><tr><td align="center"><code>*=</code></td><td align="center">乘等于</td></tr><tr><td align="center"><code>/=</code></td><td align="center">除等于</td></tr><tr><td align="center"><code>%=</code></td><td align="center">取模等于</td></tr></tbody></table><ul><li>赋值运算符,就是将符号右边的值,赋给左边的变量。</li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">5</span> <span class="token punctuation">;</span> i<span class="token operator">+=</span> <span class="token number">5</span> <span class="token punctuation">;</span><span class="token comment" spellcheck="true">//计算方式 i=i+ 5 变量i先加 5 ,再赋值变量i</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//输出结果是 10 </span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="2-3比较运算符"><a href="#2-3比较运算符" class="headerlink" title="2. 3比较运算符"></a>2. 3比较运算符</h2><table><thead><tr><th align="center">比较运算符包括:</th><th align="center"></th></tr></thead><tbody><tr><td align="center"><code>==</code></td><td align="center">比较符号两边数据是否相等,相等结果是true。</td></tr><tr><td align="center"><code><</code></td><td align="center">比较符号左边的数据是否小于右边的数据,如果小于结果是true。</td></tr><tr><td align="center"><code>></code></td><td align="center">比较符号左边的数据是否大于右边的数据,如果大于结果是true。</td></tr><tr><td align="center"><code><=</code></td><td align="center">比较符号左边的数据是否小于或者等于右边的数据,如果小于或者等于结果是true。</td></tr><tr><td align="center"><code>>=</code></td><td align="center">比较符号左边的数据是否大于或者等于右边的数据,如果大于或者等于结果是true。</td></tr><tr><td align="center"><code>!=</code></td><td align="center">不等于符号 ,如果符号两边的数据不相等,结果是true。</td></tr></tbody></table><ul><li>比较运算符,是两个数据之间进行比较的运算,运算结果都是布尔值<code>true</code>或者false ``。</li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span> <span class="token number">1</span> <span class="token operator">==</span> <span class="token number">1</span> <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//true</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span> <span class="token number">1</span> <span class="token operator"><</span> <span class="token number">2</span> <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//true</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span> <span class="token number">3</span> <span class="token operator">></span> <span class="token number">4</span> <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//false</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span> <span class="token number">3</span> <span class="token operator"><=</span> <span class="token number">4</span> <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//true</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span> <span class="token number">3</span> <span class="token operator">>=</span> <span class="token number">4</span> <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//false</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span> <span class="token number">3</span> <span class="token operator">!=</span> <span class="token number">4</span> <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//true</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="2-4逻辑运算符"><a href="#2-4逻辑运算符" class="headerlink" title="2. 4逻辑运算符"></a>2. 4逻辑运算符</h2><table><thead><tr><th align="center">逻辑运算符包括:</th><th align="center"></th></tr></thead><tbody><tr><td align="center"><code>&& </code> 短路与</td><td align="center">1. 两边都是true,结果是true<br>2. 一边是false,结果是false<br>短路特点:符号左边是false,右边不再运算</td></tr><tr><td align="center">`</td><td align="center"></td></tr><tr><td align="center"><code>!</code>取反</td><td align="center">1.! true 结果是false<br>2.! false结果是true</td></tr></tbody></table><ul><li>逻辑运算符,是用来连接两个布尔类型结果的运算符,运算结果都是布尔值true或者false</li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token boolean">true</span> <span class="token operator">&&</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//true</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token boolean">true</span> <span class="token operator">&&</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//false</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token boolean">false</span> <span class="token operator">&&</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//false,右边不计算</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token boolean">false</span> <span class="token operator">||</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//falase</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token boolean">false</span> <span class="token operator">||</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//true</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token boolean">true</span> <span class="token operator">||</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//true,右边不计算</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//true</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="2-5三元运算符"><a href="#2-5三元运算符" class="headerlink" title="2. 5三元运算符"></a>2. 5三元运算符</h2><ul><li>三元运算符格式:</li></ul><p> <code>数据类型变量名=布尔类型表达式?结果1:结果2</code></p><ul><li>三元运算符计算方式:<ul><li>布尔类型表达式结果是true,三元运算符整体结果为结果 1 ,赋值给变量。</li><li>布尔类型表达式结果是false,三元运算符整体结果为结果 2 ,赋值给变量。</li></ul></li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token punctuation">(</span> <span class="token number">1</span> <span class="token operator">==</span> <span class="token number">2</span> <span class="token operator">?</span> <span class="token number">100</span> <span class="token operator">:</span> <span class="token number">200</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">// 200</span> <span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token punctuation">(</span> <span class="token number">3</span> <span class="token operator"><=</span> <span class="token number">4</span> <span class="token operator">?</span> <span class="token number">500</span> <span class="token operator">:</span> <span class="token number">600</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>j<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">// 500</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h1 id="第三章方法入门"><a href="#第三章方法入门" class="headerlink" title="第三章方法入门"></a>第三章方法入门</h1><h2 id="3-1概述"><a href="#3-1概述" class="headerlink" title="3. 1概述"></a>3. 1概述</h2><p>我们在学习运算符的时候,都为每个运算符单独的创建一个新的类和main方法,我们会发现这样编写代码非常的繁琐,而且重复的代码过多。能否避免这些重复的代码呢 ,就需要使用方法来实现。</p><ul><li>方法:就是将一个功能抽取出来,把代码单独定义在一个大括号内,形成一个单独的功能。</li></ul><p>当我们需要这个功能的时候,就可以去调用。这样即实现了代码的替代性,也解决了代码冗余的现象。</p><h2 id="3-2方法的定义"><a href="#3-2方法的定义" class="headerlink" title="3. 2方法的定义"></a>3. 2方法的定义</h2><ul><li><p>定义格式:</p><pre class="line-numbers language-java"><code class="language-java"> 修饰符返回值类型方法名(参数列表){ 代码<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> <span class="token keyword">return</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></li><li><p>定义格式解释:</p><ul><li>修饰符: 目前固定写法 <code>public static</code> 。</li><li>返回值类型: 目前固定写法 <code>void</code> ,其他返回值类型在后面讲。</li><li>方法名:为我们定义的方法起名,满足标识符的规范,用来调用方法。</li><li>参数列表: 目前无参数, 带有参数的方法在后面讲。</li><li>return:方法结束。因为返回值类型是void,方法大括号内的return可以不写。</li></ul></li><li><p>举例:</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">methodName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"这是一个方法"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></li></ul><h2 id="3-3方法的调用"><a href="#3-3方法的调用" class="headerlink" title="3. 3方法的调用"></a>3. 3方法的调用</h2><p>方法在定义完毕后,方法不会自己运行,必须被调用才能执行,我们可以在主方法main中来调用我们自己定义好的方法。在主方法中,直接写要调用的方法名字就可以调用了。</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//调用定义的方法method</span> <span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">//定义方法,被main方法调用</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"自己定义的方法,需要被main调用运行"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="3-4调用练习"><a href="#3-4调用练习" class="headerlink" title="3. 4调用练习"></a>3. 4调用练习</h2><p>将三元运算符代码抽取到自定义的方法中,并调用。</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//调用定义的方法operator</span> <span class="token function">operator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">//定义方法,方法中定义三元运算符</span><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">operator</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span> i <span class="token operator">=</span> <span class="token punctuation">(</span> <span class="token number">1</span> <span class="token operator">==</span> <span class="token number">2</span> <span class="token operator">?</span> <span class="token number">100</span> <span class="token operator">:</span> <span class="token number">200</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span> <span class="token punctuation">;</span> j <span class="token operator">=</span> <span class="token punctuation">(</span> <span class="token number">3</span> <span class="token operator"><=</span> <span class="token number">4</span> <span class="token operator">?</span> <span class="token number">500</span> <span class="token operator">:</span> <span class="token number">600</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>j<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="3-5注意"><a href="#3-5注意" class="headerlink" title="3. 5注意"></a>3. 5注意</h2><ul><li>方法定义注意事项:<ul><li> 方法必须定义在一类中方法外</li><li> 方法不能定义在另一个方法的里面</li></ul></li></ul><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Demo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//正确写法,类中,main方法外面可以定义方法</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Demo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//错误写法,一个方法不能定义在另一方法内部</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h1 id="第四章JShell脚本工具"><a href="#第四章JShell脚本工具" class="headerlink" title="第四章JShell脚本工具"></a>第四章JShell脚本工具</h1><h2 id="JShell脚本工具是JDK-9的新特性"><a href="#JShell脚本工具是JDK-9的新特性" class="headerlink" title="JShell脚本工具是JDK 9的新特性"></a>JShell脚本工具是JDK 9的新特性</h2><p>什么时候会用到<code>JShell</code>工具呢,当我们编写的代码非常少的时候,而又不愿意编写类,main方法,也不愿意去编译和运行,这个时候可以使用JShell工具。</p><p>启动JShell工具,在DOS命令行直接输入JShell命令。</p><p><img src="/post/16c2569a/JShell.png"></p><p>接下来可以编写Java代码,无需编写类和方法,直接编写方法中的代码即可,同时无需编译和运行,直接回车即可</p><p><img src="/post/16c2569a/JShell%E8%BF%90%E8%A1%8C.png"></p><blockquote><p>小贴士:</p><p>JShell工具,只适合片段代码的测试,开发更多内容,建议编写在方法中。</p></blockquote><h1 id="第五章扩展知识点"><a href="#第五章扩展知识点" class="headerlink" title="第五章扩展知识点"></a>第五章扩展知识点</h1><h2 id="5-1-符号的扩展"><a href="#5-1-符号的扩展" class="headerlink" title="5. 1 + =符号的扩展"></a>5. 1 + =符号的扩展</h2><p>下面的程序有问题吗?</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">short</span> s <span class="token operator">=</span> <span class="token number">1</span> <span class="token punctuation">;</span> s<span class="token operator">+=</span> <span class="token number">1</span> <span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>分析:<code>s + = 1</code>逻辑上平均值是<code>s = s + 1</code>计算结果被提升为<code>int</code>类型,再向<code>short</code>赋值值时发生错误,因为不能将取值范围大的类型赋值到取值范围小的类型。,但是,<code>s = s + 1进行两次运算</code>,<code>+ =</code>是一个运算符,只运算一次,并带有<strong>强制转换</strong>的特点,从而<code>s + = 1</code>就是<code>=(short)(s + 1)</code>,因此程序没有问题编译通过,运行结果是2。</p><h2 id="5-2常量和变量的运算"><a href="#5-2常量和变量的运算" class="headerlink" title="5. 2常量和变量的运算"></a>5. 2常量和变量的运算</h2><p>下面的程序有问题吗?</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">byte</span> b1 <span class="token operator">=</span> <span class="token number">1</span> <span class="token punctuation">;</span> <span class="token keyword">byte</span> b2 <span class="token operator">=</span> <span class="token number">2</span> <span class="token punctuation">;</span> <span class="token keyword">byte</span> b3 <span class="token operator">=</span> <span class="token number">1</span> <span class="token operator">+</span> <span class="token number">2</span> <span class="token punctuation">;</span> <span class="token keyword">byte</span> b4 <span class="token operator">=</span>b1 <span class="token operator">+</span> b2 <span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>b3 <span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>b4 <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>分析:<code>b3= 1 + 2</code>,<code>1</code>和2<code>是常量</code>,为固定不变的数据,在编译的时候(编译器的javac),已经确定了<code>1 + 2</code>查询查询结果的并没有超过<code>byte</code>类型的取值范围,可以赋予值给变量<code>b3</code>,因此<code>b3 = 1 + 2</code>是正确的。</p><p>反之,<code>b4 = b1 + b2</code>,<code>b1</code>和<code>b2</code>是变量,变量的值是可能变化的,在编译的时候,编译器的javac不确定<code>b1 + b2</code>的结果是什么,因此会将结果以<code>int</code>类型进行处理,所以<code>int</code>类型不能赋值给<code>byte</code>类型,因此编译失败。</p><p>在jshell中体现:</p><p><img src="/post/16c2569a/%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C.png"></p>]]></content>
<categories>
<category> Java基础 </category>
</categories>
<tags>
<tag> Java </tag>
</tags>
</entry>
<entry>
<title>hexo 博客添加pdf 插件</title>
<link href="post/a24fd5d2/"/>
<url>post/a24fd5d2/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><h1 id="一、安装插件"><a href="#一、安装插件" class="headerlink" title="一、安装插件"></a>一、安装插件</h1><p><code>npm install --save hexo-pdf</code></p><h1 id="二、编写"><a href="#二、编写" class="headerlink" title="二、编写"></a>二、编写</h1><blockquote><p>在成的md文件中添加pdf</p></blockquote><ol><li>外部链接:<br><code>{% pdf http://7xov2f.com1.z0.glb.clouddn.com/bash_freshman.pdf %}</code></li><li>本地连接:<br><code>{% pdf ./pdf名字.pdf %}</code></li></ol>]]></content>
<categories>
<category> Hexo </category>
</categories>
<tags>
<tag> Hexo </tag>
</tags>
</entry>
<entry>
<title>一、Mybatie框架</title>
<link href="post/455c316f/"/>
<url>post/455c316f/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><h1 id="一、Mybatis–-day01"><a href="#一、Mybatis–-day01" class="headerlink" title="一、Mybatis– day01"></a>一、Mybatis– day01</h1><h2 id="1、学习目标"><a href="#1、学习目标" class="headerlink" title="1、学习目标"></a>1、学习目标</h2><ol><li>能够搭建mybatis开发的环境</li><li>能够编写mybatis的入门程序</li><li>理解自定义mybatis的过程</li></ol><h2 id="2、学习指南"><a href="#2、学习指南" class="headerlink" title="2、学习指南"></a>2、学习指南</h2><ol><li>搭建mybatis开发的环境</li><li>编写mybatis的入门程序</li><li>自定义mybatis</li></ol><h2 id="3、什么是框架"><a href="#3、什么是框架" class="headerlink" title="3、什么是框架"></a>3、什么是框架</h2><p> 它是我们软件开发中的一套解决方案,不同框架解决的是不同的问题。</p><p> 使用框架的好处:</p><p> 框架封装了很多的细节,-使开发者可以使用极简的方式实现功能。大大提高开发效率。</p><h2 id="4、三层架构"><a href="#4、三层架构" class="headerlink" title="4、三层架构"></a>4、三层架构</h2><p> 表现层:</p><p> 是用于展示数据的</p><p> 业务层</p><p> 是处理业务需求</p><p> 持久层</p><p> 是和数据库交互的</p><p><img src="/post/455c316f/01%E4%B8%89%E5%B1%82%E6%9E%B6%E6%9E%84.png"></p><h2 id="5、持久层技术解决方案"><a href="#5、持久层技术解决方案" class="headerlink" title="5、持久层技术解决方案"></a>5、持久层技术解决方案</h2><p> JDBC技术:</p><p> Connection</p><p> PreparedStatement</p><p> ResultSet</p><p> Spring的jdbcTemplate:</p><p> Spring中对jdbc的简单封装</p><p> Apache的DBUtils:</p><p> 它和Spring的jdbcTemplate很像,也是对jdbc的简单封装</p><p> 以上这些都不是框架</p><p> JDBC 是规范</p><p> Spring的jdbcTemplate和Apache的DBUtils都只是工具类</p><h2 id="6、mybatis的概述"><a href="#6、mybatis的概述" class="headerlink" title="6、mybatis的概述"></a>6、mybatis的概述</h2><p> mybatis是一个持久层框架,用Java编写的。</p><p><img src="/post/455c316f/02%E6%8C%81%E4%B9%85%E5%B1%82%E6%80%BB%E5%9B%BE.jpg"></p><p> 它封装了jdbc操作的很多细节,使开发者只需要关注sql语句本身,而无需关注注册驱动,创建连接等繁琐过程</p><p> 它使用了ORM思想实现了结果集的封装。</p><p> ORM:</p><p> Object Relational Mappging 对象关系映射</p><p> 简单来说:</p><p> 就是数据库表和实现类及实体类的属性对应起来,让我们可以操作实体类就实现操作数据库表。</p><p> user User</p><p> id userId</p><p> user_name userName</p><p>今天需要做到:</p><p> 实体类中的属性和数据库表的字段名称保持一致。</p><p> user User</p><p> id id</p><p> user_name user_name</p><h2 id="7、mybatis的入门"><a href="#7、mybatis的入门" class="headerlink" title="7、mybatis的入门"></a>7、mybatis的入门</h2><ol><li><p>mybatis的环境搭建</p><p> 第一步:创建maven工程并导入坐标</p><p> 第二步:创建实体类和dao的接口</p><p> 第三步:创建Mybatis的主配置文件</p><p> SqlMapConifg.xml</p><p> 第四步:创建映射配置文件</p><p> UserDao.xml\</p><p>环境搭建的注意事项:</p><p> 第一个:创建UserDao.xml 和 UserDao.java时 名称是为了和我们之前的知识保持一致。</p><p> 在Mybatis中它把持久层的操作接口名称和映射文件也叫做:Mapper</p><p> 所以:UserDao 和 UserMapper是一样的</p><p> 第二个:在idea中创建目录的时候,它和包是不一样的</p><p> 包在创建时:com.jiangqi.dao它是三级结构</p><p> 目录在创建时:com.jiangqi.dao它是一级目录</p><p> 第三个:mybatis的映射配置文件位置必须和dao的结构保持相同</p><p> 第四个:映射配置文件的mapper标签 namespace 属性的取值必须是dao接口的全限定类名</p><p> 第五个:映射配置文件的操作设置(select),id 属性的取值必须是dao接口的方法名</p><p>好处:</p><p> 当我们遵守了第三,四,五条之后,我们在开发中就无需再写dao的实现类</p></li><li><p>mybatis的入门案例</p><p> 第一步:读取配置文件</p><p> 第二步:创建SqlSessionFactory工厂</p><p> 第三步:创建SqlSession</p><p> 第四步:创建Dao接口的代理对象</p><p> 第五步:执行Dao中的方法</p><p> 第六步:释放资源</p><p> </p><p> 注意事项:</p><p> <strong>不要忘记在映射配置中告知mybatis要封装到那个实体类中</strong></p><p> <strong>配置的方式:指定实体类的全限定类名</strong></p><p> mybatis基于注解的入门案例:</p><p> <u>把UserDao.xml移除,在dao接口的方法上使用@Select注解,并且指定SQL语句</u>,同时需要在SqlMapConfig.xml中的mapper配置时,使用class属性指定dao接口的全限定类名。</p></li></ol><p>明确:</p><p> 我们在实际开发中,都是越简便越好,所以都是采用不写dao实现类的方式。</p><p> 不管使用XML还是注解配置。但是Mybatis它是支持写dao实现类的。</p><p><img src="/post/455c316f/%E5%85%A5%E9%97%A8%E6%A1%88%E4%BE%8B%E7%9A%84%E5%88%86%E6%9E%90.png"></p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> Exception <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//1.读取配置文件</span> InputStream in <span class="token operator">=</span> Resources<span class="token punctuation">.</span><span class="token function">getResourceAsStream</span><span class="token punctuation">(</span><span class="token string">"SqlMapConfig.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//2.创建SqlSessionFactory工厂</span> <span class="token comment" spellcheck="true">/*创建工厂mabatis使用了构建者模式*/</span><span class="token comment" spellcheck="true">/*构建者模式:把对象的创建细节隐藏,使使用者直接调用方法即可拿到对象*/</span> SqlSessionFactoryBuilder builder<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">SqlSessionFactoryBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> SqlSessionFactory factory<span class="token operator">=</span>builder<span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//3.使用工厂生产SqlSession对象</span> <span class="token comment" spellcheck="true">/*生产SqlSession使用了工厂模式*/</span><span class="token comment" spellcheck="true">/*优势:解耦(降低类之间的依赖关系)*/</span> SqlSession session<span class="token operator">=</span>factory<span class="token punctuation">.</span><span class="token function">openSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//4.使用SqlSession创建Dao接口的代理对象</span> <span class="token comment" spellcheck="true">/*创建Dao接口实现类使用了代理模式*/</span><span class="token comment" spellcheck="true">/*优势:不修改源码的基础上对已有方法的增强*/</span> UserDao userDao<span class="token operator">=</span>session<span class="token punctuation">.</span><span class="token function">getMapper</span><span class="token punctuation">(</span>UserDao<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//5.使用代理对象执行方法</span> List<span class="token operator"><</span>User<span class="token operator">></span> users<span class="token operator">=</span>userDao<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>User user<span class="token operator">:</span>users<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//6.释放资源</span> session<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> in<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="8、自定义Mybatis的分析"><a href="#8、自定义Mybatis的分析" class="headerlink" title="8、自定义Mybatis的分析"></a>8、自定义Mybatis的分析</h2><p> mybatis 在使用代理dao的方式实现增删改查时做什么事?</p><p> 两件事:</p><p> 第一:创建代理对象</p><p> 第二:在代理对象中调用selectList</p><p><img src="/post/455c316f/%E8%87%AA%E5%AE%9A%E4%B9%89Mybatis%E5%88%86%E6%9E%90.png"></p><h1 id="二、Mybatis-day02"><a href="#二、Mybatis-day02" class="headerlink" title="二、Mybatis-day02"></a>二、Mybatis-day02</h1><h2 id="1、学习目标-1"><a href="#1、学习目标-1" class="headerlink" title="1、学习目标"></a>1、学习目标</h2><ol><li><p>mybatis的curd(基于代理Dao的方式)</p></li><li><p>mybatis的参数深入及结果集的深入</p></li><li><p>mybatis中基于传统dao的方式(编写dao的实现类)———–了解内容</p></li><li><p>mybatis中的配置(主配置文件:SqlMapConfig.xml)</p><p> properties标签</p><p> typeAliases标签 —解释Integer的写法</p><p> mappers标签</p></li></ol><hr><p>OGNL 表达式:</p><p> Object Graphic Navigation Language</p><p> 对象 图 导航 语言</p><p> 他是通过对象的取值方法来获取数据的。在写法上把get省略。</p><p> 比如:我们获取用户的名称</p><p> 类中的写法:user.getUserName();</p><p> ONGL表示写法:user.username</p><p> mybatis 中为什么能直接写username,而不用user. </p><p> 是因为在parameterType中已经提供了属性所属的类。</p>]]></content>
<categories>
<category> Mybatis </category>
</categories>
<tags>
<tag> Mybatis </tag>
</tags>
</entry>
<entry>
<title>day-01-前言、入门程序、常量、变量</title>
<link href="post/490ca535/"/>
<url>post/490ca535/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><h1 id="day-01-【前言、入门程序、常量、变量】"><a href="#day-01-【前言、入门程序、常量、变量】" class="headerlink" title="day 01 【前言、入门程序、常量、变量】"></a>day 01 【前言、入门程序、常量、变量】</h1><h1 id="第一章-开发前言"><a href="#第一章-开发前言" class="headerlink" title="第一章 开发前言"></a>第一章 开发前言</h1><h2 id="1-1-Java语言概述"><a href="#1-1-Java语言概述" class="headerlink" title="1. 1 Java语言概述"></a>1. 1 Java语言概述</h2><h2 id="什么是Java语言"><a href="#什么是Java语言" class="headerlink" title="什么是Java语言"></a>什么是Java语言</h2><p>Java语言是美国Sun公司(Stanford University Network),在 1995 年推出的高级的编程语言。所谓编程语言,是<br>计算机的语言,人们可以使用编程语言对计算机下达命令,让计算机完成人们需要的功能。</p><h2 id="Java语言发展历史"><a href="#Java语言发展历史" class="headerlink" title="Java语言发展历史"></a>Java语言发展历史</h2><blockquote><p>1995 年Sun公司发布Java 1. 0 版本<br> 1997 年发布Java 1. 1 版本<br> 1998 年发布Java 1. 2 版本<br> 2000 年发布Java 1. 3 版本<br> 2002 年发布Java 1. 4 版本<br> 2004 年发布Java 1. 5 版本<br> 2006 年发布Java 1. 6 版本</p></blockquote><pre><code>2009 年Oracle甲骨文公司收购Sun公司,并于 2011 发布Java 1. 7 版本2014 年发布Java 1. 8 版本2017 年发布Java 9. 0 版本</code></pre><h3 id="Java语言能做什么"><a href="#Java语言能做什么" class="headerlink" title="Java语言能做什么"></a>Java语言能做什么</h3><p>Java语言主要应用在互联网程序的开发领域。常见的互联网程序比如天猫、京东、物流系统、网银系统等,以及服务器后台处理大数据的存储、查询、数据挖掘等也有很多应用。</p><h2 id="1-2-计算机基础知识"><a href="#1-2-计算机基础知识" class="headerlink" title="1. 2 计算机基础知识"></a>1. 2 计算机基础知识</h2><h3 id="二进制"><a href="#二进制" class="headerlink" title="二进制"></a>二进制</h3><h5 id="计算机中的数据不同于人们生活中的数据,人们生活采用十进制数,而计算机中全部采用二进制数表示,它只包含"><a href="#计算机中的数据不同于人们生活中的数据,人们生活采用十进制数,而计算机中全部采用二进制数表示,它只包含" class="headerlink" title="计算机中的数据不同于人们生活中的数据,人们生活采用十进制数,而计算机中全部采用二进制数表示,它只包含"></a>计算机中的数据不同于人们生活中的数据,人们生活采用十进制数,而计算机中全部采用二进制数表示,它只包含</h5><p>0 、 1 两个数,逢二进一, 1 + 1 = 10 。每一个 0 或者每一个 1 ,叫做一个bit(比特)。</p><p>下面了解一下十进制和二进制数据之间的转换计算。</p><pre><code>十进制数据转成二进制数据:使用除以 2 获取余数的方式</code></pre><p><img src="/post/490ca535/%E5%8D%81%E8%BF%9B%E5%88%B6%E8%BD%AC%E4%BA%8C%E8%BF%9B%E5%88%B6.png"></p><h5 id="二进制数据转成十进制数据:使用-8421-编码的方式"><a href="#二进制数据转成十进制数据:使用-8421-编码的方式" class="headerlink" title="二进制数据转成十进制数据:使用 8421 编码的方式"></a>二进制数据转成十进制数据:使用 8421 编码的方式</h5><p><img src="/post/490ca535/%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%BD%AC%E5%8D%81%E8%BF%9B%E5%88%B6.png"></p><blockquote><p>小贴士:</p><p>二进制数系统中,每个 0 或 1 就是一个位,叫做bit(比特)。</p></blockquote><h3 id="字节"><a href="#字节" class="headerlink" title="字节"></a>字节</h3><h5 id="字节是我们常见的计算机中最小存储单元。计算机存储任何的数据,都是以字节的形式存储,右键点击文件属性,我们可以查看文件的字节大小。"><a href="#字节是我们常见的计算机中最小存储单元。计算机存储任何的数据,都是以字节的形式存储,右键点击文件属性,我们可以查看文件的字节大小。" class="headerlink" title="字节是我们常见的计算机中最小存储单元。计算机存储任何的数据,都是以字节的形式存储,右键点击文件属性,我们可以查看文件的字节大小。"></a>字节是我们常见的计算机中最小存储单元。计算机存储任何的数据,都是以字节的形式存储,右键点击文件属性,我们可以查看文件的字节大小。</h5><p>8 个bit(二进制位) 0000 - 0000 表示为 1 个字节,写成 1 byte或者 1 B。</p><pre><code>8 bit = 1 B1024 B = 1 KB1024 KB = 1 MB1024 MB = 1 GB1024 GB = 1 TB</code></pre><h3 id="常用DOS命令"><a href="#常用DOS命令" class="headerlink" title="常用DOS命令"></a>常用DOS命令</h3><p>Java语言的初学者,学习一些DOS命令,会非常有帮助。DOS是一个早期的操作系统,现在已经被Windows系统取<br>代,对于我们开发人员,目前需要在DOS中完成一些事情,因此就需要掌握一些必要的命令。</p><ul><li>进入DOS操作窗口</li></ul><pre><code>按下Windows+R键盘,打开运行窗口,输入cmd回车,进入到DOS的操作窗口。</code></pre><pre><code>打开DOS命令行后,看到一个路径 c:\user 就表示我们现在操作的磁盘是c盘。</code></pre><ul><li>常用命令</li></ul><p>命令 操作符号</p><table><thead><tr><th align="center">盘符切换命令</th><th align="center">盘符名</th></tr></thead><tbody><tr><td align="center">查看当前文件夹</td><td align="center">dir</td></tr><tr><td align="center">进入文件夹命令</td><td align="center">cd 文件夹名</td></tr><tr><td align="center">退出文件夹命令</td><td align="center">cd..</td></tr><tr><td align="center">退出到磁盘根目录</td><td align="center">cd\</td></tr><tr><td align="center">清屏</td><td align="center">cls</td></tr></tbody></table><h1 id="第二章Java语言开发环境搭建"><a href="#第二章Java语言开发环境搭建" class="headerlink" title="第二章Java语言开发环境搭建"></a>第二章Java语言开发环境搭建</h1><h2 id="2-1个Java虚拟机——JVM"><a href="#2-1个Java虚拟机——JVM" class="headerlink" title="2. 1个Java虚拟机——JVM"></a>2. 1个Java虚拟机——JVM</h2><ul><li>JVM(Java Virtual Machine ):Java虚拟机,简称JVM,是运行所有Java程序的假想计算机,是Java程序的运行环境,是Java 最具吸引力的特性之一。我们编写的Java代码,都运行在JVM 之上。</li><li>跨平台:任何软件的运行,都必须要运行在操作系统之上,而我们用Java编写的软件可以运行在任何的操作系统上,这个特性称为Java语言的跨平台特性。该特性是由JVM实现的,我们编写的程序运行在JVM上,而JVM运行在操作系统上。</li></ul><p><img src="/post/490ca535/JVM.png"></p><p>如上图所示,Java的虚拟机本身不具有跨平台功能的,每个操作系统下都有不同版本的虚拟机。</p><h2 id="2-2-JRE和JDK"><a href="#2-2-JRE和JDK" class="headerlink" title="2. 2 JRE和JDK"></a>2. 2 JRE和JDK</h2><ul><li>JRE (Java Runtime Environment) :是Java程序的运行时环境,包含<code>JVM</code> 和运行时所需要的核心类库 。</li><li>JDK (Java Development Kit):是Java程序开发工具包,包含<code>JRE</code> 和开发人员使用的工具。</li></ul><p>我们想要运行一个已有的Java程序,那么只需安装<code>JRE</code>即可。</p><p>我们想要开发一个全新的Java程序,那么必须安装<code>JDK</code>。</p><blockquote><p>小贴士:</p><p> 三者关系:JDK> JRE> JVM</p></blockquote><h2 id="2-3-JDK-9安装图解"><a href="#2-3-JDK-9安装图解" class="headerlink" title="2. 3 JDK 9安装图解"></a>2. 3 JDK 9安装图解</h2><p><img src="/post/490ca535/jDK1.png"></p><p><img src="/post/490ca535/jDK2.png"></p><p><img src="/post/490ca535/jDK3.png"></p><p><img src="/post/490ca535/jDK4.png"></p><p><img src="/post/490ca535/jDK5.png"></p><blockquote><p>小贴士:</p><p> 安装路径中,不要包含中文和空格</p></blockquote><h2 id="2-4-JAVA-HOME环境变量的配置"><a href="#2-4-JAVA-HOME环境变量的配置" class="headerlink" title="2. 4 JAVA_HOME环境变量的配置"></a>2. 4 JAVA_HOME环境变量的配置</h2><h3 id="配置环境变量作用"><a href="#配置环境变量作用" class="headerlink" title="配置环境变量作用"></a>配置环境变量作用</h3><p>开发Java程序,需要使用JDK中提供的工具,工具在JDK 9安装目录的<code>bin</code>目录下。</p><p><img src="/post/490ca535/%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F.png"></p><p>在DOS命令行下使用这些工具,就要先进入到JDK的bin目录下,这个过程就会非常的麻烦。</p><p><img src="/post/490ca535/bin.png"></p><p>不进入JDK的bin目录,这些工具就不能使用,会报错。</p><p><img src="/post/490ca535/bin1.png"></p><p> 为了开发方便,我们想在<strong>任意的目录下都可以使用JDK的开发工具</strong>,则必须要配置环境变量,配置环境变量的意义:<strong>在于告诉操作系统,我们使用的JDK开发工具在其中目录下。</strong></p><h3 id="配置环境变量步骤"><a href="#配置环境变量步骤" class="headerlink" title="配置环境变量步骤"></a>配置环境变量步骤</h3><h4 id="Windows-7、8版本"><a href="#Windows-7、8版本" class="headerlink" title="Windows 7、8版本"></a>Windows 7、8版本</h4><ol><li><p>计算机鼠标右键,选择属性</p></li><li><p>选择高级系统设置</p></li><li><p>高级选项卡,点击环境变量</p></li><li><p>点击新建,创建新的环境变量</p></li><li><p>变量名输入<code>JAVA_HOME</code>,变量值输入JDK 9的安装目录c:\ Java 9 \ jdk- 9. 0. 1</p></li><li><p>进入路径环境变量,双击或者单击编辑</p></li><li><p>在变量值的最前面,键入<code>%JAVA_HOME%\ bin;</code> 分号必须要写,必须是英文格式。 </p></li><li><p>环境变量配置完成,重新打开DOS命令行,在任意目录下输入javac命令,运行成功。</p></li></ol><h4 id="Windows-10版本"><a href="#Windows-10版本" class="headerlink" title="Windows 10版本"></a>Windows 10版本</h4><ol><li>文件资源管理器–>此电脑鼠标右键–>选择属性</li><li>选择高级系统设置–>选择环境变量</li><li>单击下面的系统变量的新建,创建新的环境变量,变量名输入<code>JAVA_HOME</code>,变量值输入JDK 9的安装目录</li></ol><pre><code>D:\ 02 _DevelopTools\Java\jdk-^9.^0.^1</code></pre><ol start="4"><li>进入路径环境变量,双击或者单击编辑</li><li>单击新建,键入<code>%JAVA_HOME%\ bin</code>,必须是英文格式。选择该行,上移到最上方,单击确定。</li><li>环境变量配置完成,重新打开DOS命令行,在任意目录下输入javac命令。</li></ol><p><img src="/post/490ca535/javac%E5%91%BD%E4%BB%A4.png"></p><h1 id="第三章-HelloWorld入门程序"><a href="#第三章-HelloWorld入门程序" class="headerlink" title="第三章 HelloWorld入门程序"></a>第三章 HelloWorld入门程序</h1><h2 id="3-1程序开发步骤说明"><a href="#3-1程序开发步骤说明" class="headerlink" title="3. 1程序开发步骤说明"></a>3. 1程序开发步骤说明</h2><p>开发环境已经搭建完毕,可以开发我们第一个Java程序了。</p><p>Java程序开发三步骤:编写,编译,运行。</p><p><img src="/post/490ca535/java%E8%BF%90%E8%A1%8C.png"></p><h2 id="3-2编写Java源程序"><a href="#3-2编写Java源程序" class="headerlink" title="3. 2编写Java源程序"></a>3. 2编写Java源程序</h2><ol><li><p>在d:\day 01 目录下新建文本文件,完整的文件名修改为HelloWorld.java,其中文件名为HelloWorld,后<br> 缀名必须为.java^ 。</p></li><li><p> 用记事本打开</p></li></ol><blockquote><p>使用notepad++记事本软件。</p></blockquote><ol start="3"><li> 在文件中键入文本并保存,代码如下:</li></ol><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HelloWorld</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Hello World!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>文件名必须是HelloWorld ,保证文件名和类的名字是一致的,注意大小写。<br>每个字母和符号必须与示例代码一模一样。</p></blockquote><p>第一个HELLOWORD源程序就编写完成了,但是这个文件是程序员编写的,JVM是看不懂的,也就不能运行,因此我们必须将关系编写好的Java的源文件编译成JVM可以看懂的字节码文件。</p><h2 id="3-3编译Java源文件"><a href="#3-3编译Java源文件" class="headerlink" title="3. 3编译Java源文件"></a>3. 3编译Java源文件</h2><p>在DOS命令行中,进入Java源文件的目录,使用<code>javac</code>命令进行编译。</p><p>命令:</p><p><code>javac Java源文件名.后缀名</code></p><p>举例:</p><p><code>javac HelloWorld.java</code></p><p>打开:\ day 01目录,发现产生了一个新的文件HelloWorld.class,该文件<br>就是编译后的文件,是Java的可运行文件,称为字节码文件,有了字节码文件,就可以运行程序了。</p><blockquote><p>Java源文件的编译工具javac.exe,在JDK安装目录的bin目录下。但是由于配置了环境变量,可以再任意目录下使用。</p></blockquote><h2 id="3-4运行Java程序"><a href="#3-4运行Java程序" class="headerlink" title="3. 4运行Java程序"></a>3. 4运行Java程序</h2><p>在DOS命令行中,进入Java源文件的目录,使用<code>java</code>命令进行运行。</p><p>命令:</p><p><code>java 类名字</code></p><p>举例: </p><p><code>java HelloWorld</code></p><blockquote><p>java HelloWord 不要写 不要写 不要写 .class</p></blockquote><p><img src="/post/490ca535/%E8%BF%90%E8%A1%8CJava%E7%A8%8B%E5%BA%8F.png"></p><blockquote><p>Java程序.class文件 的运行工具java.exe ,在JDK安装目录的bin目录下。但是由于配置了环境变量,可以再任意目录下使用。</p></blockquote><h2 id="3-5入门程序说明"><a href="#3-5入门程序说明" class="headerlink" title="3. 5入门程序说明"></a>3. 5入门程序说明</h2><h3 id="编译和运行是两回事"><a href="#编译和运行是两回事" class="headerlink" title="编译和运行是两回事"></a>编译和运行是两回事</h3><ul><li>编译:是指将我们编写的Java源文件翻译成JVM认识的class文件,在这个过程中,<code>javac</code> 编译器会检查我们所写的程序是否有错误,有错误就会提示出来,如果没有错误就会编译成功。</li><li>运行:是指将<code>class文件</code> 交给JVM去运行,此时JVM就会去执行我们编写的程序了。</li></ul><h3 id="关于主要方法"><a href="#关于主要方法" class="headerlink" title="关于主要方法"></a>关于主要方法</h3><ul><li>main方法:称为主方法。写法是固定格式不可以更改。main方法是程序的入口点或起始点,无论我们编写多少程序,JVM在运行的时候,都会从main方法这里开始执行。</li></ul><h2 id="3-6添加注释"><a href="#3-6添加注释" class="headerlink" title="3. 6添加注释"></a>3. 6添加注释</h2><ul><li><strong>注释</strong>:就是对代码的解释和说明。其目的是让人们能够更加轻松地了解代码。为代码添加注释,是十分必须要的,它不影响程序的编译和运行。</li><li>Java中有单行注释和多行注释<ul><li>单行注释以 <code>//开头 换行结束</code></li><li>多行注释以 <code>/\*开头 以*/结束</code></li></ul></li></ul><h2 id="3-7关键字关键词"><a href="#3-7关键字关键词" class="headerlink" title="3. 7关键字关键词"></a>3. 7关键字关键词</h2><ul><li><strong>关键字:</strong>是指在程序中,Java已经定义好的单词,具有特殊含义。<ul><li>HelloWorld案例中,出现的关键字有 <code>public</code> 、<code>class</code> 、 <code>static</code> 、 <code>void</code> 等,这些单词已经被Java定义好,全部都是小写字母,notepad++中颜色特殊。</li><li>关键字比较多,不能死记硬背,学到哪里记到哪里即可。</li></ul></li></ul><h2 id="3-8标识符"><a href="#3-8标识符" class="headerlink" title="3. 8标识符"></a>3. 8标识符</h2><ul><li><strong>标识符</strong>:是指在程序中,我们自己定义的内容。某些类的名字,方法的名字和变量的名字等等,都是标识符。<ul><li>HelloWorld案例中,出现的标识符有类名字<code>HelloWorld</code> 。</li></ul></li></ul><pre><code>字符常量 单引号引起来,只能写一个字符,必须有内容 'a' , ' ', '好'字符串常量 双引号引起来,可以写多个字符,也可以不写 "A" ,"Hello" ,"你好" ,""布尔常量 只有两个值(流程控制中讲解) true , false空常量 只有一个值(引用数据类型中讲解) null</code></pre><ul><li><p><strong>命名规则</strong>:**<code>硬性要求</code>**</p><ul><li>标识符可以包含<code>英文字母26个(区分大小写)</code>,<code>0-9数字</code>,<code>$(美元符号)</code>和<code>_(下划线)</code>。</li><li>标识符不能以数字开头。</li><li>标识符不能是关键字。</li></ul></li><li><p><strong>命名规范</strong>:**<code>软性建议</code>**</p><ul><li>类名规范:首字母大写,后面每个单词首字母大写(大驼峰式)。</li><li>方法名规范:首字母小写,后面每个单词首字母大写(小驼峰式)。</li><li>变量名规范:全部小写。</li></ul></li></ul><h1 id="第四章常量"><a href="#第四章常量" class="headerlink" title="第四章常量"></a>第四章常量</h1><h2 id="4-1概述"><a href="#4-1概述" class="headerlink" title="4. 1概述"></a>4. 1概述</h2><p>常量:是指在Java程序中固定不变的数据。</p><h2 id="4-2分类"><a href="#4-2分类" class="headerlink" title="4. 2分类"></a>4. 2分类</h2><table><thead><tr><th align="center">类型</th><th align="center">含义</th><th align="center">数据抽取</th></tr></thead><tbody><tr><td align="center">整体常量</td><td align="center">所有的整体</td><td align="center">0,1,567,-9</td></tr><tr><td align="center">小数常量</td><td align="center">所有的小数</td><td align="center">0. 0,-0. 1,2. 55</td></tr><tr><td align="center">字符常量</td><td align="center">单引号引起来,只能写一个字符,必须有内容</td><td align="center">‘a’, ‘ ’, ‘好’</td></tr><tr><td align="center">字符串常量</td><td align="center">双引号引起来,可以写多个字符,也可以不写</td><td align="center">“A”,“Hello”,“”</td></tr><tr><td align="center">布尔常量</td><td align="center">只有两个值</td><td align="center">true,false</td></tr><tr><td align="center">空常量</td><td align="center">只有一个值</td><td align="center">null</td></tr></tbody></table><h2 id="4-3练习"><a href="#4-3练习" class="headerlink" title="4. 3练习"></a>4. 3练习</h2><h5 id="需求:输出各种类型的常量。"><a href="#需求:输出各种类型的常量。" class="headerlink" title="需求:输出各种类型的常量。"></a>需求:输出各种类型的常量。</h5><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ConstantDemo</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//输出整数常量</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span> <span class="token number">123</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//输出小数常量</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span> <span class="token number">0</span><span class="token punctuation">.</span> <span class="token number">125</span> <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//输出字符常量</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">'A'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//输出布尔常量</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//输出字符串常量</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"你好Java"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h1 id="第五章变量和数据类型"><a href="#第五章变量和数据类型" class="headerlink" title="第五章变量和数据类型"></a>第五章变量和数据类型</h1><h2 id="5-1变量概述"><a href="#5-1变量概述" class="headerlink" title="5. 1变量概述"></a>5. 1变量概述</h2><ul><li><p><strong>变量:常量是固定不变的数据,那么在程序中可以变化的量称为变量</strong>。</p><blockquote><p>数学中,可以使用字母代替数字运算,例如 x= 1 + 5 或者 6 =x+ 5 。</p><p>程序中,可以使用字母保存数字的方式进行运算,提高计算能力,可以解决更多的问题。比如x保存 5 ,x也可以保存 6 ,这样x保存的数据是可以改变的,也就是我们所讲解的变量。</p></blockquote></li></ul><p>Java中要求一个变量每次只能保存一个数据,必须要明确保存的数据类型。</p><h2 id="5-2数据类型"><a href="#5-2数据类型" class="headerlink" title="5. 2数据类型"></a>5. 2数据类型</h2><h2 id="数据类型分类"><a href="#数据类型分类" class="headerlink" title="数据类型分类"></a>数据类型分类</h2><p>Java的数据类型分为两大类:</p><ul><li>基本数据类型:包括 整数、浮点数、字符、布尔。</li><li>引用数据类型:包括 类、数组、接口。</li></ul><h2 id="基本数据类型"><a href="#基本数据类型" class="headerlink" title="基本数据类型"></a>基本数据类型</h2><p>四类八种基本数据类型:</p><table><thead><tr><th align="center">数据类型</th><th align="center">关键字</th><th align="center">内存占用</th><th align="center">取值范围</th></tr></thead><tbody><tr><td align="center">字节型</td><td align="center">byte</td><td align="center">1 个字节</td><td align="center">128 ~ 127</td></tr><tr><td align="center">短整型</td><td align="center">short</td><td align="center">2 个字节</td><td align="center">- 32768 ~ 32767</td></tr><tr><td align="center">整型</td><td align="center">int(默认)</td><td align="center">4 个字节</td><td align="center">- 2的31 次方~ 2 的 31 次方- 1</td></tr><tr><td align="center">长整型</td><td align="center">long</td><td align="center">8 个字节</td><td align="center">- 2 的 63 次方~ 2 的 63 次方- 1</td></tr><tr><td align="center">单精度浮点数</td><td align="center">float</td><td align="center">4 个字节</td><td align="center">1. 4013 E- 45 ~ 3. 4028 E+ 38</td></tr><tr><td align="center">双精度浮点数</td><td align="center">double(默认)</td><td align="center">8 个字节</td><td align="center">4. 9 E- 324 ~ 1. 7977 E+ 308</td></tr><tr><td align="center">字符型</td><td align="center">char</td><td align="center">2 个字节</td><td align="center">0 - 65535</td></tr><tr><td align="center">布尔类型</td><td align="center">boolean</td><td align="center">1 个字节</td><td align="center">true,false</td></tr></tbody></table><blockquote><ol><li>字符串不是基本类型,而是引用类型。</li><li>浮点型可能只是一个近似值,并非精确的值。</li><li>数据范围与字节数不一定相关,例如float数据范围比long更加广泛,但是float是4字节,long是8字节。</li><li>浮点数当中默认类型是double。如果一定要使用float类型,需要加上一个后缀F。<br>如果是整数,默认为int类型,如果一定要使用long类型,需要加上一个后缀L。推荐使用大写字母后缀。</li></ol></blockquote><h2 id="5-3变量的定义"><a href="#5-3变量的定义" class="headerlink" title="5. 3变量的定义"></a>5. 3变量的定义</h2><p> 变量定义的格式包括三个要素:<code>数据类型</code>,<code>变量名</code>,<code>数据值</code>。</p><h3 id="格式"><a href="#格式" class="headerlink" title="格式"></a>格式</h3><p><code>数据类型 变量名 = 数据值</code></p><h3 id="练习"><a href="#练习" class="headerlink" title="练习"></a>练习</h3><p>定义所有基本数据类型的变量,代码如下:</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Variable</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//定义字节型变量</span> <span class="token keyword">byte</span> b <span class="token operator">=</span> <span class="token number">100</span> <span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//定义短整型变量</span> <span class="token keyword">short</span> s <span class="token operator">=</span> <span class="token number">1000</span> <span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//定义整型变量</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">123456</span> <span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//定义长整型变量</span> <span class="token keyword">long</span> l <span class="token operator">=</span> <span class="token number">12345678900</span> L<span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>l<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//定义单精度浮点型变量</span> <span class="token keyword">float</span> f <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">.</span> <span class="token number">5</span> F<span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>f<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//定义双精度浮点型变量</span> <span class="token keyword">double</span> d <span class="token operator">=</span> <span class="token number">8</span><span class="token punctuation">.</span> <span class="token number">5</span> <span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>d<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//定义布尔型变量</span> <span class="token keyword">boolean</span> bool <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bool<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//定义字符型变量</span> <span class="token keyword">char</span> c <span class="token operator">=</span> <span class="token string">'A'</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>long类型:建议数据后加L表示。<br> float类型:建议数据后加F表示。</p></blockquote><h2 id="5-4注意"><a href="#5-4注意" class="headerlink" title="5. 4注意"></a>5. 4注意</h2><ul><li><p>变量名称:在同一个大括号范围内,变量的名字不可以相同。</p></li><li><p>变量赋值:定义的变量,不赋值不能使用。</p></li></ul>]]></content>
<categories>
<category> Java基础 </category>
</categories>
<tags>
<tag> Java </tag>
</tags>
</entry>
<entry>
<title>Anaconda 配置国内下载镜像</title>
<link href="post/1b524221/"/>
<url>post/1b524221/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><h2 id="Anaconda-配置国内下载镜像"><a href="#Anaconda-配置国内下载镜像" class="headerlink" title="Anaconda 配置国内下载镜像"></a>Anaconda 配置国内下载镜像</h2><ul><li><p>查看下载镜像</p><blockquote><p>conda config –show channels</p></blockquote></li><li><p>添加下载镜像</p><blockquote><p>conda config –add channels <a href="https://mirrors.aliyun.com/pypi/simple/">https://mirrors.aliyun.com/pypi/simple/</a></p><p>conda config –add channels <a href="https://pypi.mirrors.ustc.edu.cn/simple/">https://pypi.mirrors.ustc.edu.cn/simple/</a></p><p>conda config –add channels <a href="http://pypi.douban.com/simple/">http://pypi.douban.com/simple/</a></p><p>conda config –add channels <a href="https://pypi.tuna.tsinghua.edu.cn/simple/">https://pypi.tuna.tsinghua.edu.cn/simple/</a></p></blockquote><p>1 阿里云, 2 中国科技大学,3 豆瓣,4清华大学,</p></li><li><p>删除下载镜像</p><blockquote><p>conda config –remove channels <a href="https://mirrors.aliyun.com/pypi/simple/">https://mirrors.aliyun.com/pypi/simple/</a></p></blockquote></li></ul><h2 id="Anaconda基本操作"><a href="#Anaconda基本操作" class="headerlink" title="Anaconda基本操作"></a>Anaconda基本操作</h2><ul><li><p>查看虚拟环境</p><blockquote><p>C:\Users\chaom>conda env list</p><p>base * E:\ProgramData\Anaconda3 (星号表示当前激活的环境)<br>Wu_dep E:\ProgramData\Anaconda3\envs\Wu_dep</p></blockquote></li><li><p>创建虚拟环境</p><blockquote><p>conda create -n your_env_name python=3.8</p></blockquote></li><li><p>导出环境</p><blockquote><p>conda env export > environment.yaml</p></blockquote></li><li><p>拷贝环境</p><blockquote><p>conda env create -f environment.yaml</p></blockquote></li><li><p>激活环境</p><blockquote><p>activate your_env_name</p></blockquote></li><li><p>删除环境及其下属安装包</p><blockquote><p>conda remove -n your_env_name –all</p></blockquote></li><li><p>查看当前环境下的安装的python扩展库</p><blockquote><p>conda list</p></blockquote></li><li><p>安装python扩展库</p><blockquote><p>conda install numpy</p><p>pip install numpy</p></blockquote></li><li><p>卸载python扩展库</p><blockquote><p>conda remove numpy</p><p>pip uninstall numpy</p></blockquote></li></ul>]]></content>
<categories>
<category> Python </category>
</categories>
<tags>
<tag> Python </tag>
</tags>
</entry>
<entry>
<title>获取Class对象的三种方式</title>
<link href="post/da2b7869/"/>
<url>post/da2b7869/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><h1 id="获取Class对象的三种方式"><a href="#获取Class对象的三种方式" class="headerlink" title="获取Class对象的三种方式"></a>获取Class对象的三种方式</h1><ol><li>Object ——> getClass() 通过对象.getclass</li><li>任何数据类型(包括基本数据类型)都有一个“静态”的class属性 通过类名.class</li><li>通过Class类的静态方法:forName(String className)(常用) 通过对象.forname</li></ol><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Fanshe</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//第一种方式获取Class对象 </span> Student stu1<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//这一new 产生一个Student对象,一个Class对象。</span> Class <span class="token class-name">stuClass</span><span class="token operator">=</span>stu1<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//获取Class对象 System.out.println(stuClass.getName());</span> <span class="token comment" spellcheck="true">//第二种方式获取Class对象 </span> Class <span class="token class-name">stuClass2</span><span class="token operator">=</span>Student<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stuClass <span class="token operator">==</span> stuClass2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//第三种方式获取Class对象 </span> <span class="token keyword">try</span> <span class="token punctuation">{</span> Class <span class="token class-name">stuClass3</span><span class="token operator">=</span>Class<span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span><span class="token string">"lianxiApril18.Student"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//注意此字符串必须是真实路径,就是带包名的类路径,包名.类名 </span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stuClass3 <span class="token operator">==</span> stuClass2<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//判断三种方式是否获取的是同一个</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">ClassNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// TODO Auto-generated catch block e.printStackTrace();</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> Java </category>
</categories>
<tags>
<tag> Java </tag>
</tags>
</entry>
<entry>
<title>反射机制</title>
<link href="post/fan-she-ji-zhi/"/>
<url>post/fan-she-ji-zhi/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><p>反射:框架设计的灵魂</p><h1 id="一、反射"><a href="#一、反射" class="headerlink" title="一、反射"></a>一、反射</h1><h2 id="1-1-框架"><a href="#1-1-框架" class="headerlink" title="1.1 框架"></a>1.1 框架</h2><p>半成品软件。可以在框架的基础上进行软件开发,简化编码。学习框架并不需要了解反射,但是要是想自己写一个框架,那么就要对反射机制有很深入的了解。</p><h2 id="1-2-反射"><a href="#1-2-反射" class="headerlink" title="1.2 反射"></a>1.2 反射</h2><p>反射机制:将类的各个组成部分封装为其他对象,这就是反射机制。</p><p>反射的好处:<br>1.可以在程序运行过程中,操作这些对象。<br>2.可以解耦,提高程序的可扩展性。</p><p>Java代码在计算机中经历的三个阶段<br>(1)Source源代码阶段:*.java被编译成*.class字节码文件。<br>(2)Class类对象阶段:*.class字节码文件被类加载器加载进内存,并将其封装成Class对象(用于在内存中描述字节码文件),Class对象将原字节码文件中的成员变量抽取出来封装成数组Field[],将原字节码文件中的构造函数抽取出来封装成数组Construction[],在将成员方法封装成Method[]。当然Class类内不止这三个,还封装了很多,我们常用的就这三个。<br>(3)RunTime运行时阶段:创建对象的过程new。<br><img src="/post/fan-she-ji-zhi/Java%E4%BB%A3%E7%A0%81%E7%9A%84%E4%B8%89%E4%B8%AA%E9%98%B6%E6%AE%B5.bmp" alt="Java代码的三个阶段"></p><h1 id="二、获取Class对象的方式"><a href="#二、获取Class对象的方式" class="headerlink" title="二、获取Class对象的方式"></a>二、获取Class对象的方式</h1><h2 id="2-1-获取Class对象的三种方式对应着java代码在计算机中的三个阶段"><a href="#2-1-获取Class对象的三种方式对应着java代码在计算机中的三个阶段" class="headerlink" title="2.1 获取Class对象的三种方式对应着java代码在计算机中的三个阶段"></a>2.1 获取Class对象的三种方式对应着java代码在计算机中的三个阶段</h2><p>(1)【Source源代码阶段】 Class.forName(“全类名”):将字节码文件加载进内存,返回Class对象<br> * 多用于配置文件,将类名定义在配置文件中。读取文件,加载类。<br>(2)【Class类对象阶段】 类名.class:通过类名的属性class获取<br> * 多用于参数的传递<br>(3)【Runtime运行时阶段】对象.getClass():getClass()方法是定义在Objec类中的方法<br> * 多用于对象的获取字节码的方式<br> 结论:同一个字节码文件(*.class)在一次程序运行过程中,只会被加载一次,无论通过哪一种方式获取的Class对象都是同一个。</p><h2 id="2-2-测试三种获取方法"><a href="#2-2-测试三种获取方法" class="headerlink" title="2.2 测试三种获取方法"></a>2.2 测试三种获取方法</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token annotation punctuation">@Test</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">reflect1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> ClassNotFoundException <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//方式一:Class.forName("全类名");</span> Class <span class="token class-name">cls1</span> <span class="token operator">=</span> Class<span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span><span class="token string">"com.test.domain.Person"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//Person自定义实体类</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"cls1 = "</span> <span class="token operator">+</span> cls1<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//方式二:类名.class</span> Class <span class="token class-name">cls2</span> <span class="token operator">=</span> Person<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"cls2 = "</span> <span class="token operator">+</span> cls2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//方式三:对象.getClass();</span> Person person <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Class <span class="token class-name">cls3</span> <span class="token operator">=</span> person<span class="token punctuation">.</span><span class="token function">getClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"cls3 = "</span> <span class="token operator">+</span> cls3<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// == 比较三个对象</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"cls1 == cls2 : "</span> <span class="token operator">+</span> <span class="token punctuation">(</span>cls1 <span class="token operator">==</span> cls2<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//true</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"cls1 == cls3 : "</span> <span class="token operator">+</span> <span class="token punctuation">(</span>cls1 <span class="token operator">==</span> cls3<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//true</span> <span class="token comment" spellcheck="true">//结论:同一个字节码文件(*.class)在一次程序运行过程中,只会被加载一次,无论通过哪一种方式获取的Class对象都是同一个。</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><img src="/post/fan-she-ji-zhi/clipboard.png" alt="clipboard"></p><h1 id="三、-Class对象功能"><a href="#三、-Class对象功能" class="headerlink" title="三、 Class对象功能"></a>三、 Class对象功能</h1><h2 id="3-1-获取功能"><a href="#3-1-获取功能" class="headerlink" title="3.1 获取功能"></a>3.1 获取功能</h2><p>这里只写了一些常用的,具体可以参看jdk的帮助文档。<br>(1)获取成员变量们</p><pre><code>Field[] getFields() :获取所有public修饰的成员变量Field getField(String name) : 获取指定名称的 public修饰的成员变量Field[] getDeclaredFields() 获取所有的成员变量,不考虑修饰符Field getDeclaredField(String name) </code></pre><p>(2)获取构造方法们</p><pre class="line-numbers language-java"><code class="language-java">Constructor<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">getConstructors</span><span class="token punctuation">(</span><span class="token punctuation">)</span> Constructor<span class="token operator"><</span>T<span class="token operator">></span> <span class="token function">getConstructor</span><span class="token punctuation">(</span>类<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> parameterTypes<span class="token punctuation">)</span> Constructor<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">getDeclaredConstructors</span><span class="token punctuation">(</span><span class="token punctuation">)</span> Constructor<span class="token operator"><</span>T<span class="token operator">></span> <span class="token function">getDeclaredConstructor</span><span class="token punctuation">(</span>类<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> parameterTypes<span class="token punctuation">)</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p>(3)获取成员方法们:</p><pre class="line-numbers language-java"><code class="language-java">Method<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">getMethods</span><span class="token punctuation">(</span><span class="token punctuation">)</span> Method <span class="token function">getMethod</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span> 类<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> parameterTypes<span class="token punctuation">)</span> Method<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">getDeclaredMethods</span><span class="token punctuation">(</span><span class="token punctuation">)</span> Method <span class="token function">getDeclaredMethod</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span> 类<span class="token operator"><</span><span class="token operator">?</span><span class="token operator">></span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> parameterTypes<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>(4)获取全类名 </p><pre><code>String getName() </code></pre><h2 id="3-2-Field:成员变量"><a href="#3-2-Field:成员变量" class="headerlink" title="3.2 Field:成员变量"></a>3.2 Field:成员变量</h2><p>(1)设置值 void set(Object obj, Object value)<br>(2)获取值 get(Object obj)<br>(3)忽略访问权限修饰符的安全检查 setAccessible(true):暴力反射</p><h3 id="3-2-1-测试的实体类"><a href="#3-2-1-测试的实体类" class="headerlink" title="3.2.1 测试的实体类"></a>3.2.1 测试的实体类</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>test<span class="token punctuation">.</span>domain<span class="token punctuation">;</span> <span class="token keyword">import</span> lombok<span class="token punctuation">.</span>Getter<span class="token punctuation">;</span><span class="token keyword">import</span> lombok<span class="token punctuation">.</span>Setter<span class="token punctuation">;</span><span class="token keyword">import</span> lombok<span class="token punctuation">.</span>ToString<span class="token punctuation">;</span> <span class="token annotation punctuation">@Setter</span><span class="token annotation punctuation">@Getter</span><span class="token annotation punctuation">@ToString</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> String a<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//最大范围public</span> <span class="token keyword">protected</span> String b<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//受保护类型</span> String c<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//默认的访问权限</span> <span class="token keyword">private</span> String d<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//私有类型</span> <span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="3-2-2-测试getFields和getField-String-name-方法"><a href="#3-2-2-测试getFields和getField-String-name-方法" class="headerlink" title="3.2.2 测试getFields和getField(String name)方法"></a>3.2.2 测试getFields和getField(String name)方法</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">/** * 1. 获取成员变量们 * * Field[] getFields() * * Field getField(String name) * @throws Exception */</span><span class="token annotation punctuation">@Test</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">reflect2</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> Exception <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//0、获取Person的Class对象</span> Class <span class="token class-name">personClass</span> <span class="token operator">=</span> Person<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//1、Field[] getFields()获取所有public修饰的成员变量</span> Field<span class="token punctuation">[</span><span class="token punctuation">]</span> fields <span class="token operator">=</span> personClass<span class="token punctuation">.</span><span class="token function">getFields</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span>Field field <span class="token operator">:</span> fields<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>field<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"============================="</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//2.Field getField(String name)</span> Field a <span class="token operator">=</span> personClass<span class="token punctuation">.</span><span class="token function">getField</span><span class="token punctuation">(</span><span class="token string">"a"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//获取成员变量a 的值 [也只能获取公有的,获取私有的或者不存在的字符会抛出异常]</span> Person p <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Object value <span class="token operator">=</span> a<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"value = "</span> <span class="token operator">+</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//设置属性a的值</span> a<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>p<span class="token punctuation">,</span><span class="token string">"张三"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><img src="/post/fan-she-ji-zhi/clipboard1.png"></p><h3 id="3-2-3-测试getDeclaredFields和getDeclaredField-String-name-方法"><a href="#3-2-3-测试getDeclaredFields和getDeclaredField-String-name-方法" class="headerlink" title="3.2.3 测试getDeclaredFields和getDeclaredField(String name)方法"></a>3.2.3 测试getDeclaredFields和getDeclaredField(String name)方法</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">/** * Field[] getDeclaredFields() * Field getDeclaredField(String name) * @throws Exception */</span><span class="token annotation punctuation">@Test</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">reflect3</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> Exception <span class="token punctuation">{</span> Class <span class="token class-name">personClass</span> <span class="token operator">=</span> Person<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//Field[] getDeclaredFields():获取所有的成员变量,不考虑修饰符</span> Field<span class="token punctuation">[</span><span class="token punctuation">]</span> declaredFields <span class="token operator">=</span> personClass<span class="token punctuation">.</span><span class="token function">getDeclaredFields</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span>Field filed <span class="token operator">:</span> declaredFields<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>filed<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"==================================="</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//Field getDeclaredField(String name)</span> Field d <span class="token operator">=</span> personClass<span class="token punctuation">.</span><span class="token function">getDeclaredField</span><span class="token punctuation">(</span><span class="token string">"d"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//private String d;</span> Person p <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//Object value1 = d.get(p); //会抛出异常</span> <span class="token comment" spellcheck="true">//System.out.println("value1 = " + value1); //对于私有变量虽然能会获取到,但不能直接set和get</span> <span class="token comment" spellcheck="true">//忽略访问权限修饰符的安全检查</span> d<span class="token punctuation">.</span><span class="token function">setAccessible</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//暴力反射</span> Object value2 <span class="token operator">=</span> d<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"value2 = "</span> <span class="token operator">+</span> value2<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>正常运行结果<br> <img src="/post/fan-she-ji-zhi/clipboard2.png"><br>没有忽略访问修饰符直接访问抛出的异常</p><p><img src="/post/fan-she-ji-zhi/clipboard3.png"></p><h2 id="3-3-Constructor-构造方法"><a href="#3-3-Constructor-构造方法" class="headerlink" title="3.3 Constructor:构造方法"></a>3.3 Constructor:构造方法</h2><p>创建对象:T newInstance(Object… initargs)<br>注意:如果使用空参数构造方法创建对象,操作可以简化:Class对象的newInstance方法</p><h3 id="3-3-1-修改测试的实体类"><a href="#3-3-1-修改测试的实体类" class="headerlink" title="3.3.1 修改测试的实体类"></a>3.3.1 修改测试的实体类</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>test<span class="token punctuation">.</span>domain<span class="token punctuation">;</span><span class="token keyword">import</span> lombok<span class="token punctuation">.</span>Getter<span class="token punctuation">;</span><span class="token keyword">import</span> lombok<span class="token punctuation">.</span>Setter<span class="token punctuation">;</span><span class="token keyword">import</span> lombok<span class="token punctuation">.</span>ToString<span class="token punctuation">;</span><span class="token annotation punctuation">@Setter</span><span class="token annotation punctuation">@Getter</span><span class="token annotation punctuation">@ToString</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> String name<span class="token punctuation">;</span> <span class="token keyword">private</span> Integer age<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//无参构造函数</span> <span class="token keyword">public</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//单个参数的构造函数,且为私有构造方法</span> <span class="token keyword">private</span> <span class="token function">Person</span><span class="token punctuation">(</span>String name<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//有参构造函数</span> <span class="token keyword">public</span> <span class="token function">Person</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span> Integer age<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="3-3-2-测试方法(注释很重要)"><a href="#3-3-2-测试方法(注释很重要)" class="headerlink" title="3.3.2 测试方法(注释很重要)"></a>3.3.2 测试方法(注释很重要)</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">/** * 2. 获取构造方法们 * Constructor<?>[] getConstructors() * Constructor<T> getConstructor(类<?>... parameterTypes) */</span><span class="token annotation punctuation">@Test</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">reflect4</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> Exception <span class="token punctuation">{</span> Class <span class="token class-name">personClass</span> <span class="token operator">=</span> Person<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//Constructor<?>[] getConstructors()</span> Constructor<span class="token punctuation">[</span><span class="token punctuation">]</span> constructors <span class="token operator">=</span> personClass<span class="token punctuation">.</span><span class="token function">getConstructors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span>Constructor constructor <span class="token operator">:</span> constructors<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//Constructor 对象reflect包下的 import java.lang.reflect.Constructor;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>constructor<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"=========================================="</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//获取无参构造函数 注意:Person类中必须要有无参的构造函数,不然抛出异常</span> Constructor constructor1 <span class="token operator">=</span> personClass<span class="token punctuation">.</span><span class="token function">getConstructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"constructor1 = "</span> <span class="token operator">+</span> constructor1<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//获取到构造函数后可以用于创建对象</span> Object person1 <span class="token operator">=</span> constructor1<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//Constructor类内提供了初始化方法newInstance();方法</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"person1 = "</span> <span class="token operator">+</span> person1<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"=========================================="</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//获取有参的构造函数 //public Person(String name, Integer age) 参数类型顺序要与构造函数内一致,且参数类型为字节码类型</span> Constructor constructor2 <span class="token operator">=</span> personClass<span class="token punctuation">.</span><span class="token function">getConstructor</span><span class="token punctuation">(</span>String<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>Integer<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"constructor2 = "</span> <span class="token operator">+</span> constructor2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//创建对象</span> Object person2 <span class="token operator">=</span> constructor2<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token string">"张三"</span><span class="token punctuation">,</span> <span class="token number">23</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//获取的是有参的构造方法,就必须要给参数</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>person2<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"========================================="</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//对于一般的无参构造函数,我们都不会先获取无参构造器之后在进行初始化。而是直接调用Class类内的newInstance()方法</span> Object person3 <span class="token operator">=</span> personClass<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"person3 = "</span> <span class="token operator">+</span> person3<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//我们之前使用的 Class.forName("").newInstance; 其本质上就是调用了类内的无参构造函数来完成实例化的</span> <span class="token comment" spellcheck="true">//故可以得出结论 我们以后在使用 Class.forName("").newInstance; 反射创建对象时,一定要保证类内有无参构造函数</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><img src="/post/fan-she-ji-zhi/clipboard4.png"></p><h3 id="3-3-3-对于getDeclaredConstructor方法和getDeclaredConstructors方法"><a href="#3-3-3-对于getDeclaredConstructor方法和getDeclaredConstructors方法" class="headerlink" title="3.3.3 对于getDeclaredConstructor方法和getDeclaredConstructors方法"></a>3.3.3 对于getDeclaredConstructor方法和getDeclaredConstructors方法</h3><p>对于多出个Declared关键词的两个方法,与不带这个词的两个方法的对比。与之前3.2叙述的一样,getDeclaredConstructor方法可以获取到任何访问权限的构造器,而getConstructor方法只能获取public修饰的构造器。具体不再测试。此外在构造器的对象内也有setAccessible(true);方法,并设置成true就可以操作了。<br>关于为什么要使用private访问权限的构造器,使用这个构造器不就不能外部访问了嘛,不也就无法进行实例化对象了吗?无法在类的外部实例化对象正是私有构造器的意义所在,在单例模式下经常使用,整个项目只有一个对象,外部无法实例化对象,可以在类内的进行实例化并通过静态方法返回(由于实例化的对象是静态的,故只有一个对象,也就是单例的)。网上说这就是单例模式中的饿汉模式,不管是否调用,都创建一个对象。</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">class</span> <span class="token class-name">SingletonDemo</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//私有化构造方法</span> <span class="token keyword">private</span> <span class="token function">SingletonDemo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//创建一个对象 类内实例化(静态的对象)</span> <span class="token keyword">private</span> <span class="token keyword">static</span> SingletonDemo singleton <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SingletonDemo</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//提供public方法供外部访问,返回这个创建的对象</span> <span class="token keyword">public</span> <span class="token keyword">static</span> SingletonDemo <span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> singleton<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Singleton</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> SingletonDemo s1 <span class="token operator">=</span> SingletonDemo<span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//输出对象的地址,如果有地址存在,则说明对象创建成功并获取到</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s1<span class="token punctuation">)</span><span class="token punctuation">;</span> SingletonDemo s2 <span class="token operator">=</span> SingletonDemo<span class="token punctuation">.</span><span class="token function">getInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//如果结果为true,则说明是同一个对象</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s1<span class="token operator">==</span>s2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//输出结果为true</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="3-4-Method:方法对象"><a href="#3-4-Method:方法对象" class="headerlink" title="3.4 Method:方法对象"></a>3.4 Method:方法对象</h2><p>执行方法:</p><pre><code>Object invoke(Object obj, Object... args) </code></pre><p>获取方法名称:</p><pre><code>String getName();</code></pre><h3 id="3-4-1-修改测试的实体类"><a href="#3-4-1-修改测试的实体类" class="headerlink" title="3.4.1 修改测试的实体类"></a>3.4.1 修改测试的实体类</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>test<span class="token punctuation">.</span>domain<span class="token punctuation">;</span> <span class="token keyword">import</span> lombok<span class="token punctuation">.</span>Getter<span class="token punctuation">;</span><span class="token keyword">import</span> lombok<span class="token punctuation">.</span>Setter<span class="token punctuation">;</span><span class="token keyword">import</span> lombok<span class="token punctuation">.</span>ToString<span class="token punctuation">;</span> <span class="token annotation punctuation">@Setter</span><span class="token annotation punctuation">@Getter</span><span class="token annotation punctuation">@ToString</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> String name<span class="token punctuation">;</span> <span class="token keyword">private</span> Integer age<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//无参构造函数</span> <span class="token keyword">public</span> <span class="token function">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//有参构造函数</span> <span class="token keyword">public</span> <span class="token function">Person</span><span class="token punctuation">(</span>String name<span class="token punctuation">,</span> Integer age<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//无参方法</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">eat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"eat..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">//重载有参方法</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">eat</span><span class="token punctuation">(</span>String food<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"eat..."</span><span class="token operator">+</span>food<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="3-4-2-测试invoke方法"><a href="#3-4-2-测试invoke方法" class="headerlink" title="3.4.2 测试invoke方法"></a>3.4.2 测试invoke方法</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token comment" spellcheck="true">/** * 3. 获取成员方法们: * * Method[] getMethods() * * Method getMethod(String name, 类<?>... parameterTypes) */</span><span class="token annotation punctuation">@Test</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">reflect5</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> Exception <span class="token punctuation">{</span> Class <span class="token class-name">personClass</span> <span class="token operator">=</span> Person<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//获取指定名称的方法 </span> Method eat_method1 <span class="token operator">=</span> personClass<span class="token punctuation">.</span><span class="token function">getMethod</span><span class="token punctuation">(</span><span class="token string">"eat"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//执行方法</span> Person person <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> Object rtValue <span class="token operator">=</span> eat_method1<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>person<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//如果方法有返回值类型可以获取到,没有就为null</span> <span class="token comment" spellcheck="true">//输出返回值 eat方法没有返回值,故输出null</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"rtValue = "</span> <span class="token operator">+</span> rtValue<span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"--------------------------------------------"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//获取有参的构造函数 有两个参数 第一个方法名 第二个参数列表 ,不同的参数是不同的方法(重载)</span> Method eat_method2 <span class="token operator">=</span> personClass<span class="token punctuation">.</span><span class="token function">getMethod</span><span class="token punctuation">(</span><span class="token string">"eat"</span><span class="token punctuation">,</span> String<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//执行方法</span> eat_method2<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>person<span class="token punctuation">,</span><span class="token string">"饭"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"============================================"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//获取方法列表</span> Method<span class="token punctuation">[</span><span class="token punctuation">]</span> methods <span class="token operator">=</span> personClass<span class="token punctuation">.</span><span class="token function">getMethods</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span>Method method <span class="token operator">:</span> methods<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//注意:获取到的方法名称不仅仅是我们在Person类内看到的方法</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>method<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//继承下来的方法也会被获取到(当然前提是public修饰的)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="3-4-3-测试getName方法"><a href="#3-4-3-测试getName方法" class="headerlink" title="3.4.3 测试getName方法"></a>3.4.3 测试getName方法</h3><p>getName()方法获取的方法名仅仅就是方法名(不带全类名),且不带有参数列表。</p><pre class="line-numbers language-java"><code class="language-java"><span class="token annotation punctuation">@Test</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">reflect6</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> NoSuchMethodException <span class="token punctuation">{</span> Class <span class="token class-name">personClass</span> <span class="token operator">=</span> Person<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">;</span> Method<span class="token punctuation">[</span><span class="token punctuation">]</span> methods <span class="token operator">=</span> personClass<span class="token punctuation">.</span><span class="token function">getMethods</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span>Method method <span class="token operator">:</span> methods<span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>method<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//获取方法名</span> String name <span class="token operator">=</span> method<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><img src="/post/fan-she-ji-zhi/clipboard5.png"></p><h3 id="3-4-4-关于获取成员方法们的另外两个方法"><a href="#3-4-4-关于获取成员方法们的另外两个方法" class="headerlink" title="3.4.4 关于获取成员方法们的另外两个方法"></a>3.4.4 关于获取成员方法们的另外两个方法</h3><pre><code>Method[] getDeclaredMethods() Method getDeclaredMethod(String name, 类<?>... parameterTypes)method.setAccessible(true); //暴力反射</code></pre><p>同之前的叙述一样,带有Declared关键字的方法这两个方法,可以获取到任意修饰符的方法。同样的提供了setAccessible(true);方法进行暴力反射。</p><p>综上说述:对于反射机制来说,在反射面前没有公有私有,都可以通过暴力反射解决。</p><h2 id="3-5-获取类名"><a href="#3-5-获取类名" class="headerlink" title="3.5 获取类名"></a>3.5 获取类名</h2><p>getClass()方法是Object类的方法,需要注意一点获取的类名是全类名(带有路径)</p><pre><code>@Testpublic void reflect7(){ Class personClass = Person.class; String className = personClass.getName(); System.out.println(className);}</code></pre><p><img src="/post/fan-she-ji-zhi/clipboard6.png"></p><h1 id="四、案例"><a href="#四、案例" class="headerlink" title="四、案例"></a>四、案例</h1><h2 id="4-1-案例分析"><a href="#4-1-案例分析" class="headerlink" title="4.1 案例分析"></a>4.1 案例分析</h2><h3 id="4-1-1-需求"><a href="#4-1-1-需求" class="headerlink" title="4.1.1 需求"></a>4.1.1 需求</h3><p>写一个”框架”,不能改变该类的任何代码的前提下,可以帮我们创建任意类的对象,并且执行其中任意方法。</p><h3 id="4-1-2-实现"><a href="#4-1-2-实现" class="headerlink" title="4.1.2 实现"></a>4.1.2 实现</h3><p>(1)配置文件<br>(2)反射</p><h3 id="4-1-3-步骤"><a href="#4-1-3-步骤" class="headerlink" title="4.1.3 步骤"></a>4.1.3 步骤</h3><p>(1)将需要创建的对象的全类名和需要执行的方法定义在配置文件中<br>(2)在程序中加载读取配置文件<br>(3)使用反射技术来加载类文件进内存<br>(4)创建对象<br>(5)执行方法</p><h2 id="4-2-代码实现"><a href="#4-2-代码实现" class="headerlink" title="4.2 代码实现"></a>4.2 代码实现</h2><h3 id="4-2-1-需要的实体类"><a href="#4-2-1-需要的实体类" class="headerlink" title="4.2.1 需要的实体类"></a>4.2.1 需要的实体类</h3><p>(1)Person类</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>test<span class="token punctuation">.</span>domain<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//无参方法</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">eat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"eat..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>(2)Student类</p><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>test<span class="token punctuation">.</span>domain<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">study</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"student's job is to learn..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="4-2-2-编写配置文件"><a href="#4-2-2-编写配置文件" class="headerlink" title="4.2.2 编写配置文件"></a>4.2.2 编写配置文件</h3><p>以后我们在配置文件内看见全类名,就应该想到可能使用到了反射</p><pre><code>className = com.test.domain.PersonmethodName = eat</code></pre><h3 id="4-2-3-编写测试方法(模拟框架)"><a href="#4-2-3-编写测试方法(模拟框架)" class="headerlink" title="4.2.3 编写测试方法(模拟框架)"></a>4.2.3 编写测试方法(模拟框架)</h3><pre class="line-numbers language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>test<span class="token punctuation">.</span>junit<span class="token punctuation">;</span> <span class="token keyword">import</span> java<span class="token punctuation">.</span>io<span class="token punctuation">.</span>InputStream<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span>reflect<span class="token punctuation">.</span>Method<span class="token punctuation">;</span><span class="token keyword">import</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>Properties<span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ReflectTest</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> Exception <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/** * 前提:不能改变该类的任何代码。可以创建任意类的对象,可以执行任意方法 * 即:拒绝硬编码 */</span> <span class="token comment" spellcheck="true">//1.加载配置文件</span> <span class="token comment" spellcheck="true">//1.1创建Properties对象</span> Properties pro <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//1.2加载配置文件,转换为一个集合</span> <span class="token comment" spellcheck="true">//1.2.1获取class目录下的配置文件 使用类加载器</span> ClassLoader classLoader <span class="token operator">=</span> ReflectTest<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">.</span><span class="token function">getClassLoader</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> InputStream is <span class="token operator">=</span> classLoader<span class="token punctuation">.</span><span class="token function">getResourceAsStream</span><span class="token punctuation">(</span><span class="token string">"pro.properties"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> pro<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>is<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//2.获取配置文件中定义的数据</span> String className <span class="token operator">=</span> pro<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"className"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String methodName <span class="token operator">=</span> pro<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"methodName"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//3.加载该类进内存</span> Class <span class="token class-name">cls</span> <span class="token operator">=</span> Class<span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span>className<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//4.创建对象</span> Object obj <span class="token operator">=</span> cls<span class="token punctuation">.</span><span class="token function">newInstance</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//5.获取方法对象</span> Method method <span class="token operator">=</span> cls<span class="token punctuation">.</span><span class="token function">getMethod</span><span class="token punctuation">(</span>methodName<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//6.执行方法</span> method<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="4-2-4-运行结果"><a href="#4-2-4-运行结果" class="headerlink" title="4.2.4 运行结果"></a>4.2.4 运行结果</h3><p><img src="/post/fan-she-ji-zhi/clipboard7.png"></p><h3 id="4-2-5-修改配置文件,再次运行"><a href="#4-2-5-修改配置文件,再次运行" class="headerlink" title="4.2.5 修改配置文件,再次运行"></a>4.2.5 修改配置文件,再次运行</h3><p>将配置文件内的信息修改为Student类及类内的方法</p><pre class="line-numbers language-java"><code class="language-java">className <span class="token operator">=</span> com<span class="token punctuation">.</span>test<span class="token punctuation">.</span>domain<span class="token punctuation">.</span>StudentmethodName <span class="token operator">=</span> study<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p><img src="/post/fan-she-ji-zhi/clipboard8.png"></p><h2 id="4-3-好处"><a href="#4-3-好处" class="headerlink" title="4.3 好处"></a>4.3 好处</h2><p>我们这样做有什么好处呢,对于框架来说,是人家封装好的,我们拿来直接用就可以了,而不能去修改框架内的代码。但如果我们使用传统的new形式来实例化,那么当类名更改时我们就要修改Java代码,这是很繁琐的。修改Java代码以后我们还要进行测试,重新编译、发布等等一系列的操作。而如果我们仅仅只是修改配置文件,就来的简单的多,配置文件就是一个实实在在的物理文件。<br>此外使用反射还能达到解耦的效果。假设我们使用的是new这种形式进行对象的实例化,此时如果在项目的某一个小模块中我们的一个实例类丢失了,那么在编译期间就会报错,以导致整个项目无法启动。而对于反射创建对象Class.forName(“全类名”);这种形式,我们在编译期需要的仅仅只是一个字符串(全类名),在编译期不会报错,这样其他的模块就可以正常的运行,而不会因为一个模块的问题导致整个项目崩溃。这就是Spring框架中IOC控制反转的本质。</p>]]></content>
<tags>
<tag> Java </tag>
<tag> 反射机制 </tag>
</tags>
</entry>
<entry>
<title>Spring中的常用注解</title>
<link href="post/spring-zhong-de-chang-yong-zhu-jie/"/>
<url>post/spring-zhong-de-chang-yong-zhu-jie/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><h2 id="1-第一步导包"><a href="#1-第一步导包" class="headerlink" title="1. 第一步导包"></a>1. 第一步导包</h2><p><img src="https://img-blog.csdnimg.cn/20191106115900164.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTE1MTk2MA==,size_16,color_FFFFFF,t_70"><br>首先需要导入这四个包以及日志文件(slf4j-log4j12-1.7.5.jar)</p><h2 id="2-配置xml文件"><a href="#2-配置xml文件" class="headerlink" title="2.配置xml文件"></a>2.配置xml文件</h2><pre class="line-numbers language-java"><code class="language-java"><span class="token operator"><</span><span class="token operator">--</span>配置文件信息<span class="token operator">--</span><span class="token operator">></span><span class="token operator"><</span>beans xmlns<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/beans"</span> xmlns<span class="token operator">:</span>xsi<span class="token operator">=</span><span class="token string">"http://www.w3.org/2001/XMLSchema-instance"</span> xmlns<span class="token operator">:</span>context<span class="token operator">=</span><span class="token string">"http://www.springframework.org/schema/context"</span> xsi<span class="token operator">:</span>schemaLocation<span class="token operator">=</span>"http<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>org<span class="token operator">/</span>schema<span class="token operator">/</span>beans http<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>org<span class="token operator">/</span>schema<span class="token operator">/</span>beans<span class="token operator">/</span>spring<span class="token operator">-</span>beans<span class="token punctuation">.</span>xsdhttp<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>org<span class="token operator">/</span>schema<span class="token operator">/</span>context http<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>org<span class="token operator">/</span>schema<span class="token operator">/</span>context<span class="token operator">/</span>spring<span class="token operator">-</span>context<span class="token punctuation">.</span>xsd"<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 开启注解功能 <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span>context<span class="token operator">:</span>annotation<span class="token operator">-</span>config<span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>context<span class="token operator">:</span>annotation<span class="token operator">-</span>config<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> 开启注解扫描功能,该包下所有的<span class="token annotation punctuation">@Component</span> <span class="token operator"><</span>context<span class="token operator">:</span>component<span class="token operator">-</span>scan base<span class="token operator">-</span><span class="token keyword">package</span><span class="token operator">=</span><span class="token string">"com.hisoft"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>context<span class="token operator">:</span>component<span class="token operator">-</span>scan<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span><span class="token operator"><</span>context<span class="token operator">:</span>componet<span class="token operator">-</span>scan<span class="token operator">></span> 具有 <span class="token operator"><</span>context<span class="token operator">:</span>annotation<span class="token operator">-</span>config<span class="token operator">></span> 作用 ! 所以第一步开启注解功能也可以省略<span class="token operator">--</span><span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>配置本地提示:</strong><br><img src="https://img-blog.csdnimg.cn/20191106120248470.png"></p><h2 id="3-常用注解"><a href="#3-常用注解" class="headerlink" title="3.常用注解"></a>3.常用注解</h2><ul><li>@Component 这种注解可以在dao层或者service层等都可以使用,没有限制,有三种使用方式<pre class="line-numbers language-java"><code class="language-java"> <span class="token annotation punctuation">@Component</span> <span class="token comment" spellcheck="true">//默认id为类名首字母变小写</span> <span class="token annotation punctuation">@Component</span><span class="token punctuation">(</span>value<span class="token operator">=</span><span class="token string">"usermapper"</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">//设置id名称</span> <span class="token annotation punctuation">@Component</span><span class="token punctuation">(</span><span class="token string">"usermapper"</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">//设置id名称</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>实际开发中,使用的是@Component三个衍生注解(“子注解”)<br>子注解的作用:有分层的意义(分层注解)。<br><img src="https://img-blog.csdnimg.cn/2019110612095536.png"></li></ul><ul><li>简单数据类型依赖注入</li></ul><pre class="line-numbers language-java"><code class="language-java"> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"张三"</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">//直接给属性注入值</span> String name<span class="token punctuation">;</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"123"</span><span class="token punctuation">)</span> String password<span class="token punctuation">;</span> 其实这种方式是等价于 String name <span class="token operator">=</span> <span class="token string">"张三"</span>;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>复杂类型数据依赖注入</li></ul><p>** 第一种: 使用@Value 结合SpEL #{} —- spring3.0 后用 Service层注入dao层**</p><pre class="line-numbers language-java"><code class="language-java"> dao层 <span class="token annotation punctuation">@Component</span><span class="token punctuation">(</span><span class="token string">"usermapper"</span><span class="token punctuation">)</span> service层 <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"#{usermapper}"</span><span class="token punctuation">)</span> UserMapperImpl usermapper<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>** 第二种:使用@Autowired 或者结合 @Qualifier**</p><p>单独使用@Autowired 按照类型注入<br><img src="https://img-blog.csdnimg.cn/20191106121703992.png"></p><p>使用@Autowired + @ Qualifier 按照名称注入<br><img src="https://img-blog.csdnimg.cn/2019110612171283.png"></p><p>** 第三种: JSR-250标准(jdk) 提供@Resource**<br><img src="https://img-blog.csdnimg.cn/20191106121909462.png"></p><p>** 第四种: JSR-330标准(jdk) 提供 @Inject (麻烦点)不推荐(了解)**<br>需要先导入 javax.inject 的 jar (废掉了)</p><p>按照类型注入<br><img src="https://img-blog.csdnimg.cn/20191106121922326.png"></p><p>按照名称注入<br><img src="https://img-blog.csdnimg.cn/20191106121926986.png"></p><h2 id="汇总"><a href="#汇总" class="headerlink" title="汇总"></a>汇总</h2><p><img src="https://img-blog.csdnimg.cn/20191106121950983.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTE1MTk2MA==,size_16,color_FFFFFF,t_70"></p>]]></content>
<categories>
<category> Spring </category>
</categories>
<tags>
<tag> Java </tag>
<tag> Spring </tag>
</tags>
</entry>
<entry>
<title>hexo 添加admin后台</title>
<link href="post/23abcb74/"/>
<url>post/23abcb74/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="\assets\css\APlayer.min.css"><script src="\assets\js\APlayer.min.js" class="aplayer-secondary-script-marker"></script><script class="meting-secondary-script-marker" src="\assets\js\Meting.min.js"></script><h1 id="为hexo添加hexo-admin组件"><a href="#为hexo添加hexo-admin组件" class="headerlink" title="为hexo添加hexo-admin组件"></a>为hexo添加hexo-admin组件</h1><h2 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h2><p> 已安装好hexo,选择好自己的主题(我选择的主题是metary),并部署到GitHub等静态托管服务器上<br>已安装好hexo,选择好自己的主题(我选择的主题是metary),并部署到GitHub等静态托管服务器上</p><h2 id="插件介绍"><a href="#插件介绍" class="headerlink" title="插件介绍"></a>插件介绍</h2><p> hexo-admin 是一个Hexo博客引擎的管理用户界面插件。这个插件最初是作为本地编辑器设计的,在本地运行hexo使用hexo-admin编写文章,然后通过hexo g或hexo d(hexo g是本地渲染,hexo d是将渲染的静态页面发布到GitHub)将生成的静态页面发布到GitHub等静态服务器。如果你使用的是非静态托管服务器,比如自己买的主机搭建的hexo,那么一定要设置hexo-admin 的密码,否则谁都可以编辑你的文章。</p><h2 id="插件安装"><a href="#插件安装" class="headerlink" title="插件安装"></a>插件安装</h2><ol><li>首先进入hexo创建的博客项目的根目录下,执行<br><code> npm install --save hexo-admin</code><br>mac可能需要root权限,前面加个sudo 就可以了。如果报错缺少组件,则缺少什么安装什么,npm install 加缺少的组件。</li><li>运行下列命令启动hexo-admin:<br>hexo server -d<br>打开 <a href="http://localhost:4000/admin/">http://localhost:4000/admin/</a> 就可以访问到hexo-admin管理页面了。</li></ol><h2 id="密码保护"><a href="#密码保护" class="headerlink" title="密码保护"></a>密码保护</h2><p> 打开setting,点击Setup authentification here输入用户名,密码,密钥,下面会自动生成配置文件,复制加在hexo根目录下的_config.yml中: </p><pre><code>admin: username: myfavoritename password_hash: be121740bf988b2225a313fa1f107ca1 secret: a secret something</code></pre><p> 重启hexo,就可以看到登录页面了</p><h2 id="发布文章"><a href="#发布文章" class="headerlink" title="发布文章"></a>发布文章</h2><p> 进入后台之后点击Deploy,里面的Deploy按钮是用来执行发布脚本的,所以我们先在博客根目录下新建个目录admin_script,然后在目录中新建一个脚本hexo-d.sh,里面写下下面代码然后保存<br><code>hexo g && hexo d</code><br>然后给hexo-g.sh加入可执行权限<br><code>chmod +x hexo-d.sh</code><br>然后在_config.yml中的admin下添加</p><pre><code>admin: username: myfavoritename password_hash: be121740bf988b2225a313fa1f107ca1 secret: a secret something deployCommand: ./admin_script/hexo-d.sh</code></pre><p>设置发布执行的脚本,点击Deploy就会执行这个命令并提交到GitHub上。</p>]]></content>
<categories>
<category> Hexo </category>
</categories>
<tags>
<tag> Hexo </tag>
</tags>
</entry>
</search>