Java错误,构造函数Mobile无法应用于给定类型;这是什么意思?
更新的代码,错误表明内部类Mobile.mymobile修饰符中的static declaration非法,仅在常量变量declaration中允许static,第75行,public static void main(String[]args){,这是什么意思 我的代码:Java错误,构造函数Mobile无法应用于给定类型;这是什么意思?,java,Java,更新的代码,错误表明内部类Mobile.mymobile修饰符中的static declaration非法,仅在常量变量declaration中允许static,第75行,public static void main(String[]args){,这是什么意思 我的代码: /** * to write a simple java class Mobile that models a mobile phone. * * @author (jamal) * @version (1
/**
* to write a simple java class Mobile that models a mobile phone.
*
* @author (jamal)
* @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;
// 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;
Mobile samsungPhone = new Mobile(
"Samsung" // String mobilephonetype
, 1024 // int mobilescreensize
, 2 // int mobilememorycardcapacity
, 8 // int mobilecameraresolution
, "GPS" //String mobileGPS
, "verizon" // String newserviceprovider
);
//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);
}
/**
* The mymobile class implements an application that
* simply displays "new Mobile!" to the standard output.
*/
public class mymobile {
public static void main(String[] args) {
System.out.println("new Mobile!"); //Display the string.
}
}
public static void buildPhones(){
Mobile Samsung = new Mobile("Samsung", 3, 4, 8, "verizon",
"GPS");
Mobile Blackberry = new Mobile("Blackberry", 3, 4,
8, "verizon", "GPS");
Samsung.displayMobileDetails();
Blackberry.displayMobileDetails();
}
public static void main(String[] args) {
buildPhones();
}
}
任何答案或答复和帮助将不胜感激,因为我完全迷路了
构造函数不能应用于给定类型
意味着您试图使用错误的参数调用构造函数
必需:java.lang.String,int,int,int,java.lang.String,java.lang.String
意味着构造函数需要这些类型作为传递给它的参数
找到:java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String
是你真正传递给它的东西
您正在传递五个
String
参数,而构造函数需要一个String
、三个int
s和两个String
s。这就是为什么会出现错误
更新
正确的方法是:
Mobile Samsung = new Mobile("Samsung", 3, 4, 8, "verizon"
"GPS");
Mobile Blackberry = new Mobile("Blackberry", 3, 4,
8, "verizon", "GPS");
将参数更改为所需的参数
您的第一个构造函数调用也是错误的
Mobile samsungPhone = new Mobile(
"Samsung" // String mobilephonetype
, 1024 // int mobilescreensize
, 2 // int mobilememorycardcapacity
, 8 // int mobilecameraresolution
, "verizon" // String newserviceprovider
);
此处缺少一个参数,应为
Mobile samsungPhone = new Mobile(
"Samsung" // String mobilephonetype
, 1024 // int mobilescreensize
, 2 // int mobilememorycardcapacity
, 8 // int mobilecameraresolution
, "GPS" //String mobileGPS
, "verizon" // String newserviceprovider
);
您缺少构造函数String mobileGPS的第5个参数。很抱歉,我如何传递一个字符串、三个int和两个字符串???@user2898828查看我的更新。它可以工作,但您必须根据需要更改参数值更新的代码,错误显示内部类Mobile.mymobile修饰符的static是非法的static declaration只允许在常量变量decellerations中,第75行,publicstaticvoidmain(String[]args){,这意味着什么?这个问题已经回答了,但让我给你一个建议-为了防止这种情况,不要创建具有4个以上参数的方法(构造函数)。而不要使用或使用getter/setter。
Mobile samsungPhone = new Mobile(
"Samsung" // String mobilephonetype
, 1024 // int mobilescreensize
, 2 // int mobilememorycardcapacity
, 8 // int mobilecameraresolution
, "GPS" //String mobileGPS
, "verizon" // String newserviceprovider
);