Changes between Version 8 and Version 9 of waue/2009/0717


Ignore:
Timestamp:
Jul 17, 2009, 2:48:00 PM (15 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2009/0717

    v8 v9  
    8383   * interface的成員可看成是 '''public static final'''的,不過在interface中一般不定義數據成員
    8484 * 從某種意義上說,interface是一種特殊形式的abstract class。
     85
     86 == 範例 ==
     87
     88{{{
     89#!java
     90interface CanFight {
     91  void fight();
     92}
     93
     94interface CanSwim {
     95  void swim();
     96}
     97
     98interface CanFly {
     99  void fly();
     100}
     101
     102interface CanClimb {
     103  void climb();
     104}
     105
     106class ActionCharacter {
     107  public void fight() {}
     108}
     109
     110class Hero extends ActionCharacter
     111  implements CanFight, CanSwim, CanFly, CanClimb {
     112  public void swim() {}
     113  public void fly() {}
     114  public void climb() {}
     115}
     116
     117public class Ex09 {
     118  static void t(CanFight x) {
     119    x.fight();
     120  }
     121  static void u(CanSwim x) {
     122    x.swim();
     123  }
     124  static void v(CanFly x) {
     125    x.fly();
     126  }
     127  static void z(CanClimb x) {
     128    x.climb();
     129  }
     130  static void w(ActionCharacter x) {
     131    x.fight();
     132  }
     133  public static void main(String[] args) {
     134    Hero h = new Hero();
     135    t(h); // Treat it as a CanFight
     136    u(h); // Treat it as a CanSwim
     137    v(h); // Treat it as a CanFly
     138    z(h); // Treat it as a CanClimb
     139    w(h); // Treat it as an ActionCharacter
     140  }
     141}
     142}}}