简体   繁体   English

如何在不同的类型ArrayList中使用相同的方法?

[英]How can I use same methods in different classes type ArrayList?

I'm doing a project that should be stored in two differents text files. 我正在做一个应该存储在两个不同文本文件中的项目。 Let say I have 2 classes Person and Activity, each with only these attributes in common: id and isActive. 假设我有2个Person和Activity类,每个类只有这些属性的共同点:id和isActive。 But there are also many that are not common. 但也有许多不常见的。

Also I have 2 classes ArrayList type: 另外我有2个类ArrayList类型:

public class RegistryPerson extends ArrayList<Person> {
     public void add(Person obj){
              ....
     }
     public boolean isDuplicate(Person obj){
         for(Person p: this){
             if(obj.equals(p)){ 
                 return true;
             }
         }
         return false;
     } 
     public Person search(int id){
              ....
     }
     public void readFile(){
         otherClass.readFile(String txtfilePerson);
     } 
     public void activate(Person obj){
              obj.setActivate;
     }
     //more methods
}

.

public class RegistryActivity extends ArrayList<Activity> {
     public void add(Activity obj){
              ....
     }
     public boolean isDuplicate(Activity obj){
         for(Activity p: this){
             if(obj.equals(p)){ 
                 return true;
             }
         }
         return false;
     } 
     public Activity search(int id){
              ....
     }
     public void readFile(){
         otherClass.readFile(String txtfileActivity);
     } 
     public void activate(Activity obj){
              obj.setActivate;
     }
     //more methods
}

Both classes have the same methods 两个类都有相同的方法

As you see both classes type ArrayList RegitryPerson and RegistryActivigy have same methods, but some used different kind of object. 如您所见,类型ArrayList RegitryPerson和RegistryActivigy具有相同的方法,但有些使用了不同类型的对象。

I just don't wanna have almost same code in differents classes. 我只是不想在不同的类中使用几乎相同的代码。 Can I use an interface or abstract class? 我可以使用接口或抽象类吗? and most important, How can implement that?. 而最重要的是,如何实现呢? Or I am complicating everything? 或者我使一切变得复杂?

Thanks. 谢谢。

You can follow Program to an interface, not implementations design principle here. 您可以将Program附加到接口,而不是实现设计原则。

Create an interface say Entity that would be implemented by both Person and Activity 创建一个interface表示将由PersonActivity实现的Entity

Entity.java Entity.java

public interface Entity {
    public Boolean equals(Entity e);
    //other common methods
}

This interface would be implemented by both Person and Activity 此接口将由PersonActivity

Person.java Person.java

public class Person implements Entity {
    ...
    @Override
    public boolean equals(Entity e) {
        ...
    }
    ...
}

Activity.java Activity.java

public class Activity implements Entity {
    ...
    @Override
    public boolean equals(Entity e) {
        ...
    }
    ...
}

Now create a parent Class Registry 现在创建一个父类注册表

Registry.java Registry.java

public class Registry extends ArrayList<Entity> {
     public void add(Entity obj){
              ....
     }
     public boolean isDuplicate(Entity obj){
         for(Entity p: this){
             if(obj.equals(p)){ 
                 return true;
             }
         }
         return false;
     } 
     public Entity search(int id){
              ....
     }
     public void readFile(){
         otherClass.readFile(String txtfilePerson);
     } 
     public void activate(Entity obj){
              obj.setActivate;
     }
     //more methods
}

Now you can extend this Registry class to both of your implementations, ie, RegistryPerson and RegistryActivity 现在,您可以将此Registry类扩展到两个实现,即RegistryPersonRegistryActivity

RegistryPerson.java RegistryPerson.java

public class RegistryPerson extends Registry {
    ..
}

RegistryActivity.java RegistryActivity.java

public class RegistryActivity extends Registry {
    ..
}

PS: All of the classes listed above many contain more common methods. PS:上面列出的所有类都包含更常用的方法。 This is just to give you basic introduction to this design principle. 这只是为了让您对此设计原理进行基本介绍。

I had the same idea as rD but used generics to create the registry. 我有与rD相同的想法,但使用泛型来创建注册表。 I also moved the List inside the class. 我还在课堂内移动了List。 Methods to read from file and add the registry could be handled by other classes. 从文件读取和添加注册表的方法可以由其他类处理。 I try to avoid inheritance and abstract classes at all costs. 我试图不惜一切代价避免继承和抽象类。 Interfaces are generics are quite powerful. 接口是泛型非常强大。

interface IdObject {
    int getId();
}

class Registry<T extends IdObject> {
    private List<T> list = new ArrayList<T>();

    public void add(T obj){
        list.add(obj);
    }

    public boolean isDuplicate(T obj){
        for(T t: list){
            if(obj.equals(t)){
                return true;
            }
        }
        return false;
    }
    public T search(int id){
        for(T t: list){
            if(t.getId() == id)){
                return t;
            }
        }
        return null;
    }
}

class Example {
    Registery<Person> personRegistery = new Registry<>();
    Registery<Activity> activityRegistery = new Registry<>();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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