From Gossip@caterpillar

Java Essence: Ä~©Ó¤F¤°»ò¡H



 


§A¥¿¦b³]­p¤@­Ó¹CÀ¸¡AùØÀY¦³¼C¤h¡G
public class SwordsMan {
    private String name;
    private int level;
    private int blood;

    public String getName() {
return name; }

    public void setName(String name) { this.name = name; }
    public int getLevel() { return level; }
    public void setLevel(int level) { this.level = level; }
    public int getBlood() { return blood; }
    public void setBlood(int blood) { this.blood = blood; }

    public void fight() {
        System.out.println("´§¼C");
    }
}

¤]¦³Å]ªk®v¡G
public class Magician {
    private String name;
    private int level;
    private int blood;

    public String getName() {
return name; }

    public void setName(String name) { this.name = name; }
    public int getLevel() { return level; }
    public void setLevel(int level) { this.level = level; }
    public int getBlood() { return blood; }
    public void setBlood(int blood) { this.blood = blood; }

    public void fight() {

        System.out.println("§ðÀ»Å]ªk");
    }
    public void cure() {
        System.out.println("ªvÀøÅ]ªk");
    }
}

ÅãµM¦a¡AùØÀY¦³³\¦hµ{¦¡½X­«½Æ¤F¡A¦Ò¼{¼C¤h»PÅ]ªk®v³£¬O¤@ºØ¨¤¦â¡]Sprite¡^¡A§A¥i¥H³]­p¤@­Ó¤÷Ãþ§OSprite¡G
public class Sprite {
    private String name;
    private int level;
    private int blood;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public int getLevel() { return level; }
    public void setLevel(int level) { this.level = level; }
    public int getBlood() { return blood; }
    public void setBlood(int blood) { this.blood = blood; }
}

µM«áÅý¼C¤hÄ~©Ó¨¤¦â¡G
public class SwordsMan extends Sprite {
    public void fight() {
        System.out.println("´§¼C");
    }
}

Å]ªk®v¤]Ä~©Ó¨¤¦â¡G
public class Magician extends Sprite {
    public void fight() {
        System.out.println("§ðÀ»Å]ªk");
    }
    public void cure() {
        System.out.println("ªvÀøÅ]ªk");
    }
}

³o»ò¤@¨Ó¡A­«½Æªºµ{¦¡½X¤F¥i¥H¦b¤÷Ãþ§O¤¤ºûÅ@¡A¤£¹L§Aµo²{¡AÁÙ¦³­Ófight()¤èªkÁöµM¥»Å餺®e©w¸q¤£¦P¡A¦ý¤èªkñ¸p½T¹ê­«½Æ©w¸q¤F¡A³o¤èªkñ¸p©w¸qÀ³¸Ó©ñ¨ì¤÷Ãþ§O¡A©ó¬O§A¥i¥H§ï³o»ò¼¶¼g
Sprite¡G
public class Sprite {
    private String name;
    private int level;
    private int blood;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public int getLevel() { return level; }
    public void setLevel(int level) { this.level = level; }
    public int getBlood() { return blood; }
    public void setBlood(int blood) { this.blood = blood; }

    public void fight() {
    }
}

fight()¤°»ò³£¤£§@¡A¥»Å鬰ªÅ¡A¥u¬O¬°¤F©w¸q¤èªkñ¸p¡A³o¼Ë¦n¹³©Ç©Çªº¡A§YµM¤£©w¸q¥»Åé¡A¨º¤£¦p©w¸q¬°©â¶H¤èªk§a¡I³o¼Ë¥i¥H¤£¼g¨º©_©ÇªºªÅ¥»Åé¡G
public abstract class Sprite {
    private String name;
    private int level;
    private int blood;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public int getLevel() { return level; }
    public void setLevel(int level) { this.level = level; }
    public int getBlood() { return blood; }
    public void setBlood(int blood) { this.blood = blood; }

    public abstract void fight();
}

¦]¬°Ãþ§O¦³©â¶H¤èªk¡A©Ò¥HÃþ§O¥»¨­¥ç­n¬°©â¶HÃþ§O¡A³o»ò¤@¨Ó¡A°£¤F¤£¥Î¼g¨ºªÅ¥»Å餧¥~¡A¤lÃþ§O¤]´N¤@©w±o
­«·s©w¸qfight()¤èªk¡AÁÙ¤£¥Î¾á¤ß¥´¦¨¤Ffi9ht()¡C

¥H¤W¬OÄ~©Óªº³Ì°ò¥»À³¥Î¡A¨º»ò·Q·Q¡A§AÄ~©Ó¤F¤°»ò¡H¤÷Ãþ§Oªºµ{¦¡½X©w¸q¡H¥u¹ï¤F¤@³¡¥÷¡I

SwordsMan»PMagician¤£¶ÈÄ~©Ó¤FSprite¤¤ªºµ{¦¡½X©w¸q¡A¨ä¹ê¡AÁÙÄ~©Ó¤F¬O¤@ºØªºÃö«Y¡A¼C¤h¬O¤@ºØ¨¤¦â¡AÅ]ªk®v¤]¬O¤@ºØ¨¤¦â¡A¤]´N¬O»¡¡AÄ~©Ó¤£³æ¥u¬OÄ~©Ó¤÷Ãþ§Oªºµ{¦¡©w¸q¡AÁÙÄ~©Ó¤FºØÃþ¡C

½ÐÄ~Äò¬Ý
³o¬O­þºØªF¦è¡H