如何说服Java构造函数接受包含Class实现某个接口的对象的List

时间:2015-09-16 11:56:15

标签: java list constructor interface

我目前正在对软件应用程序进行一些更改,这些更改大量使用了委托设计模式。因此,我想相应地编写我的代码。目前,我似乎无法找到让我忙碌一段时间的问题。

我当前的任务是创建某种通用的“操作类”,它将被实现某个接口的所有对象用来对这些对象执行操作。为了实现这一点,处理类将为需要更改的任何对象实例化此操作类的对象,并通过此操作类的对象执行其更改。

我的问题是我无法使用接受其类实现某个接口的对象列表的构造函数来实例化“操作类”对象。

“操作类”的构造函数如下所示(请在下面找到完整的代码示例):

public OperationClass(List<InterfaceName> listOfObjects) {
    super();
    listedObjects = listOfObjects;
}

它会像这样调用:

operationObject = new OperationClass(object.getSubObjectsList());

Eclipse会抱怨

"The constructor OperationClass(List<SubObjects>) is undefined."

这对我来说有点奇怪,因为提供List的Class实现了构造函数定义中提到的接口。有趣的是,使用一个接受这个类的单个对象的构造函数就可以了。 我的问题是:

  1. 甚至可以使用接受接口列表的构造函数吗?
  2. 如何使我的代码工作而不会失去为所有实现接口的类使用代码的能力?
  3. 注意:感谢您的反馈,编辑了代码。诀窍是将列表的数据类型更改为接口类型,并确保相应地更改Getter方法。

    完整的代码示例

    接口

    package test02_interfaceList;
    
    import java.util.List;
    
    public interface PersonI {
    
        public abstract String getFirstName();
    
        public abstract void setFirstName(String firstName);
    
        public abstract String getLastName();
    
        public abstract void setLastName(String lastName);
    
        public abstract List<PersonI> getSiblings();
    
        public abstract void setSiblings(List<PersonI> siblings);
    
    }
    

    实施界面的类

    package test02_interfaceList;
    
    import java.util.List;
    
    public class Member implements PersonI {
    
        private int memberId;
        private String firstName;
        private String lastName;
        private List<PersonI> siblings;
    
        public int getMemberId() {
            return memberId;
        }
    
        public void setMemberId(int memberId) {
            this.memberId = memberId;
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see test02_interfaceList.PersonI#getFirstName()
         */
        @Override
        public String getFirstName() {
            return firstName;
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see test02_interfaceList.PersonI#setFirstName(java.lang.String)
         */
        @Override
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see test02_interfaceList.PersonI#getLastName()
         */
        @Override
        public String getLastName() {
            return lastName;
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see test02_interfaceList.PersonI#setLastName(java.lang.String)
         */
        @Override
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see test02_interfaceList.PersonI#getSiblings()
         */
        @Override
        public List<PersonI> getSiblings() {
            return siblings;
        }
    
        @Override
        public void setSiblings(List<PersonI> siblings) {
            this.siblings = siblings;
    
        }
    }
    

    通用“操作”类

    package test02_interfaceList;
    
    import java.util.List;
    
    public class Delegate {
    
        private Member currentMember;
        private List<PersonI> associatedPeople;
        private PersonI associatedPerson;
    
        /**
         * Standard Constructor, takes list of objects which implements the PersonI
         * interface.
         * 
         * @param listOfPeople
         */
        public Delegate(List<PersonI> listOfPeople) {
            super();
            associatedPeople = listOfPeople;
        }
    
        /**
         * Alternative Constructor, takes one object implementing the PersonI
         * interface.
         * 
         * @param listOfPeople
         */
        public Delegate(PersonI person) {
            super();
            associatedPerson = person;
        }
    
        public void addToAssocPeopleList(PersonI person) {
            System.out.println("Adding " + person.getFirstName() + " to list of associated people.");
            associatedPeople.add(person);
        }
    
        public void removeFromAssocPeopleList(PersonI person) {
            System.out.println("Removing " + person.getFirstName() + " from list of associated people.");
            associatedPeople.remove(person);
        }
    
        public Member getCurrentMember() {
            return currentMember;
        }
    
        public void setCurrentMember(Member currentMember) {
            this.currentMember = currentMember;
        }
    
        public List<PersonI> getAssociatedPeople() {
            return associatedPeople;
        }
    
        public void setAssociatedPeople(List<PersonI> associatedPeople) {
            this.associatedPeople = associatedPeople;
        }
    
        public PersonI getAssociatedPerson() {
            return associatedPerson;
        }
    
        public void setAssociatedPerson(PersonI associatedPerson) {
            this.associatedPerson = associatedPerson;
        }
    
    }
    

    使用main()方法的类

    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
    
        public static void main(String[] args) {
            // Main Method
    
            Delegate member01Operations;
            List<PersonI> siblings = new ArrayList<PersonI>();
    
            Member member01 = new Member();
            member01.setMemberId(42);
            member01.setFirstName("John");
            member01.setLastName("Doe");
            member01.setSiblings(siblings);
    
            Member member02 = new Member();
            member02.setMemberId(43);
            member02.setFirstName("Joanna");
            member02.setLastName("Doe");
    
            Member member03 = new Member();
            member03.setMemberId(43);
            member03.setFirstName("James");
            member03.setLastName("Doe");
    
            // The constructor now accepts the returned list as it contains the
            // PersonI interface datatype
            member01Operations = new Delegate(member01.getSiblings());
    
            member01Operations.addToAssocPeopleList(member02);
            member01Operations.addToAssocPeopleList(member03);
    
            for (PersonI person : member01Operations.getAssociatedPeople()) {
                System.out.println(person.getFirstName());
            }
        }
    }
    

2 个答案:

答案 0 :(得分:3)

您应该了解List<SubObjects>不是List<InterfaceName>的子类,因此您无法通过预计List<SubObjects>的{​​{1}}。

您应该更改List<InterfaceName>以返回getSubObjectsList()而不是List<InterfaceName>。无论如何,通常最好在变量和方法参数中使用接口类型而不是类类型。

答案 1 :(得分:1)

我已经更改了代码。请尝试使用如下。

package test02_interfaceList;

import java.util.ArrayList;
import java.util.List;

public class Delegate {

    private Member currentMember;
    private List<PersonI<?>> associatedPeople = new ArrayList<PersonI<?>>();
    private PersonI<?> associatedPerson;

    /**
     * Standard Constructor, takes list of objects which implements the PersonI
     * interface.
     * 
     * @param listOfPeople
     */
    public Delegate(List<? extends PersonI<?>> listOfPeople) {
        super();
        for (PersonI<?> personI : listOfPeople) {
            associatedPeople.add(personI);
        }

    }

    /**
     * Alternative Constructor, takes one object implementing the PersonI
     * interface.
     * 
     * @param listOfPeople
     */
    public Delegate(PersonI<?> person) {
        super();
        associatedPerson = person;
    }

    public void addToAssocPeopleList(PersonI<?> person) {
        System.out.println("Adding " + person.getFirstName() + " to list of associated people.");
        associatedPeople.add(person);
    }

    public void removeFromAssocPeopleList(PersonI<?> person) {
        System.out.println("Removing " + person.getFirstName() + " from list of associated people.");
        associatedPeople.remove(person);
    }

    public Member getCurrentMember() {
        return currentMember;
    }

    public void setCurrentMember(Member currentMember) {
        this.currentMember = currentMember;
    }

    public List<? extends PersonI<?>> getAssociatedPeople() {
        return associatedPeople;
    }

    public void setAssociatedPeople(List<PersonI<?>> associatedPeople) {
        this.associatedPeople = associatedPeople;
    }

    public PersonI<?> getAssociatedPerson() {
        return associatedPerson;
    }

    public void setAssociatedPerson(PersonI<?> associatedPerson) {
        this.associatedPerson = associatedPerson;
    }

}

会员班:

package test02_interfaceList;

import java.util.List;

public class Member implements PersonI<Member> {

    private int memberId;
    private String firstName;
    private String lastName;
    private List<Member> siblings;

    public int getMemberId() {
        return memberId;
    }

    public void setMemberId(int memberId) {
        this.memberId = memberId;
    }

    /* (non-Javadoc)
     * @see test02_interfaceList.PersonI#getFirstName()
     */
    @Override
    public String getFirstName() {
        return firstName;
    }

    /* (non-Javadoc)
     * @see test02_interfaceList.PersonI#setFirstName(java.lang.String)
     */
    @Override
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /* (non-Javadoc)
     * @see test02_interfaceList.PersonI#getLastName()
     */
    @Override
    public String getLastName() {
        return lastName;
    }

    /* (non-Javadoc)
     * @see test02_interfaceList.PersonI#setLastName(java.lang.String)
     */
    @Override
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /* (non-Javadoc)
     * @see test02_interfaceList.PersonI#getSiblings()
     */
    @Override
    public List<Member> getSiblings() {
        return siblings;
    }

    /* (non-Javadoc)
     * @see test02_interfaceList.PersonI#setSiblings(java.util.List)
     */
    @Override
    public void setSiblings(List<Member> siblings) {
        this.siblings = siblings;
    }

}

人员界面:

package test02_interfaceList;

import java.util.List;

public interface PersonI<T extends PersonI> {

    public abstract String getFirstName();

    public abstract void setFirstName(String firstName);

    public abstract String getLastName();

    public abstract void setLastName(String lastName);

    public abstract List<T> getSiblings();

    public abstract void setSiblings(List<T> siblings);

}

主类:

package test02_interfaceList;

    public class Main {

        public static void main(String[] args) {
            // Main Method

            Delegate peopleOperations;

            Member member01 = new Member();
            member01.setMemberId(42);
            member01.setFirstName("John");
            member01.setLastName("Doe");

            Member member02 = new Member();
            member01.setMemberId(43);
            member01.setFirstName("Joanna");
            member01.setLastName("Doe");

            member01.getSiblings().add(member02);
            member02.getSiblings().add(member01);

            // This works
            peopleOperations = new Delegate(member01);

            // This doesn't ('Constructor(List<Member>) undefined') - why? Class
            // Member implements the PersonI interface!
            peopleOperations = new Delegate(member01.getSiblings());

        }

    }