2014-12-04 116 views
-4

我剛開始在學校學習編程,我被困在其中一個問題上。 我知道這太容易了,但我沒有得到任何這方面的信息。 我得到了創建類和構造到類型爲int數組的單個參數的類(我認爲),但我沒有得到方法部分。 另外,如果你們不想提供一個例子,你能否至少給我清楚的構造函數和方法的定義,因爲我無法理解它在java教程中說的是什麼。有人可以幫我解決這個問題嗎?

這裏是到目前爲止我的工作:

private int[] members; 
    public Lesson3(int[] array1){ 
    this.members= array1; 
    } 

,這裏是我想去做任務:

創建一個名爲GiveMeNext類。爲類型爲int數組的單個參數添加一個構造函數。將int數組存儲在類的成員變量中。 將成員方法添加到名爲getNextGreaterThan的類中,該類具有一個int參數並返回一個int。

+1

你應該多研究一下,至少看看java編程的基礎教程。 – JpCrow 2014-12-04 01:47:00

+1

看看Java教程中的這個例子:https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html – 2014-12-04 01:48:08

回答

0

只是試圖幫助。請不要評論,如果那裏有什麼問題我的回答:)

public class GiveMeNext { 

    // this is the member variable of the class marked as a private. 
    // it is accessible only inside this class. 
    private int[] someArray; 

    // this is your default Constructor. 
    public GiveMeNext(){ 

    } 

    // This is the constructor with a parameter. 
    // Constructor doesnt contain any return type 
    // You can create multiple Constructor in this class but ofcourse with diff parameter. 
    public GiveMeNext(int[] someArray){ 
     this.someArray = someArray; 
    } 


    //this is the method getNextGreaterThan. 
    // the 'int' after public is what we called return type. 
    // the 'int someInt' is what we called parameter. 
    // the return someInt is used to return the value of 'someInt'. 
    public int getNextGreaterThan(int someInt){ 
     return someInt; 
    } 
} 

更多信息看這些參考/教程:

Constructor

Understanding Constructor

Method

Understanding Methods

相關問題