0

嗨,我正在尝试让我的代码创建我需要的两个新的构造函数对象

为什么我会收到这个错误?,java 错误,Class Mobile 已经在包 unnamed 包中定义

错误出现在页面底部的公共课堂移动端

更新 我刚刚将第二个公共课程重命名为 mymobile 而不是 mobile,并将主要的公共课程保留为 Mobile(因为我正在做作业,它说第一个公共课程必须命名为 public class mobile),但是我现在收到此错误:Mobile 类中的构造函数 Mobile 不能应用于给定类型。

/**
 * to write a simple java class Mobile that models a mobile phone.
 * 
 * @author (ME) 
 * @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
4

2 回答 2

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();
    }
}
于 2013-10-19T16:29:27.690 回答
0

您已将模型类命名为 Mobile,并将主类命名为 Mobile。将主类重命名为其他名称。

于 2013-10-19T16:29:44.287 回答