![]() |
|
Tutorial Basic polymorphism example - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Java, JVM, & JRE (https://sinister.li/Forum-Java-JVM-JRE) +--- Thread: Tutorial Basic polymorphism example (/Thread-Tutorial-Basic-polymorphism-example) |
Basic polymorphism example - Chalant - 11-12-2013 I was playing around trying to get the concept of polymorphism down, and I ended up creating a basic skeleton for entity-based combat (could be used in a game, though this is just barebones), simply by printing out the actions. Main.java Code: package org.chaltech.project;
import org.chaltech.project.model.NPC;
import org.chaltech.project.model.Player;
/**
* @author Chalant <chalant123@gmail.com>
*/
public class Main {
/**
* Constructor for our main class,
* use this to assign object variables.
*/
public Main() {
Player player = new Player("Chalant", 10);
Player playerTwo = new Player("Nonchalant", 9);
NPC npc = new NPC("Rat", 6);
NPC npcTwo = new NPC("Goblin", 9);
/**
* Player vs NPC
*/
player.attack(npc, 1);
npc.attack(player, 2);
/**
* NPC vs NPC
*/
npc.attack(npcTwo, 3);
/**
* Player vs Player
*/
player.attack(playerTwo, 3);
}
/**
* Initialisation method, use this to invoke
* the constructor for our main class
* @param args
*/
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
new Main();
}
}).start();
}
}Entity.java Code: package org.chaltech.project.model;
/**
* @author Chalant <chalant123@gmail.com>
*/
public abstract class Entity {
public abstract void attack(Entity entity, int damage);
public abstract void setHealth(int health);
public abstract int getHealth();
public abstract String getName();
}Player.java Code: package org.chaltech.project.model;
/**
* @author Chalant <chalant123@gmail.com>
*/
public class Player extends Entity {
private int health;
private String name;
public Player(String name, int health) {
this.name = name;
this.health = health;
}
public void attack(Entity entity, int damage) {
if (entity instanceof NPC || entity instanceof Player) {
if (!((entity.getHealth() - damage) < 0)) {
entity.setHealth(entity.getHealth() - damage);
} else {
entity.setHealth(0);
}
System.out.println(getName() + " attacks " + entity.getName() + " and deals " + damage + " damage. \n");
System.out.println(entity.getHealth() > 0 ? entity.getName() + " now has " + entity.getHealth() + " health. \n" : entity.getName() + " has died.\n");
}
}
public void setHealth(int health) {
this.health = health;
}
public int getHealth() {
return health;
}
public String getName() {
return name;
}
}NPC.java Code: package org.chaltech.project.model;
/**
* @author Chalant <chalant123@gmail.com>
*/
public class NPC extends Entity {
private int health;
private String name;
public NPC(String name, int health) {
this.name = name;
this.health = health;
}
public void attack(Entity entity, int damage) {
if (entity instanceof Player || entity instanceof NPC) {
if (!((entity.getHealth() - damage) < 0)) {
entity.setHealth(entity.getHealth() - damage);
} else {
entity.setHealth(0);
}
System.out.println(getName() + " attacks " + entity.getName() + " and deals " + damage + " damage. \n");
System.out.println(entity.getHealth() > 0 ? entity.getName() + " now has " + entity.getHealth() + " health. \n" : entity.getName() + " has died.\n");
}
}
public void setHealth(int health) {
this.health = health;
}
public int getHealth() {
return health;
}
public String getName() {
return name;
}
}Output: Code: Chalant attacks Rat and deals 1 damage.
Rat now has 5 health.
Rat attacks Chalant and deals 2 damage.
Chalant now has 8 health.
Rat attacks Goblin and deals 3 damage.
Goblin now has 6 health.
Chalant attacks Nonchalant and deals 3 damage.
Nonchalant now has 6 health.RE: Basic polymorphism example - §herlock - 07-03-2014 Great one, I have seen people don't use abstract class which is most important here. |