在同一列表中返回两个不同的bean

时间:2012-10-02 06:19:24

标签: java interface polymorphism

我使用MVC在Java中开发Web项目 我的问题是我需要从类中返回三个不同的bean。所有三个bean都有多个对象,所以我现在正在列表中添加每个相同的bean对象并返回三个不同的列表 好的,为了更清楚,我需要从存储注释的表中检索所有内容。因此,所有注释文本都存储在一个名为comment的bean中,并添加到名为listcomment的列表中。发表评论的成员的名字被添加到名为member的另一个bean中,并且再次将这些名称添加到名为listmember的列表中 那么有没有什么方法可以将这两个bean添加到同一个列表中呢?

public class TeleCommentView {

int qid;
TeleComment comment;
TeleHospital hospital;
doctorperson doctor;

ConnectionFile connection = new ConnectionFile();
List<TeleComment> listcomment = new ArrayList<TeleComment>();
List<doctorperson> listdoctor = new ArrayList<doctorperson>();
List<TeleHospital> listhospital = new ArrayList<TeleHospital>();




public TeleCommentView(int qid)
{
    this.qid = qid;
    process();
}
public void process()
{
    int count=0;
    try
    {
    Connection con = connection.connectionfile();
    PreparedStatement  pstmt = con.prepareStatement("select TeleHospital.HospitalName,DoctorDetail.Name,TeleComment.Comment,TeleComment.SDate from"
                                                     + "( (TeleComment left join TeleHospital on TeleHospital.HospitalId=TeleComment.Hid) "
                                                    + "left join DoctorDetail on DoctorDetail.DoctorId = TeleComment.Did) "
                                                    + "where TeleComment.Qid=?");
    ResultSet rs = pstmt.executeQuery();

    while(rs.next())
    {
        comment = new TeleComment();

        comment.setComment(rs.getString("Comment"));
        comment.setSdate(rs.getDate("SDate"));

        listcomment.add(count,comment) ;

        /******End of comment**************/

        //Add doctor or hospital name as required

        doctor = new doctorperson();
        hospital = new TeleHospital();

        if(rs.getString("HospitalName").equals(null))
            {
                doctor.setName(rs.getString("Name"));
                listdoctor.add(count,doctor);
            }
            else
                {
                    hospital.setHospitalname(rs.getString("HospitalName"));
                    listhospital.add(count,hospital);
                 }
        count++;
         }
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }

}

  public List getCommentList()
  {
   return listcomment;
  }

 public List getDoctorList()
  {
   return listdoctor;
  }

 public List getHospitalList()
  {
   return listhospital;
  }

 }

1 个答案:

答案 0 :(得分:2)

如果不同的bean都包含某种方法(或方法),则可以创建interface并使每个bean实现它。

以下是一个类似的问题:

Treating different objects the same

愚蠢的示例代码:

interface CommentItem {
    public String getComment();
}


class ModeratorComment implements CommentItem {
    public String getComment() {
        return "Comment from moderator";
    }
    // other moderator-specific code...
}



class StudentComment implements CommentItem {
    public String getComment() {
        return "Comment from student";
    }
    // other student-specific code...
}


class CommentContainer {

    private List<CommentItem> commentList;

    public List<CommentItem> getCommentList() {
        return commentList;
    }

    public void addComment(CommentItem someComment) {
        commentList.add(someComment);
    }
}


class TestIt() {

    public static void main(String[] args) {

        StudentComment sc = new StudentComment();
        ModeratorComment mc = new ModeratorComment();

        CommentContainer comments = CommentContainerFactory.createCommentContainer();
        comments.add(sc);
        comments.add(mc);

        for (CommentItem ci : comments.getCommentList()) {
            System.out.println(ci.getComment());
        }

    }

}