类构造函数的两种方法

时间:2016-11-15 18:11:39

标签: java

我有一个名为User的主类,如下所示

package edu.uoc.dpoo;
import java.util.ArrayList;
import java.util.List;

public class User {

   private String username;
   private String password;
   private String fullName;

   private Platform platform;

   private List<Message> inBox;
   private List<Message> outBox;


   public User(String username, String password, String fullName) {

    this.username = username;
    this.password = password;
    this.fullName = fullName;

    inBox = new ArrayList<Message>();
    outBox = new ArrayList<Message>();
  }

    public User() {
       username="";
       password="";
       fullName="";
    }




public String getUserName() {
    return username;
}

public String getPassword() {
    return password;
}

public String getFullName() {
    return fullName;
}

public void setUserName(String username) {
    this.username = username;
}

public void setPassword(String password) {
    this.password = password;
}

public void setFullName(String fullname) {
    this.fullName = fullname;
}


public boolean checkPassword(String password){

    if (password.equals(this.password)){

        return true;
    }else
    {
            return false;
            }        
}

public List<Message> getMessages() {
    return null;
}
public Organizer asOrganizer(){
      Organizer ObjetoO =  new Organizer(username,password,fullName);
    return  ObjetoO;
}



public Participant asParticipant(){
    Participant ObjetoP =  new Participant(username,password,fullName);
    return  ObjetoP;

}



public Message sendMessage(String to,String Subject,String message){
    return null;
}

public String toString () {
    return null;
}

@Override
public boolean equals (Object o) {
    boolean prueba = false;

    if ((o != null) && (o instanceof User)) {
        User p = (User) o;
        if (this.username == null && this.password == null && this.fullName == null) {
            prueba = (p.username == null);
                            prueba = (p.password == null);
                            prueba = (p.fullName == null);
        }
        else {
            prueba =  this.username.equals(p.username);
                            prueba =  this.password.equals(p.password);
                            prueba =  this.fullName.equals(p.fullName);
        }
    }

    return prueba;
}

    public List<Competition> myCompetitions(){
        return null;
    }


 User(User u1) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}


}

从User继承的两个类是Organizer和Participant,实现如下

组织者

package edu.uoc.dpoo;

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

public class Organizer extends User {

    private List<Competition> competitions;



    public Organizer(String username, String password, String fullName) {
        super(username, password, fullName);
                competitions = new ArrayList<Competition>();

    }

    public boolean removeSubmission(Submission submission){
        return true;

    }

    public boolean sendMessage(Competition competition, String subject, String message){
        return true;

    }

    public Competition newCompetition(String tittle,float target){
        return null;

    }
    public void closeCompetition(Competition competition){

    }

}


   Participant 

    package edu.uoc.dpoo;

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

    public class Participant extends User {

    private List<Competition> competitions;
       private List<Submission> submission;



    public Participant(String username, String password, String fullName) {
        super(username, password, fullName);
                competitions = new ArrayList<Competition>();
                submission = new ArrayList<Submission>();
    }

    public Submission submitPrediction(Competition competition, float prediction){
        return null;
    }
    public List<Submission> getSubmissions(){
        return null;
    }

    void asParticipant(Submission aThis) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

我要求实现User类的asOrganizer和asParticipant方法。  这些方法必须创建特定类的对象,并使用User对象的数据初始化父类。要执行此任务,系统会提示您实现User类的复制构造函数。

我在两个问我的方法中创建了对象,但我不应该在构造函数中做得好,因为junit让我失望

我添加了失败的junit

   @Test
    public void userCopyConstructor() {        
    Platform platform = new Platform();
    User u1=platform.registerUser(username1, password1, fullName1);

    // User is not NULL
    assertNotNull(u1);

    // Get a participant object from user object
    User u2 = new User(u1);        
    assertNotNull(u2);
    assertEquals(u1, u2);        
}

此时我的问题出现了

   // Get a participant object from user object
    User u2 = new User(u1);        
    assertNotNull(u2);
    assertEquals(u1, u2);     

1 个答案:

答案 0 :(得分:0)

 public User(User Prueba){
 this(Prueba.getUserName(),Prueba.getPassword(),Prueba.getFullName());

}

感谢他们给我的答案。 如果没有你,那就更难了