Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Objects;
- abstract class Type {
- public abstract String getType();
- }
- class MyInt extends Type {
- private int x;
- public MyInt(int x) {
- this.x = x;
- }
- @Override
- public String getType() {
- return "Type: MyInt";
- }
- @Override
- public String toString() {
- return String.valueOf(x);
- }
- }
- class MyString extends Type {
- private String string;
- public MyString(String string) {
- this.string = string;
- }
- @Override
- public String getType() {
- return "Type: String";
- }
- @Override
- public String toString() {
- return string;
- }
- }
- class MyColletion extends Type {
- List list;
- public MyColletion() {
- list = new ArrayList();
- }
- @Override
- public String getType() {
- return "Type: Collection";
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- MyColletion that = (MyColletion) o;
- return Objects.equals(list, that.list);
- }
- public void add(Object o) {
- list.add(o);
- }
- @Override
- public String toString() {
- String s = "[";
- Iterator iterator = list.iterator();
- while (iterator.hasNext()) {
- s += iterator.next().toString() + " ";
- }
- return s + "]";
- }
- }
- public class Main {
- public static void main(String[] args) {
- MyColletion myColletion = new MyColletion();
- myColletion.add(new MyInt(7));
- myColletion.add(new MyInt(4));
- myColletion.add(new MyString("Eu"));
- myColletion.add(new MyInt(12));
- MyColletion myColletion1 = new MyColletion();
- myColletion1.add(new MyInt(2));
- myColletion1.add(new MyInt(8));
- myColletion.add(myColletion1);
- System.out.println(myColletion);
- System.out.println(myColletion.equals(myColletion1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement