个人博客:skyfffire.cn

上溯造型《Thinking in Java》随笔011

 1 package cn.skyfffire;
 2 
 3 /**
 4  * 
 5  * @author skyfffire
 6  * 
 7  * 测试上溯造型
 8  *
 9  */
10 class Instrument {
11     public void play() {
12         System.out.println(this.getClass().getName() + "is run");
13     }
14     
15     static void tune(Instrument i) {
16         i.play();
17     }
18 }
19 
20 public class Wind extends Instrument {
21     public static void main(String[] args) {
22         Wind flute = new Wind();
23         
24         /**
25          *  可以发现,即使类型不同
26          *  但是只要是继承关系,就完成了上溯造型的过程。
27          */
28         Instrument.tune(flute);
29     }
30 }

 

posted @ 2017-03-01 09:05  skyfffire  阅读(100)  评论(0编辑  收藏  举报
个人博客:skyfffire.cn