2017-05-05 24 views
0

我有以下代碼:C#反對面向

public class Game 
    { 
     public Game(int board) 
     { 

     } 

     public List<int> play(List<int> lines) 
     { 
      return lines; 
     } 
    } 

如果我實例化類「遊戲」的新對象有,比如說一個參數,2:

Game game = new Game(2) 

是否有辦法在「play」方法中檢索(並使用)這個參數,而不是在這兩種方法之外聲明任何其他變量,或者定義任何其他方法?

任何幫助,將不勝感激。謝謝。

回答

0

您可以定義一個類型爲int的private int board;成員,併爲其分配板的值。然後你可以在play方法中訪問它。

0
public class Game { 

    private int Board; 
    public Game(int board) { 
     Board = board; 
    } 

    public List<int> play(List<int> lines) 
    { 
     //used board here 
     return lines; 
    } 
} 

只需創建可由播放方法

0

訪問的變量Board聲明board作爲成員在你的類,使所有功能都可以訪問它。也許是這樣的:

public class Game 
{ 
    private int board; 

    // Constructor for game. Initializes a new board 
    public Game(int board) 
    { 
     this.board = board 
    } 

    public List<int> play(List<int> lines) 
    { 
     // Access board 
     return lines; 
    } 
} 
+1

我很好奇,因爲我見過一對夫婦的答案做到這一點 - 什麼是在做一個私有字段中的值了' {get;組; }屬性? –

+0

我想這樣做,但我不認爲我參與的項目準則允許。有沒有辦法直接從Game對象/方法獲取值? – bloomers

+0

對不起@ @ JoeClay這是一個錯字。爲私人變量設置gettere/setter是沒有意義的。只是習慣於一直添加它們。修正它 – degant

1

您需要將其存儲在一個類的成員變量一樣

public class Game 
{ 
private int board; 
    public Game(int board) 
    { 
     this.board = board; 
    } 

    public List<int> play(List<int> lines) 
    { 
     return lines[board]; 
    } 
} 
1
public class Game { 
    private int _board; 
    public Game(int board) { 
     _board = board; 
    } 

    public List<int> play(List<int> lines) 
    { 
     //use _board here eg: this._board ... 
     return lines; 
    } 
} 
0

定義private field舉行這是通過構造函數傳遞的值。然後存取權限這一領域的Play-方法:

public class Game { 
    private readonly int _Board; 
    public Game(int board) { 
     _Board = board; 
    } 

    public List<int> play(List<int> lines) 
    { 
     //access field here 
     var board = _Board; 
     return lines; 
    } 
} 
0

我建議申報性能和使用構造函數鏈

public class Game { 
    public Game(int board, List<int> lines) { 
    if (board < 0) 
     throw new ArgumentOutOfRange("board"); 
    else if (lines == null) 
     throw new ArgumentNullException("lines"); 

    Board = board; 
    Lines = lines; 
    }   

    public Game(int board) 
    : this(board, new List<int>()) { 
    } 

    public Game(List<int> list) 
    : this(0, list) { 
    } 

    public int Board { 
    get; 
    private set; 
    } 

    public List<int> Lines { 
    get; 
    private set; 
    } 
} 

典型用途:

Game game = new game(3); 

game.Lines.Add(123); 
game.Lines.Add(456); 

// Board: 3; Lines: 123, 456 
Console.Write("Board: {0}; Lines: {1}", game.Board, string.Join(", ", game.Lines)); 
0

把它作爲第二個參數給你的播放方法,而不是將它傳遞給構造函數,因爲你沒有做任何事情構造函數。

public class Game 
{ 
    public Game() {} 

    public List<int> play(List<int> lines, int board) 
    { 
     // do whatever you want to do with board here 
     return lines; 
    } 
} 
0

我相信答案的數量是因爲你沒有在你的問題中澄清「你在哪裏」使用參數的值。

如果像大多數人懷疑的那樣,你只想在你的課堂內使用它(比如任何類的方法),那麼一個字段就足夠了,你可以使用任何以前給出的答案。儘管祕密我喜歡@ jon-Lopez-garcia,因爲它符合我的風格並且沒有任何額外的假設。

另一方面,如果您需要從類的方法外部訪問該值,那麼屬性是正確的方法。檢查@ dmitry-bychenko的答案非常完整,但是假設您沒有提到所需功能。

然而,如果你想從外面訪問它,那麼你也必須考慮如果你想從外部改變它,因爲@dmitry-bychenko的答案是沒有從外部改變(注意「私人設置;「)

只是爲了不被短這裏是我會做給缺乏上下文的內容:

public class Game 
{ 
    public int Board { get; private set; } 
    public Game(int board) 
    { 
     Board = board; 
    } 

    public List<int> play(List<int> lines) 
    { 
     //use Board here 
     return lines; 
    } 
} 

這可能是該主題好讀數: