2013-10-20 1 views
0

업데이트 코드, Error에 따르면 내부 클래스의 잘못된 static decleration이라고합니다. Mobile.mymobile 수정 자 'static은 상수 변수 decularations에서만 허용됩니다 (Line 75, public static void main (String [] args) {, 이것은 무엇을 의미합니까 ??Java 오류, 생성자 Mobile 주어진 유형에 적용 할 수 없습니다. 이것은 무엇을 의미 하는가?

내 코드 : 나는 완전히 상실하고 같이

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

} 

어떤 답변이나 답변 및 도움을 주시면 감사하겠습니다!

+0

질문에 이미 답변되어 있지만 조언을 해드립니다. 이러한 상황을 방지하기 위해 4 개 이상의 인수가있는 메소드 (생성자)를 만들지 마십시오. 대신에 [패턴 빌더] (http://en.wikipedia.org/wiki/Builder_pattern)를 사용하거나 getters/setters를 사용하십시오. – ArtemStorozhuk

답변

1

생성자 모바일 주어진 유형에 적용 할 수 없습니다.

잘못된 매개 변수로 생성자를 호출하려고합니다.

필수 항목 : 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;

당신이 실제로 전달하고있는 것입니까? 생성자가 하나 String, 세 int의 두 String의를 요구하면서


Mobile Samsung = new Mobile("Samsung", "3.0", "4gb", "8mega pixels", 
       "GPS"); 
Mobile Blackberry = new Mobile("Blackberry", "3.0", "4gb", 
       "8mega pixels", "GPS"); 

5 개 개의 String 인수를 전달하고 있습니다. 그래서 당신은 오류가 발생합니다.

UPDATE를 할

올바른 방법 : 당신이 원하는에 매개 변수

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 

); 

매개 변수는 여기에 없습니다, 당신이 생성자 문자열 mobileGPS에 5 인수를 놓치고

Mobile samsungPhone = new Mobile(
    "Samsung" // String mobilephonetype 
, 1024 // int mobilescreensize 
, 2  // int mobilememorycardcapacity 
, 8  // int mobilecameraresolution 
, "GPS" //String mobileGPS 
, "verizon" // String newserviceprovider 
); 
+0

미안 어떻게 하나의 문자열, 3 ints 및 두 문자열을 전달합니까 ?? – user2898828

+0

@ user2898828 내 업데이트를 참조하십시오. 그것은 작동하지만 필요한 값에 따라 매개 변수 값을 변경해야합니다. – BackSlash

+0

업데이트 된 코드, 오류에 의하면 내부 클래스의 잘못된 static declation이 명시되어 있습니다. Mobile.mymobile 수정 자 'static은 상수 변수 decularations, Line 75, public static void main (String [] args) {, 이것은 무엇을 의미합니까 ?? – user2898828

관련 문제