Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. package com.javarush.task.task24.task2410;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.List;
  5.  
  6. /*
  7. Рефакторинг, анонимные классы
  8. */
  9. public class Solution {
  10. public static List<Iterator> iterators = new LinkedList<>();
  11.  
  12. private int countItems;
  13.  
  14. public Iterator getIterator(final String name) {
  15. class LocalIterator implements Iterator {
  16. public LocalIterator() {
  17. countItems++;
  18. System.out.println(name + " item " + countItems);
  19. }
  20.  
  21. public Iterator next() {
  22. return new LocalIterator();
  23. }
  24. }
  25. return new Iterator()
  26. {
  27. {
  28. countItems++;
  29. System.out.println(name + " item " + countItems);
  30. }
  31. @Override
  32. public Iterator next() {
  33. return new LocalIterator();
  34. }
  35. };
  36.  
  37. }
  38.  
  39. public static void main(String[] args) {
  40. Solution solution = new Solution();
  41.  
  42. Iterator iterator = solution.getIterator("iterator");
  43. for (int i = 1; i < 5; i++) {
  44. iterators.add(iterator.next());
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement