-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2421 lines (1188 loc) · 140 KB
/
index.html
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />
<meta name="keywords" content="Hexo, NexT" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />
<meta property="og:type" content="website">
<meta property="og:title" content="Hello World">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="Hello World">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Hello World">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Pisces',
sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
fancybox: true,
motion: true,
duoshuo: {
userId: '0',
author: '博主'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<link rel="canonical" href="http://yoursite.com/"/>
<title>Hello World</title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container sidebar-position-left
page-home
">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">Hello World</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle">Hello Young</p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />
首页
</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section">
<i class="menu-item-icon fa fa-fw fa-th"></i> <br />
分类
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
归档
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
标签
</a>
</li>
<li class="menu-item menu-item-about">
<a href="/about/" rel="section">
<i class="menu-item-icon fa fa-fw fa-user"></i> <br />
关于
</a>
</li>
<li class="menu-item menu-item-search">
<a href="javascript:;" class="popup-trigger">
<i class="menu-item-icon fa fa-search fa-fw"></i> <br />
搜索
</a>
</li>
</ul>
<div class="site-search">
<div class="popup search-popup local-search-popup">
<div class="local-search-header clearfix">
<span class="search-icon">
<i class="fa fa-search"></i>
</span>
<span class="popup-btn-close">
<i class="fa fa-times-circle"></i>
</span>
<div class="local-search-input-wrapper">
<input autocomplete="off"
placeholder="搜索..." spellcheck="false"
type="text" id="local-search-input">
</div>
</div>
<div id="local-search-result"></div>
</div>
</div>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/03/28/java-tutorial-learning_interface_summary/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Hello Young">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Hello World">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/03/28/java-tutorial-learning_interface_summary/" itemprop="url">Java 学习指南_学习Java:接口总结</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-28T15:50:59+08:00">
2018-03-28
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/java/" itemprop="url" rel="index">
<span itemprop="name">java</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="接口总结"><a href="#接口总结" class="headerlink" title="接口总结"></a>接口总结</h1><p>接口定义可以包含方法签名,默认方法,静态方法以及常量定义。只有默认方法和静态方法可以有实现。</p>
<p>类如果实现了接口必须实现接口里定义的所有抽象方法。</p>
<p>使用有类型名字的地方都可以使用接口名。</p>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/03/27/java-tutorial-learning_interface_default_methods/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Hello Young">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Hello World">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/03/27/java-tutorial-learning_interface_default_methods/" itemprop="url">Java 学习指南_学习Java:默认方法</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-27T16:50:59+08:00">
2018-03-27
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/java/" itemprop="url" rel="index">
<span itemprop="name">java</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="默认方法Default-Methods"><a href="#默认方法Default-Methods" class="headerlink" title="默认方法Default Methods"></a>默认方法Default Methods</h1><p>接口章节 <a href="https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html" target="_blank" rel="external">Interfaces</a> 描述了一个例子,关于电脑控制汽车生产厂商发布工业标准接口,调用其中的方法可以对他们的汽车进行操控。如果电脑控制汽车厂商为汽车增加了新的功能,例如飞行的功能,会怎么样呢?这些厂商需要明确新的方法是其他的公司(例如电子制导系统公司)调整他们的软件来控制汽车飞行。这些厂商要在哪里声明这些飞行相关的方法呢?如果他们吧方法添加在原有的接口中,那么已经实现了这些接口的开发者就必须重写他们的实现。如果作为静态方法添加,开发者会将其作为工具方法,而不是必要的和兴方法来对待。</p>
<p>默认方法可以让你为库里的接口添加新的功能,并且在重写旧版本接口的代码时确保两部分功能兼容。</p>
<p>考虑下面的接口, <a href="https://docs.oracle.com/javase/tutorial/java/IandI/examples/TimeClient.java" target="_blank" rel="external"><code>TimeClient</code></a>, 在之前的联系中出现的 <a href="https://docs.oracle.com/javase/tutorial/java/IandI/QandE/interfaces-answers.html" target="_blank" rel="external">Answers to Questions and Exercises: Interfaces</a>:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> java.time.*; </div><div class="line"> </div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">TimeClient</span> </span>{</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">setTime</span><span class="params">(<span class="keyword">int</span> hour, <span class="keyword">int</span> minute, <span class="keyword">int</span> second)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">setDate</span><span class="params">(<span class="keyword">int</span> day, <span class="keyword">int</span> month, <span class="keyword">int</span> year)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">setDateAndTime</span><span class="params">(<span class="keyword">int</span> day, <span class="keyword">int</span> month, <span class="keyword">int</span> year,</span></span></div><div class="line"> <span class="keyword">int</span> hour, <span class="keyword">int</span> minute, <span class="keyword">int</span> second);</div><div class="line"> <span class="function">LocalDateTime <span class="title">getLocalDateTime</span><span class="params">()</span></span>;</div><div class="line">}</div></pre></td></tr></table></figure>
<p>下面的类, <a href="https://docs.oracle.com/javase/tutorial/java/IandI/examples/defaultmethods/SimpleTimeClient.java" target="_blank" rel="external"><code>SimpleTimeClient</code></a>, 实现了上面的接口 <code>TimeClient</code>:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div></pre></td><td class="code"><pre><div class="line">package defaultmethods;</div><div class="line"></div><div class="line">import java.time.*;</div><div class="line">import java.lang.*;</div><div class="line">import java.util.*;</div><div class="line"></div><div class="line">public class SimpleTimeClient implements TimeClient {</div><div class="line"> </div><div class="line"> private LocalDateTime dateAndTime;</div><div class="line"> </div><div class="line"> public SimpleTimeClient() {</div><div class="line"> dateAndTime = LocalDateTime.now();</div><div class="line"> }</div><div class="line"> </div><div class="line"> public void setTime(int hour, int minute, int second) {</div><div class="line"> LocalDate currentDate = LocalDate.from(dateAndTime);</div><div class="line"> LocalTime timeToSet = LocalTime.of(hour, minute, second);</div><div class="line"> dateAndTime = LocalDateTime.of(currentDate, timeToSet);</div><div class="line"> }</div><div class="line"> </div><div class="line"> public void setDate(int day, int month, int year) {</div><div class="line"> LocalDate dateToSet = LocalDate.of(day, month, year);</div><div class="line"> LocalTime currentTime = LocalTime.from(dateAndTime);</div><div class="line"> dateAndTime = LocalDateTime.of(dateToSet, currentTime);</div><div class="line"> }</div><div class="line"> </div><div class="line"> public void setDateAndTime(int day, int month, int year,</div><div class="line"> int hour, int minute, int second) {</div><div class="line"> LocalDate dateToSet = LocalDate.of(day, month, year);</div><div class="line"> LocalTime timeToSet = LocalTime.of(hour, minute, second); </div><div class="line"> dateAndTime = LocalDateTime.of(dateToSet, timeToSet);</div><div class="line"> }</div><div class="line"> </div><div class="line"> public LocalDateTime getLocalDateTime() {</div><div class="line"> return dateAndTime;</div><div class="line"> }</div><div class="line"> </div><div class="line"> public String toString() {</div><div class="line"> return dateAndTime.toString();</div><div class="line"> }</div><div class="line"> </div><div class="line"> public static void main(String... args) {</div><div class="line"> TimeClient myTimeClient = new SimpleTimeClient();</div><div class="line"> System.out.println(myTimeClient.toString());</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<p>Suppose that you want to add new functionality to the <code>TimeClient</code> interface, such as the ability to specify a time zone through a <a href="https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html" target="_blank" rel="external"><code>ZonedDateTime</code></a> object (which is like a<a href="https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html" target="_blank" rel="external"><code>LocalDateTime</code></a> object except that it stores time zone information):</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">TimeClient</span> </span>{</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">setTime</span><span class="params">(<span class="keyword">int</span> hour, <span class="keyword">int</span> minute, <span class="keyword">int</span> second)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">setDate</span><span class="params">(<span class="keyword">int</span> day, <span class="keyword">int</span> month, <span class="keyword">int</span> year)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">setDateAndTime</span><span class="params">(<span class="keyword">int</span> day, <span class="keyword">int</span> month, <span class="keyword">int</span> year,</span></span></div><div class="line"> <span class="keyword">int</span> hour, <span class="keyword">int</span> minute, <span class="keyword">int</span> second);</div><div class="line"> <span class="function">LocalDateTime <span class="title">getLocalDateTime</span><span class="params">()</span></span>; </div><div class="line"> <span class="function">ZonedDateTime <span class="title">getZonedDateTime</span><span class="params">(String zoneString)</span></span>;</div><div class="line">}</div></pre></td></tr></table></figure>
<p>下面对于接口 <code>TimeClient</code>的修改,你也必须修改 类 <code>SimpleTimeClient</code> 并实现其中新增的方法 <code>getZonedDateTime</code>. 不过, 与其将 <code>getZonedDateTime</code> 作为 <code>abstract</code> 抽象的,不如将它替换为默认方法 <em>default implementation</em>. (记住 <a href="https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html" target="_blank" rel="external">abstract method</a> 是没有实现的方法.)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> defaultmethods;</div><div class="line"> </div><div class="line"><span class="keyword">import</span> java.time.*;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">TimeClient</span> </span>{</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">setTime</span><span class="params">(<span class="keyword">int</span> hour, <span class="keyword">int</span> minute, <span class="keyword">int</span> second)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">setDate</span><span class="params">(<span class="keyword">int</span> day, <span class="keyword">int</span> month, <span class="keyword">int</span> year)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">setDateAndTime</span><span class="params">(<span class="keyword">int</span> day, <span class="keyword">int</span> month, <span class="keyword">int</span> year,</span></span></div><div class="line"> <span class="keyword">int</span> hour, <span class="keyword">int</span> minute, <span class="keyword">int</span> second);</div><div class="line"> <span class="function">LocalDateTime <span class="title">getLocalDateTime</span><span class="params">()</span></span>;</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">static</span> ZoneId <span class="title">getZoneId</span> <span class="params">(String zoneString)</span> </span>{</div><div class="line"> <span class="keyword">try</span> {</div><div class="line"> <span class="keyword">return</span> ZoneId.of(zoneString);</div><div class="line"> } <span class="keyword">catch</span> (DateTimeException e) {</div><div class="line"> System.err.println(<span class="string">"Invalid time zone: "</span> + zoneString +</div><div class="line"> <span class="string">"; using default time zone instead."</span>);</div><div class="line"> <span class="keyword">return</span> ZoneId.systemDefault();</div><div class="line"> }</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">default</span> ZonedDateTime <span class="title">getZonedDateTime</span><span class="params">(String zoneString)</span> </span>{</div><div class="line"> <span class="keyword">return</span> ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<p>在方法签名前使用 <code>default</code> 关键字来明确接口中的方法是默认方法。接口中定义的所有方法包括默认方法,都是<code>public</code>的,因此你可以省略<code>public</code>修饰符。</p>
<p>这样定义接口的话,你就不需要修改类 <code>SimpleTimeClient</code>, 这个类 (以及所有实现了接口 <code>TimeClient</code>的类),都会已定义的 <code>getZonedDateTime</code> 方法.下面的例子, <a href="https://docs.oracle.com/javase/tutorial/java/IandI/examples/defaultmethods/TestSimpleTimeClient.java" target="_blank" rel="external"><code>TestSimpleTimeClient</code></a>,调用了<code>SimpleTimeClient</code>一个实例的 <code>getZonedDateTime</code> 的方法:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> defaultmethods;</div><div class="line"> </div><div class="line"><span class="keyword">import</span> java.time.*;</div><div class="line"><span class="keyword">import</span> java.lang.*;</div><div class="line"><span class="keyword">import</span> java.util.*;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestSimpleTimeClient</span> </span>{</div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String... args)</span> </span>{</div><div class="line"> TimeClient myTimeClient = <span class="keyword">new</span> SimpleTimeClient();</div><div class="line"> System.out.println(<span class="string">"Current time: "</span> + myTimeClient.toString());</div><div class="line"> System.out.println(<span class="string">"Time in California: "</span> +</div><div class="line"> myTimeClient.getZonedDateTime(<span class="string">"Blah blah"</span>).toString());</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<h2 id="扩展包含有默认方法的接口Extending-Interfaces-That-Contain-Default-Methods"><a href="#扩展包含有默认方法的接口Extending-Interfaces-That-Contain-Default-Methods" class="headerlink" title="扩展包含有默认方法的接口Extending Interfaces That Contain Default Methods"></a><a href="">扩展包含有默认方法的接口Extending Interfaces That Contain Default Methods</a></h2><p>当你从一个包含有默认方法的接口进行扩展时,你可以有一下做法:</p>
<ul>
<li>不提及默认方法,让扩展的几口继承原有的默认方法</li>
<li>重新定义默认方法,声明为<code>abstract</code></li>
<li>重新定义默认方法,复写它</li>
</ul>
<p>假设你按照下面的写法扩展 <code>TimeClient</code> 接口:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">AnotherTimeClient</span> <span class="keyword">extends</span> <span class="title">TimeClient</span> </span>{ }</div></pre></td></tr></table></figure>
<p>任何实现了 <code>AnotherTimeClient</code> 接口的类都有默认方法 <code>TimeClient.getZonedDateTime</code>的实现.</p>
<p>假设你按照如下写法扩展 `TimeClient接口:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">AbstractZoneTimeClient</span> <span class="keyword">extends</span> <span class="title">TimeClient</span> </span>{</div><div class="line"> <span class="function"><span class="keyword">public</span> ZonedDateTime <span class="title">getZonedDateTime</span><span class="params">(String zoneString)</span></span>;</div><div class="line">}</div></pre></td></tr></table></figure>
<p>任何实现接口 <code>AbstractZoneTimeClient</code>的类都必须重新实现方法 <code>getZonedDateTime</code>;这个方法现在是抽象方法 <code>abstract</code> ,就像接口中其他的非默认和非静态方法一样 。</p>
<p>假设你按照如下写法扩展 `TimeClient接口:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">HandleInvalidTimeZoneClient</span> <span class="keyword">extends</span> <span class="title">TimeClient</span> </span>{</div><div class="line"> <span class="function"><span class="keyword">default</span> <span class="keyword">public</span> ZonedDateTime <span class="title">getZonedDateTime</span><span class="params">(String zoneString)</span> </span>{</div><div class="line"> <span class="keyword">try</span> {</div><div class="line"> <span class="keyword">return</span> ZonedDateTime.of(getLocalDateTime(),ZoneId.of(zoneString)); </div><div class="line"> } <span class="keyword">catch</span> (DateTimeException e) {</div><div class="line"> System.err.println(<span class="string">"Invalid zone ID: "</span> + zoneString +</div><div class="line"> <span class="string">"; using the default time zone instead."</span>);</div><div class="line"> <span class="keyword">return</span> ZonedDateTime.of(getLocalDateTime(),ZoneId.systemDefault());</div><div class="line"> }</div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<p>任何实现 <code>HandleInvalidTimeZoneClient</code> 接口的类都会用其中的 <code>getZonedDateTime</code> 方法实现类替代原有接口 <code>TimeClient</code>中的默认方法实现.</p>
<h2 id="静态方法Static-Methods"><a href="#静态方法Static-Methods" class="headerlink" title="静态方法Static Methods"></a><a href="">静态方法Static Methods</a></h2><p>默认方法之外,你也可以在接口中定义静态方法 <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html" target="_blank" rel="external">static methods </a>.(静态方法属于类而不是其他的对象。类的每个实例都可以分享类它的静态方法)这样能让你更加容易的组织你库里的辅助方法;你可以将静态方法定义在统一接口中而不是其他的类里面。下面的例子定义了一个静态方法,获取一个 <a href="https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html" target="_blank" rel="external"><code>ZoneId</code></a> 对象先关的时区标识符;如果该对象没有给出相关的时区标识符则使用系统默认的时区标识符。(这样你就可以简化方法 <code>getZonedDateTime</code>):</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">TimeClient</span> </span>{</div><div class="line"> <span class="comment">// ...</span></div><div class="line"> <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> ZoneId <span class="title">getZoneId</span> <span class="params">(String zoneString)</span> </span>{</div><div class="line"> <span class="keyword">try</span> {</div><div class="line"> <span class="keyword">return</span> ZoneId.of(zoneString);</div><div class="line"> } <span class="keyword">catch</span> (DateTimeException e) {</div><div class="line"> System.err.println(<span class="string">"Invalid time zone: "</span> + zoneString +</div><div class="line"> <span class="string">"; using default time zone instead."</span>);</div><div class="line"> <span class="keyword">return</span> ZoneId.systemDefault();</div><div class="line"> }</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">default</span> <span class="keyword">public</span> ZonedDateTime <span class="title">getZonedDateTime</span><span class="params">(String zoneString)</span> </span>{</div><div class="line"> <span class="keyword">return</span> ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));</div><div class="line"> } </div><div class="line">}</div></pre></td></tr></table></figure>
<p>就像类中定义的静态方法一样,在接口中定义静态方法同样是在方法签名前面加上<code>static</code>关键字。因为接口中定义的所有方法都是<code>public</code>的,所以你可以省略<code>public</code>修饰符。</p>
<h2 id="在已有的代码库中融入默认方法Integrating-Default-Methods-into-Existing-Libraries"><a href="#在已有的代码库中融入默认方法Integrating-Default-Methods-into-Existing-Libraries" class="headerlink" title="在已有的代码库中融入默认方法Integrating Default Methods into Existing Libraries"></a><a href="">在已有的代码库中融入默认方法Integrating Default Methods into Existing Libraries</a></h2><p>默认方法可以你是你在已有的接口中添加新的功能,并同时确保兼容老版本接口写的代码。特别的是,默认方法允许你添加接受lambda表达式作为参数。本节演示了如何使用默认方法和静态方法增强 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html" target="_blank" rel="external"><code>Comparator</code></a>接口.</p>
<p>考虑 <code>Card</code> 以及 <code>Deck</code> 类,在之前的联系中出现过 <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/QandE/creating-questions.html" target="_blank" rel="external">Questions and Exercises: Classes</a>.本例重写了两个类 <a href="https://docs.oracle.com/javase/tutorial/java/IandI/examples/defaultmethods/Card.java" target="_blank" rel="external"><code>Card</code></a> 以及 <a href="https://docs.oracle.com/javase/tutorial/java/IandI/examples/defaultmethods/Deck.java" target="_blank" rel="external"><code>Deck</code></a> 作为接口. <code>Card</code> 接口包含了两个枚举类型 <code>enum</code> (<code>Suit</code> 和 <code>Rank</code>) 以及两个抽象方法(<code>getSuit</code> 和 <code>getRank</code>):</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> defaultmethods;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Card</span> <span class="keyword">extends</span> <span class="title">Comparable</span><<span class="title">Card</span>> </span>{</div><div class="line"> </div><div class="line"> <span class="keyword">public</span> <span class="keyword">enum</span> Suit { </div><div class="line"> DIAMONDS (<span class="number">1</span>, <span class="string">"Diamonds"</span>), </div><div class="line"> CLUBS (<span class="number">2</span>, <span class="string">"Clubs"</span> ), </div><div class="line"> HEARTS (<span class="number">3</span>, <span class="string">"Hearts"</span> ), </div><div class="line"> SPADES (<span class="number">4</span>, <span class="string">"Spades"</span> );</div><div class="line"> </div><div class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> value;</div><div class="line"> <span class="keyword">private</span> <span class="keyword">final</span> String text;</div><div class="line"> Suit(<span class="keyword">int</span> value, String text) {</div><div class="line"> <span class="keyword">this</span>.value = value;</div><div class="line"> <span class="keyword">this</span>.text = text;</div><div class="line"> }</div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">value</span><span class="params">()</span> </span>{<span class="keyword">return</span> value;}</div><div class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">text</span><span class="params">()</span> </span>{<span class="keyword">return</span> text;}</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="keyword">public</span> <span class="keyword">enum</span> Rank { </div><div class="line"> DEUCE (<span class="number">2</span> , <span class="string">"Two"</span> ),</div><div class="line"> THREE (<span class="number">3</span> , <span class="string">"Three"</span>), </div><div class="line"> FOUR (<span class="number">4</span> , <span class="string">"Four"</span> ), </div><div class="line"> FIVE (<span class="number">5</span> , <span class="string">"Five"</span> ), </div><div class="line"> SIX (<span class="number">6</span> , <span class="string">"Six"</span> ), </div><div class="line"> SEVEN (<span class="number">7</span> , <span class="string">"Seven"</span>),</div><div class="line"> EIGHT (<span class="number">8</span> , <span class="string">"Eight"</span>), </div><div class="line"> NINE (<span class="number">9</span> , <span class="string">"Nine"</span> ), </div><div class="line"> TEN (<span class="number">10</span>, <span class="string">"Ten"</span> ), </div><div class="line"> JACK (<span class="number">11</span>, <span class="string">"Jack"</span> ),</div><div class="line"> QUEEN (<span class="number">12</span>, <span class="string">"Queen"</span>), </div><div class="line"> KING (<span class="number">13</span>, <span class="string">"King"</span> ),</div><div class="line"> ACE (<span class="number">14</span>, <span class="string">"Ace"</span> );</div><div class="line"> <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> value;</div><div class="line"> <span class="keyword">private</span> <span class="keyword">final</span> String text;</div><div class="line"> Rank(<span class="keyword">int</span> value, String text) {</div><div class="line"> <span class="keyword">this</span>.value = value;</div><div class="line"> <span class="keyword">this</span>.text = text;</div><div class="line"> }</div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">value</span><span class="params">()</span> </span>{<span class="keyword">return</span> value;}</div><div class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">text</span><span class="params">()</span> </span>{<span class="keyword">return</span> text;}</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="keyword">public</span> Card.<span class="function">Suit <span class="title">getSuit</span><span class="params">()</span></span>;</div><div class="line"> <span class="keyword">public</span> Card.<span class="function">Rank <span class="title">getRank</span><span class="params">()</span></span>;</div><div class="line">}</div></pre></td></tr></table></figure>
<p> <code>Deck</code> 接口包含了许多操作cards的方法:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> defaultmethods; </div><div class="line"> </div><div class="line"><span class="keyword">import</span> java.util.*;</div><div class="line"><span class="keyword">import</span> java.util.stream.*;</div><div class="line"><span class="keyword">import</span> java.lang.*;</div><div class="line"> </div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Deck</span> </span>{</div><div class="line"> </div><div class="line"> <span class="function">List<Card> <span class="title">getCards</span><span class="params">()</span></span>;</div><div class="line"> <span class="function">Deck <span class="title">deckFactory</span><span class="params">()</span></span>;</div><div class="line"> <span class="function"><span class="keyword">int</span> <span class="title">size</span><span class="params">()</span></span>;</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">addCard</span><span class="params">(Card card)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">addCards</span><span class="params">(List<Card> cards)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">addDeck</span><span class="params">(Deck deck)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">shuffle</span><span class="params">()</span></span>;</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">sort</span><span class="params">()</span></span>;</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">sort</span><span class="params">(Comparator<Card> c)</span></span>;</div><div class="line"> <span class="function">String <span class="title">deckToString</span><span class="params">()</span></span>;</div><div class="line"></div><div class="line"> <span class="function">Map<Integer, Deck> <span class="title">deal</span><span class="params">(<span class="keyword">int</span> players, <span class="keyword">int</span> numberOfCards)</span></span></div><div class="line"> <span class="keyword">throws</span> IllegalArgumentException;</div><div class="line"></div><div class="line">}</div></pre></td></tr></table></figure>
<p>类 <a href="https://docs.oracle.com/javase/tutorial/java/IandI/examples/defaultmethods/PlayingCard.java" target="_blank" rel="external"><code>PlayingCard</code></a> 实现了 接口 <code>Card</code>, 类 <a href="https://docs.oracle.com/javase/tutorial/java/IandI/examples/defaultmethods/StandardDeck.java" target="_blank" rel="external"><code>StandardDeck</code></a> 实现了接口 <code>Deck</code>.</p>
<p>类 <code>StandardDeck</code> 实现了类的 <code>Deck.sort</code> 方法:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StandardDeck</span> <span class="keyword">implements</span> <span class="title">Deck</span> </span>{</div><div class="line"> </div><div class="line"> <span class="keyword">private</span> List<Card> entireDeck;</div><div class="line"> </div><div class="line"> <span class="comment">// ...</span></div><div class="line"> </div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sort</span><span class="params">()</span> </span>{</div><div class="line"> Collections.sort(entireDeck);</div><div class="line"> }</div><div class="line"> </div><div class="line"> <span class="comment">// ...</span></div><div class="line">}</div></pre></td></tr></table></figure>
<p> <code>Collections.sort</code> 方法讲一个List实例进行了排序,List实例的元素类型是实现了 <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html" target="_blank" rel="external"><code>Comparable</code></a>接口.成员变量 <code>entireDeck</code> 是一个元素为<code>Card</code>的 <code>List</code>实例。 <code>Card</code>实现了<code>Comparable</code> 接口. 类 <code>PlayingCard</code> 实现了 <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html#compareTo-T-" target="_blank" rel="external"><code>Comparable.compareTo</code></a> 方法,内容如下:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">hashCode</span><span class="params">()</span> </span>{</div><div class="line"> <span class="keyword">return</span> ((suit.value()-<span class="number">1</span>)*<span class="number">13</span>)+rank.value();</div><div class="line">}</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">compareTo</span><span class="params">(Card o)</span> </span>{</div><div class="line"> <span class="keyword">return</span> <span class="keyword">this</span>.hashCode() - o.hashCode();</div><div class="line">}</div></pre></td></tr></table></figure>
<p> 方法 <code>StandardDeck.sort()</code> 通过 <code>compareTo</code> 方法是扑克先按照花色再按照点数排序。如果你想要让扑克先按照点数,再按照花色排序该怎么做呢?你可能需要实现<code>Comparator</code>接口来明确新的排序规则,并使用方法<a href="https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-java.util.Comparator-" target="_blank" rel="external"><code>sort(List list, Comparator c)</code></a> ( <code>sort</code>方法包含<code>Comparator</code>参数的版本).你可以在类<code>StandardDeck</code>中定义如下方法:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sort</span><span class="params">(Comparator<Card> c)</span> </span>{</div><div class="line"> Collections.sort(entireDeck, c);</div><div class="line">}</div></pre></td></tr></table></figure>
<p>有了这个方法你可以,你是可以使用 <code>Collections.sort</code> 明确 <code>Card</code> 类实例的排序规则.一种方法是实现 <code>Comparator</code> 接口来明确扑克排序的规则.下面的类 <a href="https://docs.oracle.com/javase/tutorial/java/IandI/examples/defaultmethods/SortByRankThenSuit.java" target="_blank" rel="external"><code>SortByRankThenSuit</code></a> 进行了示例:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> defaultmethods;</div><div class="line"></div><div class="line"><span class="keyword">import</span> java.util.*;</div><div class="line"><span class="keyword">import</span> java.util.stream.*;</div><div class="line"><span class="keyword">import</span> java.lang.*;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SortByRankThenSuit</span> <span class="keyword">implements</span> <span class="title">Comparator</span><<span class="title">Card</span>> </span>{</div><div class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">compare</span><span class="params">(Card firstCard, Card secondCard)</span> </span>{</div><div class="line"> <span class="keyword">int</span> compVal =</div><div class="line"> firstCard.getRank().value() - secondCard.getRank().value();</div><div class="line"> <span class="keyword">if</span> (compVal != <span class="number">0</span>)</div><div class="line"> <span class="keyword">return</span> compVal;</div><div class="line"> <span class="keyword">else</span></div><div class="line"> <span class="keyword">return</span> firstCard.getSuit().value() - secondCard.getSuit().value(); </div><div class="line"> }</div><div class="line">}</div></pre></td></tr></table></figure>
<p>下面的调用时扑克先按照点数再按照花色排序:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">StandardDeck myDeck = <span class="keyword">new</span> StandardDeck();</div><div class="line">myDeck.shuffle();</div><div class="line">myDeck.sort(<span class="keyword">new</span> SortByRankThenSuit());</div></pre></td></tr></table></figure>
<p>不过,这种方法太冗长verbose;如果你可以明确按照什么排序而不是怎么排序会更好。假设你是写<code>Compatator</code>接口 开发者。你会在<code>Comparator</code>接口中添加什么默认方法或者静态方法来是其他开发者更加简单的明确排序规则?</p>
<p>一开始,你想要将这些扑克按照点数排序而不管花色。你可以这样调用 `StandardDeck.sort方法:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">StandardDeck myDeck = <span class="keyword">new</span> StandardDeck();</div><div class="line">myDeck.shuffle();</div><div class="line">myDeck.sort(</div><div class="line"> (firstCard, secondCard) -></div><div class="line"> firstCard.getRank().value() - secondCard.getRank().value()</div><div class="line">);</div></pre></td></tr></table></figure>
<p>因为接口 <code>Comparator</code> 是一个功能接口 <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#approach6" target="_blank" rel="external">functional interface</a>, 所以你可以使用lambda表达式作为<code>sort</code>方法的参数。本例中,lambda表达式比较了两个整数值。</p>
<p>这样可以是开发者通过只调用<code>Card.getRank</code>方法就简单的创建一个<code>Comparator</code>实例。并且,他可以付诸开发者<code>Comparator</code>实例来比较任何可以通过入住<code>getValue</code> <code>hashCode</code>等方法返回数字值的对象。<code>Comparator</code>接口已经通过静态方法 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#comparing-java.util.function.Function-java.util.Comparator-" target="_blank" rel="external"><code>comparing</code></a>增强而拥有这个能力:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">myDeck.sort(Comparator.comparing((card) -> card.getRank()));</div></pre></td></tr></table></figure>
<p>这个例子中,你也可以通过方法引用来代替 <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html" target="_blank" rel="external">method reference</a>:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">myDeck.sort(Comparator.comparing(Card::getRank));</div></pre></td></tr></table></figure>
<p>这种调用很好的演示了用什么排序而不是怎么排序.</p>
<p> <code>Comparator</code> 接口同样有其他类似<code>comparing</code>版本的静态方法增强如 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#comparingDouble-java.util.function.ToDoubleFunction-java.util.Comparator-" target="_blank" rel="external"><code>comparingDouble</code></a> 和 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#comparingLong-java.util.function.ToLongFunction-" target="_blank" rel="external"><code>comparingLong</code></a> 可以用来创建通过其他数据类型比较的 <code>Comparator</code> 实例.</p>
<p>假设你的开发者想要创建一个 <code>Comparator</code> 实例,使用不止一个条件比较对象。例如,你如何将扑克先按照点数再按照花色排序?像前面的例子一样,你可以使用lambda表达式来明确这些排序规则:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">StandardDeck myDeck = <span class="keyword">new</span> StandardDeck();</div><div class="line">myDeck.shuffle();</div><div class="line">myDeck.sort(</div><div class="line"> (firstCard, secondCard) -> {</div><div class="line"> <span class="keyword">int</span> compare =</div><div class="line"> firstCard.getRank().value() - secondCard.getRank().value();</div><div class="line"> <span class="keyword">if</span> (compare != <span class="number">0</span>)</div><div class="line"> <span class="keyword">return</span> compare;</div><div class="line"> <span class="keyword">else</span></div><div class="line"> <span class="keyword">return</span> firstCard.getSuit().value() - secondCard.getSuit().value();</div><div class="line"> } </div><div class="line">);</div></pre></td></tr></table></figure>
<p>如果开发者可以通过一系列的 <code>Comparator</code> 实例来构造 <code>Comparator</code> 实例,将会更加简单. <code>Comparator</code> 接口已经通过默认方法 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#thenComparing-java.util.Comparator-" target="_blank" rel="external"><code>thenComparing</code></a>增强而拥有了这个能力:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">myDeck.sort(</div><div class="line"> Comparator</div><div class="line"> .comparing(Card::getRank)</div><div class="line"> .thenComparing(Comparator.comparing(Card::getSuit)));</div></pre></td></tr></table></figure>
<p> <code>Comparator</code> 接口同样有其他版本的 <code>thenComparing</code>方法 (例如 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#thenComparingDouble-java.util.function.ToDoubleFunction-" target="_blank" rel="external"><code>thenComparingDouble</code></a> 以及 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#thenComparingLong-java.util.function.ToLongFunction-" target="_blank" rel="external"><code>thenComparingLong</code></a>) 这样你可以构造 <code>Comparator</code> 实例来比较其他的数据类型.</p>
<p>假设你的开发者想要创建 <code>Comparator</code> 实例,倒序排列一个集合,例如你想要将扑克按照点数降序排列,从A到2.像之前一样你可以定义lambda表达式来实现。不过,如果可以通过已有的<code>Comparator</code>实例调用方法将会更简单。<code>Comparator</code>接口已经预知了默认方法 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#reversed--" target="_blank" rel="external"><code>reversed</code></a>:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">myDeck.sort(</div><div class="line"> Comparator.comparing(Card::getRank)</div><div class="line"> .reversed()</div><div class="line"> .thenComparing(Comparator.comparing(Card::getSuit)));</div></pre></td></tr></table></figure>
<p>这个例子演示了 <code>Comparator</code> 接口是如何通过默认方法,静态方法,lambda表达式,以及方法引用来增强的,这样就创建了更有表现力的库和方法,功能开发者可以快速的推断出该如何调用其中的方法。用同样的构造来创建你自己的库里的接口。</p>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/03/27/java-tutorial-learning_interface_evolving/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Hello Young">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Hello World">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/03/27/java-tutorial-learning_interface_evolving/" itemprop="url">Java 学习指南_学习Java:进化接口</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-27T16:40:59+08:00">
2018-03-27
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/java/" itemprop="url" rel="index">
<span itemprop="name">java</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="进化接口-Interfaces"><a href="#进化接口-Interfaces" class="headerlink" title="进化接口 Interfaces"></a>进化接口 Interfaces</h1><p>考虑一下你之前开发的一个名为 <code>DoIt</code>的接口:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">DoIt</span> </span>{</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">doSomething</span><span class="params">(<span class="keyword">int</span> i, <span class="keyword">double</span> x)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">int</span> <span class="title">doSomethingElse</span><span class="params">(String s)</span></span>;</div><div class="line">}</div></pre></td></tr></table></figure>
<p>假设,过了一段时间以后,你想要在接口中添加一个方法,那么接口会变成下面的样子:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">DoIt</span> </span>{</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">doSomething</span><span class="params">(<span class="keyword">int</span> i, <span class="keyword">double</span> x)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">int</span> <span class="title">doSomethingElse</span><span class="params">(String s)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">boolean</span> <span class="title">didItWork</span><span class="params">(<span class="keyword">int</span> i, <span class="keyword">double</span> x, String s)</span></span>;</div><div class="line"> </div><div class="line">}</div></pre></td></tr></table></figure>
<p>如果要做这个改变,那么所有实现了旧的<code>DoIt</code>接口的类都不能再使用了,因为他们没有实现接口的所有方法。依赖这个接口的开发者将会大声抗议。</p>
<p>试着在开始就预见你的借口的所有用处并做完整的定义。如果你想要在接口中添加额外的方法,可以有几种选择。你可以创建一个 <code>DoItPlus</code> 接口来扩展 <code>DoIt</code>:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">DoItPlus</span> <span class="keyword">extends</span> <span class="title">DoIt</span> </span>{</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">boolean</span> <span class="title">didItWork</span><span class="params">(<span class="keyword">int</span> i, <span class="keyword">double</span> x, String s)</span></span>;</div><div class="line"> </div><div class="line">}</div></pre></td></tr></table></figure>
<p>现在你代码的用户可以选择继续使用旧的接口,或者升级到新的接口。</p>
<p>同样,也可以定义新的方法为默认方法 <a href="https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html" target="_blank" rel="external">default methods</a>. 下面的列子定义了一个名为<code>didItWork</code>:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">DoIt</span> </span>{</div><div class="line"></div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">doSomething</span><span class="params">(<span class="keyword">int</span> i, <span class="keyword">double</span> x)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">int</span> <span class="title">doSomethingElse</span><span class="params">(String s)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">default</span> <span class="keyword">boolean</span> <span class="title">didItWork</span><span class="params">(<span class="keyword">int</span> i, <span class="keyword">double</span> x, String s)</span> </span>{</div><div class="line"> <span class="comment">// Method body </span></div><div class="line"> }</div><div class="line"> </div><div class="line">}</div></pre></td></tr></table></figure>
<p>注意,你必须为默认方法提供一个实现。你也可以在现存从接口中定义一个新的静态方法 <a href="https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html#static" target="_blank" rel="external">static methods</a>。实现了这个接口的用户可以通过默认方法或者静态方法类增强他们的功能,而不需要为适应新增的方法修改和重新编译所有的类。</p>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/03/26/java-tutorial-learning_interface_use_as_type/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Hello Young">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Hello World">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/03/26/java-tutorial-learning_interface_use_as_type/" itemprop="url">Java 学习指南_学习Java:使用接口作为一个类型</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-26T17:40:59+08:00">
2018-03-26
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/java/" itemprop="url" rel="index">
<span itemprop="name">java</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="使用接口作为一个类型"><a href="#使用接口作为一个类型" class="headerlink" title="使用接口作为一个类型"></a>使用接口作为一个类型</h1><p>当你定义一个新的接口,你定义了一个新的引用数据类型。使用其他引用类型名的地方都可以使用接口名。如果你定义的一个接口的引用类型变量,赋值必须为实现了这个接口的类的实例。</p>
<p>下面的例子,定义了一个找到size比较大的对象的方法,对象可以是任何实现了<code>Relatable</code>接口的类的实例:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> Object <span class="title">findLargest</span><span class="params">(Object object1, Object object2)</span> </span>{</div><div class="line"> Relatable obj1 = (Relatable)object1;</div><div class="line"> Relatable obj2 = (Relatable)object2;</div><div class="line"> <span class="keyword">if</span> ((obj1).isLargerThan(obj2) > <span class="number">0</span>)</div><div class="line"> <span class="keyword">return</span> object1;</div><div class="line"> <span class="keyword">else</span> </div><div class="line"> <span class="keyword">return</span> object2;</div><div class="line">}</div></pre></td></tr></table></figure>
<p>通过转换 <code>object1</code> 类型为 <code>Relatable</code> 类型, 就可以调用其 <code>isLargerThan</code> 方法.</p>
<p>如果你注意到很多的类都实现了 <code>Relatable</code>接口,任何这些类的实例都可以通过方法 <code>findLargest()</code>进行比较—只要他们是同一个类的对象.同样,他们都可以通过下面的方法进行比较:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> Object <span class="title">findSmallest</span><span class="params">(Object object1, Object object2)</span> </span>{</div><div class="line"> Relatable obj1 = (Relatable)object1;</div><div class="line"> Relatable obj2 = (Relatable)object2;</div><div class="line"> <span class="keyword">if</span> ((obj1).isLargerThan(obj2) < <span class="number">0</span>)</div><div class="line"> <span class="keyword">return</span> object1;</div><div class="line"> <span class="keyword">else</span> </div><div class="line"> <span class="keyword">return</span> object2;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isEqual</span><span class="params">(Object object1, Object object2)</span> </span>{</div><div class="line"> Relatable obj1 = (Relatable)object1;</div><div class="line"> Relatable obj2 = (Relatable)object2;</div><div class="line"> <span class="keyword">if</span> ( (obj1).isLargerThan(obj2) == <span class="number">0</span>)</div><div class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line"> <span class="keyword">else</span> </div><div class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line">}</div></pre></td></tr></table></figure>
<p>这个方法对所有的”relatable”对象都有效,不管他们的继承成了什么,当他们实现了 <code>Relatable</code>接口,他们同时可以是谈么本身(或者其父类)的类型, 也可以是一个 <code>Relatable</code> 类型.这就给了他们多重继承的优势,他们同时可以表现出其父类和接口的行为.</p>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/03/26/java-tutorial-learning_interface_implements/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Hello Young">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.jpg">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Hello World">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2018/03/26/java-tutorial-learning_interface_implements/" itemprop="url">Java 学习指南_学习Java:实现接口</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2018-03-26T16:21:59+08:00">
2018-03-26
</time>
</span>
<span class="post-category" >
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/java/" itemprop="url" rel="index">
<span itemprop="name">java</span>
</a>
</span>
</span>