1

我正在尝试让我的代码创建创建手机对象所需的新构造函数对象。我尝试命名构造函数字段来创建对象。

更新:我已经修改stringString,但是现在当我更新它时,我得到了错误:

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();
    }
}
4

3 回答 3

7

string应该是大写的 S。你的字符串类型是小写的s

private String phonetype;

Java 区分大小写。String是从 Object 类扩展而来的类,这就是它被大写的原因,就像Integer. 然而,原始类型没有大写(即boolean, int, char)。

于 2013-10-19T21:41:20.407 回答
4

Java 区分大小写。S使用大写String

private String phonetype;

对象名称(又名引用类型)总是以大写字母开头。核心类遵循 Oracle 的命名约定。在此处阅读有关它们的信息

于 2013-10-19T21:41:07.930 回答
0

使字符串中的 s 大写。这是区分大小写的。

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

于 2013-10-19T21:41:48.460 回答