我想讓我的代碼創建我需要創建一個手機對象的新構造函數對象。我嘗試命名構造函數字段來創建對象。Java錯誤,無法找到符號 - 類字符串
更新:我已經修改了string
是String
,但現在當我更新我的錯誤:
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();
}
}
錯誤 「構造移動(java.lang.String中,INT,INT,java.lang.String中,INT,java.lang.String中)中的一類移動已經定義」 是指正是它說。你有兩個具有相同簽名的構造函數。只要刪除一個重複的構造函數。 – pburka
如果您有新問題,請提出一個新問題。不要編輯一個問題將其轉化爲另一個問題。否則,堆棧溢出會遇到一個問題,以及一些與它不匹配的答案。 ...編譯器錯誤意味着它說的是什麼。你已經定義了一個構造函數Mobile(String,int,int,String,int,String)'當你已經使用這些參數GOT構造函數時。 –