Advertisement
xeritt

Принцип разделения интерфейсов (Interface Segregation)

Oct 21st, 2017
251
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ru.company.app;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. interface ICar {
  7.     int currentSpeed();
  8. }
  9.  
  10. abstract class ACar implements ICar {
  11.     private int speed;
  12.     protected int eNumber;
  13.  
  14.     @Override
  15.     public int currentSpeed() {
  16.         return speed;
  17.     }
  18. }
  19.  
  20. interface IDriver {
  21.     int pedals();
  22. }
  23.  
  24. class DriverCar extends ACar implements IDriver {
  25.  
  26.     @Override
  27.     public int pedals() {
  28.         return 0;
  29.     }
  30. }
  31.  
  32. interface IInspector {
  33.     int engineNumber();
  34. }
  35.  
  36. class InspectorCar extends ACar implements IInspector {
  37.  
  38.     @Override
  39.     public int engineNumber() {
  40.         return eNumber;
  41.     }
  42.  
  43. }
  44.  
  45. class PoliceCar extends InspectorCar implements IDriver {
  46.  
  47.     @Override
  48.     public int pedals() {
  49.         return 0;
  50.     }
  51.  
  52. }
  53.  
  54. public class Main {
  55.     public static void main(String[] args) {
  56.         List <ACar> cars = new ArrayList <>();
  57.         cars.add(new DriverCar());
  58.         cars.add(new InspectorCar());
  59.         cars.add(new PoliceCar());
  60.     }
  61. }
Advertisement
RAW Paste Data Copied
Advertisement