Как сделать методы общими для 2 классов

192
20 марта 2018, 01:22

Здравствуйте . Помогите решить проблему. Есть четыре класса -

public class SimpleArray<T> implements Iterable<T> {
   private Object[] array;
   private int position = 0;
   private int i = 0;
    public SimpleArray(int size) {
        this.array = new Object[size];
    }
    public void add(T model) {
      array[position] = model;
      position++;
    }
    public void set(int index, T model) {
        if (index < array.length) {
            array[index] = model;
        }
    }
    public boolean delete(int index) {
        boolean temp = false;
        if (index < array.length) {
            array[index] = null;
            temp = true;
        }
        return temp;
    }
   public T get(int index) {
       return (T) array[index];
   }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {
            @Override
            public boolean hasNext() {
                boolean temp = false;
                if (array[i] != null) {
                    temp = true;
                }
                return temp;
            }
            @Override
            public T next() {
                if (array[i] == null) {
                    throw new NoSuchElementException();
                }
                return (T) array[i++];
            }
        };
    }
}

public class RoleStore  extends  AbstractStore  {
    SimpleArray<Role> roles = new SimpleArray<>(100);
    public RoleStore(SimpleArray<Role> roles) {
        this.roles = roles;
    }
}

public class UserStore  extends  AbstractStore   {

  SimpleArray<User> user = new SimpleArray<>(100);
    public UserStore(SimpleArray<User> user) {
        this.user = user;
    }

}
public abstract class AbstractStore<T>  {

   private int index = 0;
   private int count = 0;
   private int index2 =0;
   private int count2 = 0;
   private int index3 = 0;
   private int count3 = 0;


    public void add(T model) {
        simpleArray.add(model);
    }

    public boolean replace(String id, T model) {
        boolean result = false;
        while (count < 100 && !result) {
            T temp = simpleArray.get(index);
            Base temp2 = (Base) temp;
            if (temp2.getId() == id) {
                simpleArray.set(index, model);
                result = true;
            }
            index++;
            count++;
        }
        return result;
    }

    public boolean delete(String id) {
        boolean result = false;
        while (count2 < 100 && !result) {
            T temp = simpleArray.get(index2);
            Base tmp = (Base) temp;
            if (tmp.getId() == id) {
                simpleArray.delete(index2);
                result = true;
            }
            index2++;
            count2++;
        }
        return result;
    }

    public T findById(String id) {
        T result = null;
        boolean rsl = false;
        while (count3 < 100 && !rsl) {
            T temp = simpleArray.get(index3);
            Base tmp = (Base) temp;
            if (tmp.getId() == id) {
               result = temp;
              rsl = true;
            }
            index3++;
            count3++;
        }
        return result;
    }
}
public class Role extends Base  {
    protected Role(String id) {
        super(id);
    }
}
public class User extends Base {
    protected User(String id) {
        super(id);
    }
    @Override
    public String toString() {
        return "Jerry";
    }
}
public interface Store<T extends Base> {
    void add(T model);
    boolean replace(String id, T model);
    boolean delete(String id);
    T findById(String id);
}

Как сделать так, чтобы методы из класса AbstractStore я мог бы использовать при создании объекта Role и User. В основе класса RoleStore и UserStore лежит класс контейнер SipleArray.

READ ALSO
Счётчик объектов

Счётчик объектов

Как создать в конструкторе объекта счётчик, который будет вписывать в поле объекта номер его создания? То есть первый объект имеет номер...

175
Как сделать задержку в ходе выполнения цикла?

Как сделать задержку в ходе выполнения цикла?

Необходимо сделать цикл, в котором происходит выполнения действия -> задержка (допустим секунда) -> опять действие

168