Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- public class PassAClass {
- public static void main(String[] args) {
- MagicGun gun = new MagicGun();
- gun.loadWith(Pebbl.class);
- gun.loadWith(Bullet.class);
- gun.loadWith(NuclearMissl.class);
- for(int i=0; i<5; i++){
- try {
- String effect = gun.shoot().getName();
- System.out.printf("You've %sed the target!\n", effect);
- } catch (GunIsEmptyException e) {
- System.err.printf("click\n");
- }
- }
- }
- }
- class MagicGun {
- LinkedList<Class<? extends Ammo>> ammos = new LinkedList<Class<? extends Ammo>>();
- public void loadWith(Class<? extends Ammo> ammo){
- ammos.add(ammo);
- }
- public Class<? extends Ammo> shoot() throws GunIsEmptyException{
- if (ammos.isEmpty()) {
- throw new GunIsEmptyException();
- }
- return ammos.remove();
- }
- }
- class GunIsEmptyException extends Exception { }
- interface Ammo { }
- class Pebbl implements Ammo { }
- class Bullet implements Ammo { }
- class NuclearMissl implements Ammo { }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement