码迷,mamicode.com
首页 > 编程语言 > 详细

[20-05-06][Thinking in Java 12]Java Interfaces 2 - Interface

时间:2020-05-06 19:34:54      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:ssi   on()   没有   return   note   out   tostring   public   xtend   

1 package test_3_1;
2 
3 public enum Note {
4     
5     MIDDLE_C, C_SHARP, B_FLAT;
6 }

 

 1 package test_3_1;
 2 
 3 interface Instrument {
 4 
 5     // 自动static final
 6     int VALUE = 5;
 7     
 8     // 没有方法体,自动public
 9     void play(Note n);
10     void adjust();
11 }

 

 1 package test_3_1;
 2 
 3 public class Wind implements Instrument {
 4 
 5     public void play(Note n) {
 6         
 7         System.out.println(this + ".play() " + n);
 8     }
 9     
10     public String toString() {
11         
12         return "Wind";
13     }
14     
15     public void adjust() {
16         
17         System.out.println(this + ".adjust() ");
18     }
19 }

 

 1 package test_3_1;
 2 
 3 public class Music {
 4 
 5     public static void tune(Instrument i) {
 6 
 7         i.play(Note.MIDDLE_C);
 8     }
 9 
10     public static void tuneAll(Instrument[] e) {
11 
12         for (Instrument i : e)
13             tune(i);
14     }
15 
16     public static void main(String[] args) {
17 
18         Instrument[] orchestra = { new Wind(), new Percussion(), new Stringed(), new Brass(), new Woodwind() };
19 
20         tuneAll(orchestra);
21 
22     }
23 
24 }

 

 1 package test_3_1;
 2 
 3 public class Stringed implements Instrument {
 4 
 5     public void play(Note n) {
 6 
 7         System.out.println(this + ".play() " + n);
 8     }
 9 
10     public String toString() {
11 
12         return "Stringed";
13     }
14 
15     public void adjust() {
16 
17         System.out.println(this + ".adjust() ");
18     }
19 }

 

1 package test_3_1;
2 
3 public class Brass extends Wind {
4 
5     public String toString() {
6         
7         return "Brass";
8     }
9 }

 

1 package test_3_1;
2 
3 public class Woodwind extends Wind {
4 
5     public String toString() {
6         
7         return "Woodwind";
8     }
9 }

 

 1 package test_3_1;
 2 
 3 public class Music {
 4 
 5     public static void tune(Instrument i) {
 6 
 7         i.play(Note.MIDDLE_C);
 8     }
 9 
10     public static void tuneAll(Instrument[] e) {
11 
12         for (Instrument i : e)
13             tune(i);
14     }
15 
16     public static void main(String[] args) {
17 
18         Instrument[] orchestra = { new Wind(), new Percussion(), new Stringed(), new Brass(), new Woodwind() };
19 
20         tuneAll(orchestra);
21 
22     }
23 
24 }

 

结果如下:

Wind.play() MIDDLE_C
Percussion.play() MIDDLE_C
Stringed.play() MIDDLE_C
Brass.play() MIDDLE_C
Woodwind.play() MIDDLE_C

[20-05-06][Thinking in Java 12]Java Interfaces 2 - Interface

标签:ssi   on()   没有   return   note   out   tostring   public   xtend   

原文地址:https://www.cnblogs.com/mirai3usi9/p/12838302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!