2017-05-03 35 views
0
public class Testing{ 
private String firstName; 
private String lastName; 
private double salary; 
private String subject; 
private String highestDegree; 
private int years; 


public Testing 
(String first, String last, String sub, String degree, double sal, int year) 
//constructor being called in the main method. 
    { 
     lastName = last; 
     firstName = first; 
     subject = sub; 
     highestDegree = degree; 
     salary = sal; 
     years = year; 
    } 
public class Hello{ 

public static void main(String []args){ 
    //Part missing 
} 
} 

我做了setter和getters,我所缺少的是如何在main方法中調用構造函數。我正在考慮創建一個像Testing in = new Testing()這樣的新對象,但我很確定我錯過了其他的東西。如果還有更多的東西可能會丟失,請告訴我。我在學習java。如何調用一個方法及其參數?

+1

究竟是什麼不清楚呢?你有嘗試過什麼嗎?收到任何編譯器錯誤? – Siguza

+0

你只是問如何調用構造函數?因爲這很簡單 - '測試in = new測試(首先,最後,次,度,年,年)',其中這些參數是您在調用之前定義的變量或文字值。還是你也在問如何從命令行獲取這些參數? –

+0

請閱讀Java教程! –

回答

2

既然你已經定義了自定義構造函數
public Testing (String first, String last, String sub, String degree, double sal, int year)
你不能使用默認的構造函數。

Testing in = new Testing() // Not allowed now 

你將不得不使用你的構造函數來定義,實例化和初始化Testing類的對象。

public static void main(String []args){ 
    String first = "userName"; 
    ... 
    ... 

    Testing in = new Testing(first, last, sub, degree, sal, year) 
} 
+0

哦,T_T現在有道理。非常感謝! – BreakerMC

相關問題