2013-10-19 71 views
0

我想讓我的代碼創建我需要創建一個手機對象的新構造函數對象。我嘗試命名構造函數字段來創建對象。Java錯誤,無法找到符號 - 類字符串

更新:我已經修改了stringString,但現在當我更新我的錯誤:

error: constructor Mobile(java.lang.String,int,int,java.lang.String,int,java.lang.String) is already defined in class Mobile 

這個錯誤在頁面的底部出現在:

public Mobile(String MobilephoneType, int Mobilescreensize, int Mobilememorycardcapacity, String newserviceprovider, int Mobilecameraresolution, 
     String MobileGPS) { 

這個錯誤是什麼意思?

至今代碼:

/** 
* to write a simple java class Mobile that models a mobile phone. 
* 
* @author (Lewis Burte-Clarke) 
* @version (14/10/13) 
*/ 
public class Mobile 

{ 
    // type of phone 
    private String phonetype; 
    // size of screen in inches 
    private int screensize; 
    // menory card capacity 
    private int memorycardcapacity; 
    // name of present service provider 
    private String serviceprovider; 
    // type of contract with service provider 
    private int typeofcontract; 
    // camera resolution in megapixels 
    private int cameraresolution; 
    // the percentage of charge left on the phone 
    private int checkcharge; 
    // wether the phone has GPS or not 
    private String GPS; 
    // instance variables - replace the example below with your own 
    private int x; 

    // The constructor method 

    public Mobile(String mobilePhoneType, int mobileScreenSize, 
      int mobileMemoryCardCapacity, String newserviceprovider, int mobileCameraResolution, 
      String mobileGPS) { 
     this.phonetype = mobilePhonetype; 
     this.screensize = mobileScreensize; 
     this.memorycardcapacity = mobileMemoryCardCapacity; 
     this.cameraresolution = mobileCameraResolution; 
     this.GPS = mobileGPS; 

     // you do not use this ones during instantiation,you can remove them if you do not need or assign them some default values 
     this.serviceprovider = newserviceprovider; 
     this.typeofcontract = 12; 
     this.checkcharge = checkcharge; 

    } 

    // A method to display the state of the object to the screen 
    public void displayMobileDetails() { 
     System.out.println("phonetype: " + phonetype); 
     System.out.println("screensize: " + screensize); 
     System.out.println("memorycardcapacity: " + memorycardcapacity); 
     System.out.println("cameraresolution: " + cameraresolution); 
     System.out.println("GPS: " + GPS); 
     System.out.println("serviceprovider: " + serviceprovider); 
     System.out.println("typeofcontract: " + typeofcontract); 

    } 

    public Mobile(String MobilephoneType, int Mobilescreensize, int Mobilememorycardcapacity, String newserviceprovider, int Mobilecameraresolution, 
      String MobileGPS) { 
     this.phonetype = Mobilephonetype; 
     this.screensize = 3; 
     this.memorycardcapacity = 4; 
     this.cameraresolution = 8; 
     this.GPS = GPS; 
     this.serviceprovider = newserviceprovider; 
     this.typeofcontract = 12; 
     this.checkcharge = checkcharge; 


    } 

} 

class mymobile { 
    public static void main(String[] args) { 
     Mobile Samsung = new Mobile("Samsung", "3", "4", "8", 
       "GPS"); 
     Mobile Blackberry = new Mobile("Blackberry", "3.", "4", 
       "8", "GPS"); 
     Samsung.displayMobileDetails(); 
     Blackberry.displayMobileDetails(); 
    } 
} 
+1

錯誤 「構造移動(java.lang.String中,INT,INT,java.lang.String中,INT,java.lang.String中)中的一類移動已經定義」 是指正是它說。你有兩個具有相同簽名的構造函數。只要刪除一個重複的構造函數。 – pburka

+0

如果您有新問題,請提出一個新問題。不要編輯一個問題將其轉化爲另一個問題。否則,堆棧溢出會遇到一個問題,以及一些與它不匹配的答案。 ...編譯器錯誤意味着它說的是什麼。你已經定義了一個構造函數Mobile(String,int,int,String,int,String)'當你已經使用這些參數GOT構造函數時。 –

回答

4

Java是區分大小寫的。使用大寫SString

private String phonetype; 

對象名稱(又名引用類型)始終以大寫字母開頭。核心類遵循Oracle的命名約定。閱讀關於他們here

6

string應該大寫的S,你有你的字符串類型爲小寫s

private String phonetype; 

Java是區分大小寫的。 String是一個擴展自Object類的類,這就是爲什麼它是大寫的,就像Integer一樣。然而,原始類型不是capitlized的(即,boolean,int,char)。

0

使字符串大寫。這是情況敏感的。

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

+0

更新修正並更改爲字符串大寫S,但現在當我更新它說錯誤:構造函數mobile(java.lang.String,int,int,java.lang.String,int,java.lang.String)已經然而,在頁面底部出現錯誤:public Mobile(String MobilephoneType,int Mobilescreensize,int Mobilememorycardcapacity,String newserviceprovider,int Mobilecameraresolution,String MobileGPS){ 這是什麼意思? – user2898828

+0

你有兩個完全相似的構造函數。刪除您發佈的代碼中的第59-71行。 而當您創建您的Mobile對象時,整數不會作爲字符串傳遞。所以擺脫引號。 您的值也必須與您將其放入構造函數中相匹配。因此,在創建Mobile對象時,必須按照與構造函數中列出的順序相同的順序給參數。 Mobile samsungPhone = new Mobile(「Samsung」,1,2,「verizon」,3 「GPS」); 其中1024 =屏幕尺寸,2 =存儲卡容量,「verizon」=服務提供商,3 =分辨率,並且「GPS」= gps。 – CamHart