Advertisement
STANAANDREY

lect10 poo

Dec 9th, 2023
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import java.util.Objects;
  5.  
  6. abstract class Type {
  7.     public abstract String getType();
  8. }
  9.  
  10. class MyInt extends Type {
  11.     private int x;
  12.  
  13.     public MyInt(int x) {
  14.         this.x = x;
  15.     }
  16.  
  17.     @Override
  18.     public String getType() {
  19.         return "Type: MyInt";
  20.     }
  21.  
  22.     @Override
  23.     public String toString() {
  24.         return String.valueOf(x);
  25.     }
  26. }
  27.  
  28. class MyString extends Type {
  29.     private String string;
  30.  
  31.     public MyString(String string) {
  32.         this.string = string;
  33.     }
  34.  
  35.     @Override
  36.     public String getType() {
  37.         return "Type: String";
  38.     }
  39.  
  40.     @Override
  41.     public String toString() {
  42.         return string;
  43.     }
  44. }
  45.  
  46. class MyColletion extends Type {
  47.     List list;
  48.  
  49.     public MyColletion() {
  50.         list = new ArrayList();
  51.     }
  52.  
  53.     @Override
  54.     public String getType() {
  55.         return "Type: Collection";
  56.     }
  57.  
  58.     @Override
  59.     public boolean equals(Object o) {
  60.         if (this == o) return true;
  61.         if (o == null || getClass() != o.getClass()) return false;
  62.  
  63.         MyColletion that = (MyColletion) o;
  64.  
  65.         return Objects.equals(list, that.list);
  66.     }
  67.  
  68.     public void add(Object o) {
  69.         list.add(o);
  70.     }
  71.  
  72.     @Override
  73.     public String toString() {
  74.         String s = "[";
  75.         Iterator iterator = list.iterator();
  76.         while (iterator.hasNext()) {
  77.             s += iterator.next().toString() + " ";
  78.         }
  79.         return s + "]";
  80.     }
  81. }
  82.  
  83. public class Main {
  84.     public static void main(String[] args) {
  85.         MyColletion myColletion = new MyColletion();
  86.         myColletion.add(new MyInt(7));
  87.         myColletion.add(new MyInt(4));
  88.         myColletion.add(new MyString("Eu"));
  89.         myColletion.add(new MyInt(12));
  90.  
  91.         MyColletion myColletion1 = new MyColletion();
  92.         myColletion1.add(new MyInt(2));
  93.         myColletion1.add(new MyInt(8));
  94.         myColletion.add(myColletion1);
  95.  
  96.         System.out.println(myColletion);
  97.         System.out.println(myColletion.equals(myColletion1));
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement