Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.function.Supplier;
- public class Main {
- public interface Movable {
- void move();
- }
- public static class Dragon implements Movable {
- String name;
- int size;
- Type type;
- public Dragon(String name, int size, Type type) {
- this.name = name;
- this.size = size;
- this.type = type;
- }
- @Override
- public void move() {
- type.move();
- }
- public void introduce() {
- System.out.println(String.format("My name is %s, my size is %s", name, size));
- }
- }
- public enum Type implements Movable {
- FLYING(() -> "I am flying"),
- SWIMMING(() -> "I am swimming"),
- WALKING(() -> "I am walking");
- Supplier<String> action;
- Type(Supplier<String> action) {
- this.action = action;
- }
- @Override
- public void move() {
- System.out.println(action.get());
- }
- }
- public static void main(String[] args) {
- Dragon flyingZalupa = new Dragon("Zalupa", 1, Type.FLYING);
- flyingZalupa.introduce();
- flyingZalupa.move();
- Dragon swimmingManda = new Dragon("Manda", 2, Type.SWIMMING);
- swimmingManda.introduce();
- swimmingManda.move();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement