2013-10-20 71 views
0

我想讓我的代碼創建我需要創建一個手機對象的新構造函數對象。我嘗試命名構造函數字段來創建對象。Java錯誤 - 不是一個聲明,這是什麼意思?

當我編譯我的代碼在這一行this.Mobile samsungPhone = new Mobile("Samsung", 1, 2, "verizon", 3 "GPS");我得到這個錯誤:不是一個聲明,這是什麼意思?

更新代碼!

我的代碼:

/** 
* 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,int mobilecameraresolution,String mobileGPS, String newserviceprovider) { 
     this.phonetype = mobilephonetype; 
     this.screensize = mobilescreensize; 
     this.memorycardcapacity = mobilememorycardcapacity; 
     this.cameraresolution = mobilecameraresolution; 
     this.GPS = mobileGPS; 
     this.serviceprovider = newserviceprovider; 
     this.typeofcontract = 12; 
     this.checkcharge = checkcharge; 
     // you do not use this ones during instantiation,you can remove them if you do not need or assign them some default values 


     Mobile samsungPhone = new Mobile("Samsung", 1, 2, "verizon", 3, "GPS"); 
     1024 = screen size; 
     2 = memory card capacity; 
     3=resolution; 
     GPS = gps; 
     "verizon"=service provider; 
     typeofcontract = 12; 
     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); 

    } 



} 

class mymobile { 
    public static void) { 
     Mobile Samsung = new Mobile("Samsung", 1, 2, "verizon", 3, "GPS"); 
     Mobile Blackberry = new Mobile("Blackberry", "3.", "4","8", "GPS"); 
     Samsung.displayMobileDetails(); 
     Blackberry.displayMobileDetails(); 
    } 
} 

任何答覆和答覆將不勝感激!

+1

'this.Mobile'沒有意義。 – SLaks

+0

報告的錯誤是什麼? – pburka

+0

你認爲這條線有什麼作用? '這個。「verizon」=服務提供商;' – pburka

回答

0
Mobile Samsung = new Mobile("Samsung", 1, 2, "verizon", 3 "GPS"); 

參數值3後缺少一個逗號。我會說這是你的罪魁禍首。

0

它的編譯錯誤,這意味着編譯器要求的聲明,你給了別的東西,請refer

應該

Mobile samsungPhone = new Mobile("Samsung", 1, 2, "verizon", 3, "GPS"); 
1
this.2 = memory card capacity; 
this.3=resolution; 

毫無意義可言。

您不能將值分配給文字(2,3)。


編輯:您還需要解決這個問題:

Mobile Samsung = new Mobile("Samsung", 1, 2, "verizon", 3, "GPS"); 

正如其他成員說。

0

Mobile是一個類,「這」代表當前object.So,您不能使用「本」爲Mobile.You應:

Mobile samsungPhone = new Mobile("Samsung", 1, 2, "verizon", 3, "GPS"); 
0
Mobile samsungPhone = new Mobile("Samsung", 1, 2, "verizon", 3, "GPS"); 
    1024 = screen size; 
    2 = memory card capacity; 
    3=resolution; 
    GPS = gps; 
    "verizon"=service provider; 
    typeofcontract = 12; 
    checkcharge = checkcharge; 

您需要更換號碼你的new Mobile ctor調用具有合理的價值,如下面必須註釋/註釋所描述的,例如,如果你有一個1放在決議中(這是沒有意義的,除非它是索引或什麼的),你有一個2放在存儲卡容量,其中有3放入分辨率等

然後註釋掉垃圾的行,或者最好刪除它們。