Created
February 9, 2015 22:05
-
-
Save anonymous/791f50d9f590e2698899 to your computer and use it in GitHub Desktop.
Java interfaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Weapon { | |
public boolean fire(); | |
public void reload(); | |
} | |
public interface Vehicle { | |
public void drive(int distance); | |
} | |
public class Tank implements Vehicle, Weapon { | |
private int position = 0; | |
private int ammo = 1; | |
public boolean fire() { | |
if (ammo > 0) { | |
ammo -= 1; | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public void reload() { | |
ammo = 1; | |
} | |
public void drive(int distance) { | |
position += distance; | |
} | |
} | |
public class FireArm implements Weapon { | |
private int ammo = 20; | |
public boolean fire() { | |
if (ammo > 0) { | |
ammo -= 1; | |
return true; | |
} else { | |
return false; | |
} | |
public void reload() { | |
ammo = 20; | |
} | |
} | |
public class Car implements Vehicle { | |
private boolean direction = 0; | |
private double x = 0, y = 0; | |
public void drive(int distance) { | |
double xT += Math.sin(direction) * d; | |
double yT += Math.cos(direction) * d; | |
x = xT; | |
y = yT; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment