Advertisement
jaVer404

level14.lesson08.home08

Jun 23rd, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. package com.javarush.test.level14.lesson08.home08;
  2.  
  3. /* Исправление ошибок
  4. 1. Подумать, как связаны интерфейсы Swimable(способен плавать) и Walkable(способен ходить) с классом OceanAnimal(животное океана).
  5. 2. Расставить правильно наследование интерфейсов и класса OceanAnimal.
  6. 3. Подумать, как могут быть связаны классы  Orca(Косатка), Whale(Кит), Otter(Выдра) с классом OceanAnimal.
  7. 4. Расставить правильно наследование между классами Orca, Whale, Otter и классом OceanAnimal.
  8. 5. Подумать, какой класс должен реализовать интерфейс Walkable и добавить интерфей этому классу.
  9. 6. Подумать, какое животное еще не умеет плавать и добавить ему интерфейс Swimable.
  10. */
  11.  
  12. public class Solution
  13. {
  14.     public static void main(String[] args)
  15.     {
  16.  
  17. /*        Swimable animal = new Orca();
  18.         animal.swim();
  19.         animal = new Whale();
  20.         animal.swim();
  21.         animal = new Otter();
  22.         animal.swim();*/
  23.  
  24.     }
  25.  
  26.     public static void test(Swimable animal)
  27.     {
  28.         animal.swim();
  29.     }
  30.  
  31.     static interface Walkable extends Swimable
  32.     {
  33.         void walk();
  34.     }
  35.  
  36.     static interface Swimable
  37.     {
  38.         void swim();
  39.     }
  40.  
  41.     static abstract class OceanAnimal implements Swimable
  42.     {
  43.         public void swim()
  44.         {
  45.             OceanAnimal currentAnimal = (OceanAnimal) getCurrentAnimal();
  46.             currentAnimal.swimming();
  47.         }
  48.  
  49.         private void swimming()
  50.         {
  51.             System.out.println(getCurrentAnimal().getClass().getSimpleName() + " is swimming");
  52.         }
  53.  
  54.         abstract Swimable getCurrentAnimal();
  55.     }
  56.  
  57.     static class Orca extends Whale
  58.     {
  59.  
  60.     }
  61.  
  62.     static class Whale extends OceanAnimal
  63.     {
  64.         Swimable getCurrentAnimal() {
  65.             return this;
  66.         }
  67.  
  68.     }
  69.  
  70.     static class Otter implements Walkable
  71.     {
  72.         public void swim(){};
  73.  
  74.  
  75.         public void walk(){};
  76.  
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement