2013-04-03 55 views
0
import java.io.*; 
import java.util.*; 

public class CompanyDetail { 

    int Id; 
    String name; 
    String department; 
    static String companyname="Maruti Suzuki"; 

    CompanyDetail(int ID,String Name,String Dept) { 
     Id=ID; 
     name=Name; 
     department=Dept; 
    } 

    public void getdata() { 
     try { 
      InputStreamReader in = new InputStreamReader(System.in); 
      BufferedReader br = new BufferedReader(in); 
      System.out.println("Employee Id :"); 
      Id = Integer.parseInt(br.readLine()); 
      System.out.println("Employee name :"); 
      name= br.readLine(); 
      System.out.println("Employee Department :"); 
      department=br.readLine(); 
      System.out.println("Company is :"+companyname); 
     } 
     catch(Exception e) { 
     } 
    } 
    public void printdata() { 
     System.out.println("Employee Id is :"+Id); 
     System.out.println("Employee Name is :"+name); 
     System.out.println("Employee Department is :"+department); 
     System.out.println("Company is :"+companyname); 
    } 
} 

public class CompanyUse { 
    public static void main(String[] args) { 
     CompanyDetail cd = new CompanyDetail(int Id,String name,String department); 
     cd.getdata(); 
     cd.printdata(); 
    } 
}  

我得到錯誤的主要功能塊,當我創建一個object..eclips一直teeling我,構造CompanyDetail()是未定義....,沒有它的構造函數給我一個空值在我的output..please幫助我..我剛剛開始學習java ...非常感謝你提前:) :)構造CompanyDetail()是不確定的(JAVA)

回答

2

默認的構造函數(帶有空的參數列表)由編譯器自動創建只有當你不創建任何其他構造函數。在你的情況下,你有CompanyDetail(int ID,String Name,String Dept),所以它不會自動生成。你可以寫自己的默認構造函數:

CompanyDetail() {} 

我也有一些小技巧:

  • 這個代碼不編譯,請修正所有錯誤 - java編譯器是在說相當好有什麼錯碼(:
  • 永遠,永遠使用空的catch塊,
  • 儘量遵循命名約定流行的Java中
相關問題