Advertisement
VirKato

Animal interface

Jan 16th, 2023 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | Source Code | 0 0
  1. public class InterfaceExample {
  2.     public static void main(String[] args) {
  3.         Dog dog = new Dog();
  4.         Cat cat = new Cat();
  5.         Mouse mouse = new Mouse();
  6.  
  7.         System.out.println(mouse.catchThe(cat) ? "мышка поймала кошку" : "мышка не поймала кошку");
  8.         System.out.println(dog.say());
  9.         System.out.println(dog.catchThe(cat) ? "собака поймала кошку" : "собака не поймала кошку");
  10.         System.out.println(cat.say());
  11.         System.out.println(cat.catchThe(mouse) ? "кошка поймала мышку" : "кошка не поймала мышку");
  12.         System.out.println(mouse.say());
  13.     }
  14. }
  15.  
  16.  
  17. interface Animal {
  18.     /***
  19.      * обязывает предоставить состояние из объекта
  20.      * @return голос
  21.      */
  22.     String say();
  23.  
  24.     /***
  25.      * обязывает предоставить состояние из объекта
  26.      * @return сила атаки животного
  27.      */
  28.     int getAttackForce();
  29.  
  30.     /***
  31.      * обязывает хранить состояние в объекте
  32.      * @return пойманное животное
  33.      */
  34.     default Animal getCaught() {
  35.         return null;
  36.     }
  37.  
  38.     /***
  39.      * сравнение состояний объектов
  40.      * @param other атакуемое животное
  41.      * @return true - слабее атакуемого
  42.      */
  43.     default boolean isWeakerThan(Animal other) {
  44.         return getAttackForce() < other.getAttackForce();
  45.     }
  46.  
  47.     /***
  48.      * иной результат сравнения состояний
  49.      * @param other атакуемое животное
  50.      * @return true - сильнее атакуемого
  51.      */
  52.     default boolean catchThe(Animal other) {
  53.         return !isWeakerThan(other);
  54.     }
  55. }
  56.  
  57.  
  58. class Dog implements Animal {
  59.     /***
  60.      * пойманное животное
  61.      */
  62.     private Animal caught;
  63.  
  64.     @Override
  65.     public String say() {
  66.         return "Гав-гав";
  67.     }
  68.  
  69.     @Override
  70.     public int getAttackForce() {
  71.         return 5;
  72.     }
  73.  
  74.     /***
  75.      * изменение состояния по результатам сравнения
  76.      * @param other атакуемое животное
  77.      * @return true - животное поймано
  78.      */
  79.     @Override
  80.     public boolean catchThe(Animal other) {
  81.         if (Animal.super.catchThe(other)) {
  82.             caught = other;
  83.             return true;
  84.         }
  85.         return false;
  86.     }
  87.  
  88.     @Override
  89.     public Animal getCaught() {
  90.         return caught;
  91.     }
  92. }
  93.  
  94.  
  95. class Cat implements Animal {
  96.     private Animal caught;
  97.  
  98.     @Override
  99.     public String say() {
  100.         return "Мяу-мяу";
  101.     }
  102.  
  103.     @Override
  104.     public int getAttackForce() {
  105.         return 3;
  106.     }
  107.  
  108.     @Override
  109.     public boolean catchThe(Animal other) {
  110.         if (Animal.super.catchThe(other)) {
  111.             caught = other;
  112.             return true;
  113.         }
  114.         return false;
  115.     }
  116.  
  117.     @Override
  118.     public Animal getCaught() {
  119.         return caught;
  120.     }
  121. }
  122.  
  123.  
  124. class Mouse implements Animal {
  125.  
  126.     @Override
  127.     public String say() {
  128.         return "Пи-пи";
  129.     }
  130.  
  131.     @Override
  132.     public int getAttackForce() {
  133.         return 1;
  134.     }
  135.  
  136.     @Override
  137.     public boolean catchThe(Animal other) {
  138.         // other съеден, поэтому getCaught ничего не вернёт ))
  139.         return Animal.super.catchThe(other);
  140.     }
  141. }
  142.  
  143.  
  144. class Insect implements Animal {
  145.  
  146.     @Override
  147.     public String say() {
  148.         return "";
  149.     }
  150.  
  151.     @Override
  152.     public int getAttackForce() {
  153.         return 0;
  154.     }
  155. }
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement