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

神奇的JAVA多态

时间:2016-04-20 17:44:00      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

以前理解了基本思想,这版本的演示和应用比较真实。

顺路下来抽象方法和类,接口,就顺理成章啦。。。

技术分享

JAVA文件放一块了,分别对照前一个帖子的文件名:

////////////////////////////
package cc.openhome;

public class Role {
  private String name;
  private int level;
  private int blood;

  public int getBlood() {
    return blood;
  }

  public void setBlood(int blood) {
    this.blood = blood;
  }

  public int getLevel() {
    return level;
  }

  public void setLevel(int level) {
    this.level = level;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
  
  public void fight() {
      // subclass override this method;
  }
}
//////////////////////////////////////
package cc.openhome;



public class SwordsMan extends Role {
    @Override
  public void fight() {
    System.out.println("Attack with swords");
  }
}
/////////////////////////////////////////////
package cc.openhome;



public class Magician extends Role {
    @Override
  public void fight() {
    System.out.println("Attack with magician");
  }
}
///////////////////////////////////////////
package cc.openhome;

public class RPG {
  public static void main(String[] args) {
    SwordsMan swordsMan = new SwordsMan();
    swordsMan.setName("Justin");
    swordsMan.setLevel(1);
    swordsMan.setBlood(200);
    System.out.printf("SwordsMan: (%s, %d, %d)%n", swordsMan.getName(),
                swordsMan.getLevel(), swordsMan.getBlood());

    Magician magician = new Magician();
    magician.setName("Monica");
    magician.setLevel(2);
    magician.setBlood(100);
    System.out.printf("Magician: (%s, %d, %d)%n", magician.getName(),
                                magician.getLevel(), magician.getBlood());
    
    showBlood(swordsMan);
    showBlood(magician);
    drawFight(swordsMan);
    drawFight(magician);
    Role role1 = new SwordsMan();
    Role role2 = new Magician();
    SwordsMan swordsMan1 = (SwordsMan) role1;
    Magician magician1 = (Magician) role2;
    SwordsMan swordsMan2 = new SwordsMan();
    Role role3 = swordsMan2;
    SwordsMan swordsMan3 = (SwordsMan) role3;
  }
  
  /*
  public static void showBlood(SwordsMan swordsMan) {
      System.out.printf("%s Blood %d%n", 
                                          swordsMan.getName(), swordsMan.getBlood());
  }
  
  public static void showBlood(Magician magician) {
      System.out.printf("%s Blood %d%n", 
                                          magician.getName(), magician.getBlood());
  }
  */
  
  public static void showBlood(Role role) {
      System.out.printf("%s Blood %d%n", 
                                          role.getName(), role.getBlood());
  }
  
  public static void drawFight(Role role) {
      System.out.print(role.getName());
      role.fight();
  }
}
/////////////////////////////////

技术分享

神奇的JAVA多态

标签:

原文地址:http://www.cnblogs.com/aguncn/p/5413422.html

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