Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. package animal;
  2.  
  3. abstract class Animal implements Node{
  4.     public void speak(String s){
  5.         System.out.println(s);
  6.     }
  7. }
  8.  
  9. class Snake extends Animal{
  10.     public void speak(String s){
  11.         System.out.println(s+"...sssss");
  12.     }
  13.  
  14.     @Override
  15.     public void accept(Visitor visitor) {
  16.         visitor.visit(this);
  17.        
  18.     }
  19. }
  20.  
  21. class Monkey extends Animal{
  22.     public void speak(String s){
  23.         System.out.println(s + "  me like banana!");
  24.     }
  25.  
  26.     @Override
  27.     public void accept(Visitor visitor) {
  28.         visitor.visit(this);
  29.        
  30.     }
  31. }
  32.  
  33. interface Visitor{
  34.     public void visit(Monkey monkey);
  35.     public void visit(Snake snake);
  36. }
  37.  
  38. interface Node {
  39.     public void accept(Visitor visitor);
  40. }
  41.  
  42. class FightMonkey implements Visitor {
  43.  
  44.     @Override
  45.     public void visit(Monkey monkey) {
  46.         System.out.println("Even fight");
  47.        
  48.     }
  49.  
  50.     @Override
  51.     public void visit(Snake snake) {
  52.         System.out.println("Snake wins");
  53.        
  54.     }
  55.    
  56. }
  57.  
  58. public class AnimalTest {
  59.        
  60.     public static void fight(Animal a, Animal b){
  61.         System.out.println("boring");
  62.     }
  63.    
  64.     public static void fight(Monkey m, Snake s) {
  65.         System.out.println("Monkey run");
  66.     }
  67.     public static void fight(Snake s, Monkey m) {
  68.         System.out.println("Snake kills");
  69.     }
  70.    
  71.     public static void main(String[] args) {
  72.         Animal snake = new Snake();
  73.         Animal monkey = new Monkey();
  74.         AnimalTest.fight(snake, monkey);
  75.        
  76.         Visitor monkeyFighter = new FightMonkey();
  77.        
  78.         snake.accept(monkeyFighter);
  79.        
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement