【问题标题】:returning two different beans in same list在同一个列表中返回两个不同的 bean
【发布时间】:2012-10-02 06:19:23
【问题描述】:

我使用 MVC 在 Java 中开发 Web 项目。
我的问题是我需要从一个类中返回三个不同的 bean。所有三个 bean 都有多个对象,所以我现在将每个相同的 bean 对象添加到一个列表中并返回三个不同的列表。
好的,为了更清楚,我需要从存储 cmets 的表中检索所有内容。所以所有评论文本都存储在一个名为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;
  }

 }

【问题讨论】:

    标签: java interface polymorphism


    【解决方案1】:

    如果不同的 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());
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 2014-01-30
      • 2015-02-18
      • 1970-01-01
      相关资源
      最近更新 更多