简体   繁体   中英

Generics class cast in Java

Hi I have very similar classes (Cars, Clients, Reservations). In those classes i have the same functions like ( add, delete, getItem, getAll and sort).

public class Clients {  
    List<Client> persons = new ArrayList<Client>(); 

    public void add (Client k) {
        persons.add(k);
    }

    public void delete (Client k) {
        persons.remove(k);
    }

    public Client getKlienet(int id) {
       for ( Client k: persons) {
           if ( k.getId() == id)
               return k;
       }            

       return null;
    }

    public List<Client> getAllClients() {
        List<Client> temp = new ArrayList<Client>();                
        temp.addAll(persons);   

        return temp;
    }

    public List<Client> sortujKontakty() {
        Collections.sort(persons);          

        return persons;
    }

I'd like to do with these classes one generic class. But i have a lot of problems.. First with function getId() (this function simply returns value of Id), and with sort() method. Compareto() methods are different for other classes.

So, i done interface:

public interface ManagerInterface <T> { 
    public void    add (T t );
    public void    delete ( T t);
    public T       getRecord(int id);
    public List<T> getAll();
    public List<T> sort();  
}

And class:

public class RecordManager<T> implements RecordManagerInterface <T> {
    private T id;

    public T getId() {
        return id;
    }

    public ArrayList<T> record = new ArrayList<T>();

    @Override
    public void add(T t) {
        record.add(t);  
    }

    @Override
    public void delete(T t) {
        record.remove(t);
    }

    @Override
    public T getRecord(int id) {
        for ( T k: record) {
            if ( ((Client) k).getId() == id)
                return k;
            else if ( ((Person) k).getId() == id)
                return k;
            else if ( ((Reservation) k).getId() == id)
                return k;               
        }

        return null;
    }

    @Override
    public List<T> getAll() {
        List<T> temp = new ArrayList<T>();              

        temp.addAll( record);   

        return temp;        
    }

    @Override
    public  List<T> sort() {
        Collections.sort(record);           

        return record;      

    }

Please for any help.

if you have similar classes, try to create common interface for them,

ie

interface  HasId { 
int getId();
}

then all what you need is make your classes to implement it, and change your RecordManager to be

public class RecordManager<T extends HasId> implements RecordManagerInterface <T> {
...
}

that means your T has to implement your interface, so your get getRecord method will be much simpler

 public T getRecord(int id) {
        for ( T k: record) {
           if (k.getId() == id)
               return k;
        }

        return null;
    }

EDIT Lets say your common interface will be

 interface CommonInterface<T> extends Comparable<T> {
 public int getId(); 
}

then definition of your class will be, ie

Person implements CommonInterface<Person> {
...
}

your RecordManager

 class RecordManager<T extends CommonInterface<T>> implements
            RecordManagerInterface<T>{
}

and that should fix all your problems with sort

This is what is called "coding to the interface". Then when you instantiate the objects, make your reference to the interface and the object to the specific class you want to instantiate.

Ex:

Interface example = new Class();

Then use example.getMethod(), example.setMethod(). This will actually call the Class.getMethod() if it has one, as per inheritance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM