2012-10-04 33 views
-2

好的,我需要創建三個構造函數作爲項目的一部分,一個默認,一個普通和一個副本。我設法創建了一個默認的構造函數,但是我不能創建通用或複製構造函數,否則我的代碼將無法編譯。下面是代碼,如果有人知道了答案:我需要創建兩個更多的構造函數,但Java不會讓我

package lab02; 

import javax.swing.JOptionPane; 

/** 
* Stores the personal details of a friend. 
* 
* @author Keith Francis(11109971) 
* @date 4-10-2012 
*/ 
public class Friend { 

    private String firstName;// stores first name 
    private String surname;// stores surname 
    private String address;// stores address 
    private int age;// stores age in years 
    private int height;// stores height in cms 
    private String hairColourString;// stores hiar colour as a string 

    private boolean colourTrue = false;// hair colour value is not valid 

    public static final int BLACK = 0; 
    public static final int BROWN = 1; 
    public static final int BLONDE = 2; 
    public static final int RED = 3; 
    public static final int GREY = 4; 

    /** 
    * Default constructor sets everything to 0 or null, depending on type. 
    */ 
    public Friend() { 

     firstName = null; 
     surname = null; 
     address = null; 
     age = 0; 
     height = 0; 
     hairColourString = null; 
    } 

    /** 
    * Allows the first name to be edited 
    * 
    * @param first 
    *   first name variable 
    */ 
    public void setFirstName(String first) { 
     firstName = first; 
    } 

    /** 
    * Retrieves first name 
    * 
    * @return first name to String 
    */ 
    public String getFirstName() { 
     return firstName; 
    } 

    /** 
    * Allows the surname to be edited 
    * 
    * @param last 
    *   creates last name variable 
    */ 
    public void setSurname(String last) { 
     surname = last; 
    } 

    /** 
    * Retrieves the surname 
    * 
    * @return last name to string 
    */ 
    public String getSurname() { 
     return surname; 
    } 

    /** 
    * Allows the address to be edited 
    * 
    * @param place 
    *   where the friend lives 
    */ 
    public void setAddress(String place) { 
     address = place; 
    } 

    /** 
    * Retrieves the address 
    * 
    * @return the address of the friend 
    */ 
    public String getAddress() { 
     return address; 
    } 

    /** 
    * Allows the age (in years) to be edited 
    * 
    * @param years 
    *   the age in years 
    */ 
    public void setAge(int years) { 
     age = years; 
    } 

    /** 
    * Retrieves the age in years 
    * 
    * @return the age in years 
    */ 
    public int getAge() { 
     return age; 
    } 

    /** 
    * Allows the height in centimetres to be edited 
    * 
    * @param h 
    *   height in centimetres 
    */ 
    public void setHeight(int h) { 
     height = h; 
    } 

    /** 
    * Retrieves the height in centimetres 
    * 
    * @return height in centimetres 
    */ 
    public int getHeight() { 
     return height; 
    } 

    /** 
    * 
    * @return String of the personal details of the friend 
    */ 
    @Override 
    public String toString() { 
     return ("First name is: " + firstName + "\nSurname is: " + surname 
       + "\nAddress is: " + address + "\nAge is :" + age 
       + "\nHeight is: " + height + "\nHair colour is: " + hairColourString); 
    } 

    /** 
    * Uses JOptionPanel to edit the friend's personal details 
    */ 
    void inputFriend() 
    { 
     //welcome message 
     JOptionPane.showMessageDialog(null,"Weclome",null,JOptionPane.PLAIN_MESSAGE); 
     //prompt to enter first name 
     String name1 = JOptionPane.showInputDialog("Enter the friend's first name."); 
     //calls setFirstName method 
     setFirstName(name1); 
     //prompt user to enter second name 
     String name2 = JOptionPane.showInputDialog("Enter the friend's surname."); 
     setSurname(name2);// calls setSurname method 

     //prompt user to enter address 
     String thisAddress = JOptionPane.showInputDialog("Enter the friend's address."); 
     setAddress(thisAddress);//calls setAddress method 
     //prompt user to enter age in years 
     String ageString = JOptionPane.showInputDialog("Enter the friend's age in years."); 
     int i = Integer.parseInt(ageString); 
     setAge(i); 
     //prompt user to enter height in centimetres 
     String heightString = JOptionPane.showInputDialog("Enter the friend's height in cenimetres."); 
     int j = Integer.parseInt(heightString); 
     setHeight(j); 
     //prompt user to enter hair colour 
     String hairColourInput = JOptionPane.showInputDialog("Select the friend's " + 
       "hair colour:\n 0 = Black\n1 = Brown\n2 = Blonde\n3 = Red\n4 = Grey"); 
     while(colourTrue != true)//if hair colour is valid 
     { 
     if(
      hairColourInput.equals("0")) 
     { hairColourString = "Black";//hair is black 
      colourTrue = true;}//entry is valid 
     else if (hairColourInput.equals("1")) 
     { hairColourString = "Brown";//hair is brown 
      colourTrue = true;}//entry is valid 
     else if (hairColourInput.equals("2")) 
     { hairColourString = "Blonde";//hair is blonde 
      colourTrue = true;}//entry is valid 
     else if (hairColourInput.equals("3")) 
     { hairColourString = "Red";//hair is red 
      colourTrue = true;}//entry is valid 
     else if (hairColourInput.equals("4")) 
     { hairColourString = "Grey";//hair is grey 
      colourTrue = true;}//entry is valid 
      else { 
       JOptionPane.showMessageDialog(null, 
         "The number entered is invalid.", "Error", 
         JOptionPane.WARNING_MESSAGE);// warns user that entry is 
                 // not valid 
       hairColourInput = JOptionPane 
         .showInputDialog("Select the friend's " + 
         "hair colour:\n 0 = Black\n1 = Brown\n2 = Blonde\n3 = Red\n4 = Grey"); 
      }// user is asked to choose again until they enter a valid number 

     } 
    } 

    /** 
    * 
    * @param args 
    *   Calls inputFriend method and prints out the final String using 
    *   JOptionPane 
    */ 
    public static void main(String[] args) { 
     Friend friend = new Friend(); 
     friend.inputFriend();// calls inputFriend method 
     JOptionPane.showMessageDialog(null, friend.toString()); // prints out details 
    } 
} 

這是我在一個拷貝構造函數的嘗試:

public Friend(Friend aFriend) { 
    this(aFriend.getFirstName(), aFriend.getSurname(), aFriend.getAddress, aFriend.getAge, aFriend.getHeight); 

和我在一般構造嘗試:

public Friend2(){ 
    public static final int BLACK = 0; 
    public static final int BROWN = 1; 
    public static final int BLONDE = 2; 
    public static final int RED = 3; 
    public static final int GREY = 4; 
} 

什麼來了當我插入構造函數時,預計會有一個類,接口或枚舉。希望有所幫助。

好吧,我已經試過拷貝構造函數是這樣的:

public Friend(Friend f) { 
this(f.getFirstName(),f.getSurname(),f.getAddress(),f.getAge(),f.getHeight()); 
} 

但我得到一個消息,說我沒有一個合適的構造函數。

更新:一般和複製構造函數正在工作。謝謝你的幫助。

+3

當您嘗試添加它們時會得到什麼錯誤? –

+3

請爲您無法工作的其他構造函數添加您嘗試的代碼。 –

+0

如果您的問題已修復,請接受答案或寫信並接受您自己的答案。 –

回答

2

你可以重載構造象下面這樣:

cons1:

public Friend() 
{ 

} 

cons2:

public Friend(int arg) 
{ 

} 

cons3:

public Friend(String s) 
{ 

} 

副本缺點:

public Friend(Friend f) 
{ 

} 
+1

複製構造函數應該帶一個'Friend'參數? –

+0

@ChrisDennett編輯,謝謝:) – PermGenError

1

Friend2()構造函數是錯誤的,因爲它實際上是一個Friend2類的構造函數。類的構造函數應該都有一個與類名相同的方法名。 (構造函數的聲明看起來像方法聲明的名稱與類相同,但沒有指定返回類型)

您的副本構造函數使用this來調用不存在的構造函數。 (this(x,y,z)被調用構造函數的3參數版本)

你想要什麼東西,看起來像下面這樣:

public class Friend 
{ 

// snip 


/** 
* Default constructor sets everything to 0 or null, depending on type. 
*/ 
public Friend() 
{ 

firstName = null; 
surname = null; 
address = null; 
age = 0; 
height = 0; 
hairColourString = null; 
} 

public Friend(Friend f) { 
    // copy constructor 
} 

public Friend(String fName, String sName, String address, int age, int height, String hair) { 
    // fill in stuff here 
} 

// snip 
} 
0

超載你的構造函數,只要確保他們有不同的簽名,它應該編譯罰款。爲了保持乾爽,通過調用this()使副本和一般構造函數使用默認構造函數。

相關問題