Advertisement
Guest User

Untitled

a guest
Jul 15th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. import java.util.function.Supplier;
  2.  
  3. public class Main {
  4.  
  5.     public interface Movable {
  6.         void move();
  7.     }
  8.  
  9.     public static class Dragon implements Movable {
  10.         String name;
  11.         int size;
  12.         Type type;
  13.  
  14.         public Dragon(String name, int size, Type type) {
  15.             this.name = name;
  16.             this.size = size;
  17.             this.type = type;
  18.         }
  19.  
  20.         @Override
  21.         public void move() {
  22.             type.move();
  23.         }
  24.  
  25.         public void introduce() {
  26.             System.out.println(String.format("My name is %s, my size is %s", name, size));
  27.         }
  28.     }
  29.  
  30.     public enum Type implements Movable {
  31.         FLYING(() -> "I am flying"),
  32.         SWIMMING(() -> "I am swimming"),
  33.         WALKING(() -> "I am walking");
  34.  
  35.         Supplier<String> action;
  36.  
  37.         Type(Supplier<String> action) {
  38.             this.action = action;
  39.         }
  40.  
  41.         @Override
  42.         public void move() {
  43.             System.out.println(action.get());
  44.         }
  45.     }
  46.  
  47.     public static void main(String[] args) {
  48.         Dragon flyingZalupa = new Dragon("Zalupa", 1, Type.FLYING);
  49.         flyingZalupa.introduce();
  50.         flyingZalupa.move();
  51.  
  52.         Dragon swimmingManda = new Dragon("Manda", 2, Type.SWIMMING);
  53.         swimmingManda.introduce();
  54.         swimmingManda.move();
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement