a.要求修改编写一个程序(class Person),添加以下内容
  i   设置姓
  ii  设置名
  iii 中间名字
  iv  检测所给姓是否和该人姓一致
  v   检测所给名是否和该人姓一致
  vi  检测所给中间名是否和该人姓一致b.添加方法 equals 如果2个对象姓名一致 返回ture c.添加方法 makecopy 可以复制Person中的对象到另一个 Person 的对象中d. 添加方法getCopy 可以创造和返回已经复制到另一个Person中的对象的地址e. 添加copy 构造器f. 写出class Person 方法的定义以操作该类 (write the definitions of the methods of the class Person to implement the operations for this class)g. 编写一个测试程序以测试Person类的不同功能
下面是我编写的一部分 有些部分不是太理解  还有对测试程序也没什么头绪 希望大哥们帮忙public class Person {

private String firstName; //store the first name
private String middleName; //store the middle name
    private String lastName;  //store the last name
    // Default constructor; - update (nib)
    public Person() {
      firstName = "";
      middleName = "";
         lastName = "";    }    // Constructor with parameters
    public Person(String first, String middle, String last)
    {
     setName(first,middle,last);
    }    // copy constructor 3e                        如何复制?
    public Person(Person anotherPerson)
    {
     firstName=anotherPerson.firstName;
     middleName= anotherPerson.middleName;
     lastName= anotherPerson.lastName;
    }    // Method to output the first name, middle name, and last name
    // in the form firstName middleName lastName
    public String toString()
    {
        return (firstName + " " + middleName + " " + lastName);
    }    // Override method to set firstName, middleName, and lastName
    public void setName(String first, String middle, String last)
    {
      firstName = first;
         middleName = middle;
         lastName = last;
    }    // “setter” methods 3ai, 3aii, 3aiii.  All created in class on 
    // May 6    public void setFirstName(String first)
    {
     firstName= first;
    }    public void setMiddleName(String middle)
    {
     middleName= middle;
    }    public void setLastName(String last)
    {
     lastName= last;
    }
    // “getter” methods – first two are provided
    public String getFirstName()
    {
     return firstName;
    }    public String getMiddleName()
    {
     return middleName;
    }    public String getLastName()
    {
     return lastName; 
    }
    boolean equalLastName(String last) // 3aiv
    {
return(lastName==last);
    }    boolean equalFirstName(String first) // 3av
    {
     return(firstName==first);
    }    boolean equalMiddleName(String middle) // 3avi
    {
     return(middleName==middle);
    }
    public void makeCopy(Person otherPerson) // 3c  不知道这怎么写
    {
     otherPerson.firstName=firstName;
     otherPerson.middleName=middleName;
     otherPerson.lastName=lastName;
    }    // Method to return a copy 3d
    public Person getCopy()                     //这边不知道怎么编
    {
     return ;    
    }    // Returns true if two objects contain the same first 
    // and last name
    public boolean equals(Person otherPerson) // 3b      
    {
     return( firstName==otherPerson.firstName&& lastName==otherPerson.lastName);
    }}

解决方案 »

  1.   

    哦~~
    好乱。    boolean equalLastName(String last) // 3aiv 
        { 
    return(lastName==last); 
        }     boolean equalFirstName(String first) // 3av 
        { 
        return(firstName==first); 
        }     boolean equalMiddleName(String middle) // 3avi 
        { 
        return(middleName==middle); 
        } 这段对吗?最差也应该是 this.middleName==middle 可是,这里是应该使用equals()的。
      public void makeCopy(Person otherPerson) // 3c  不知道这怎么写 
        { 
        otherPerson.firstName=firstName; 
        otherPerson.middleName=middleName; 
        otherPerson.lastName=lastName; 
        } 这个反了。应该是根据传入的复制新的,类似C++的拷贝构造函数,做一个浅拷贝。
    你这成了更改传入对象了……
    好霸道,不管谁进来,出去的时候都要和这个人变得一样……
    public Person getCopy()                    //这边不知道怎么编 
        { 
        return ;    
        } 
    这个的意图应该是获取一份当前对象的副本吧。那么
    public Person getCopy()                   
        { 
        Person p = new Person();
        p.makeCopy(this);
        return p;    
        } 
    还有你最后改写的equals方法,把==都改成.equals来判断。
      

  2.   


    public static boolean equalsname(Person p1,Person p2) {
    return (p1.firstname+p1.lastname+p1.midname).equals(p2.firstname+p2.lastname+p2.midname);
    }