您好我正在尝试让我的代码创建我需要的两个新构造函数对象
为什么我收到此错误?,java错误,Class Mobile已在包unamed包中定义
页面底部的公共类移动设备出现错误
更新 我刚刚将第二个公共类重命名为mymobile而不是移动,并且将主要的公共类重新命名为Mobile(因为我正在进行任务,并且它说第一个公共类必须被命名为公共类移动),不过我现在得到此错误:构造函数Mobile类中的Mobile不能应用于给定类型。
/**
* 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 int phonetype;
// size of screen in inches
private int screensize;
// menory card capacity
private int memorycardcapacity;
// name of present service provider
private int 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 int GPS;
// instance variables - replace the example below with your own
private int x;
// The constructor method
public Mobile (String Mobilephonetype, String Mobilescreensize, String Mobilememorycardcapacity, String Mobilecameraresolution, String MobileGPS, String Mobileserviceprovider, String Mobiletypeofcontract, String Mobilecheckcharge)
{
this.phonetype = phonetype;
this.screensize = screensize;
this.memorycardcapacity = memorycardcapacity;
this.cameraresolution = cameraresolution;
this.GPS = GPS;
this.serviceprovider = serviceprovider;
this.typeofcontract = typeofcontract;
this.checkcharge = checkcharge;
}
// The new constructor method
{
this.phonetype = phonetype;
this.screensize = screensize;
this.memorycardcapacity = memorycardcapacity;
this.cameraresolution = cameraresolution;
this.GPS = GPS;
this.serviceprovider = serviceprovider;
this.typeofcontract = typeofcontract;
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);
}
public class myMobile {
public static void main(String[] args) {
Mobile Samsung = new Mobile("Samsung", "3.0", "4gb", "8mega pixels","GPS" );
Mobile Blackberry = new Mobile("Blackberry", "3.0", "4gb", "8mega pixels","GPS" );
Samsung.displayMobileDetails();
Blackberry.displayMobileDetails();
}
}
}
any replies/answers would be greatly appreciated, thanks
答案 0 :(得分:0)
在同一个包中不能有两个具有相同名称的类
尝试更改您的某个班级名称
public class Mobile {...}
public class Test {
public static void main(String[] args) {.... }
}
=============================================== ==
<强>更新强>
尝试以下代码
public class Mobile
{
// type of phone
private int phonetype;
// size of screen in inches
private int screensize;
// menory card capacity
private int memorycardcapacity;
// name of present service provider
private int 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 int GPS;
// instance variables - replace the example below with your own
private int x;
// The constructor method
public Mobile(String Mobilephonetype, String Mobilescreensize,
String Mobilememorycardcapacity, String Mobilecameraresolution,
String MobileGPS, String Mobileserviceprovider,
String Mobiletypeofcontract, String Mobilecheckcharge) {
this.phonetype = phonetype;
this.screensize = screensize;
this.memorycardcapacity = memorycardcapacity;
this.cameraresolution = cameraresolution;
this.GPS = GPS;
this.serviceprovider = serviceprovider;
this.typeofcontract = typeofcontract;
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);
}
public Mobile(String Mobilephonetype, String Mobilescreensize,
String Mobilememorycardcapacity, String Mobilecameraresolution,
String MobileGPS) {
this.phonetype = phonetype;
this.screensize = screensize;
this.memorycardcapacity = memorycardcapacity;
this.cameraresolution = cameraresolution;
this.GPS = GPS;
}
}
class mymobile {
public static void main(String[] args) {
Mobile Samsung = new Mobile("Samsung", "3.0", "4gb", "8mega pixels",
"GPS");
Mobile Blackberry = new Mobile("Blackberry", "3.0", "4gb",
"8mega pixels", "GPS");
Samsung.displayMobileDetails();
Blackberry.displayMobileDetails();
}
}
答案 1 :(得分:0)
您已将模型类命名为Mobile,将主类命名为Mobile。将主类重命名为其他类。