2017-02-13 81 views
-3

你好我想學C#中,我遇到了這個代碼從C#教程:類實例化功能

Public Class A() 
{ 
    Public A() : this(capacity:10) 
    { 
    } 

    public int capacity 
    { 
     get { return _buffer.length;} 
    } 
} 

我只是不明白,爲什麼他Public A()this(capacity:10)之間使用:

我不知道Google 要搜索什麼,所以我決定在這裏問一下。

我的問題是:這是用來幹什麼的?爲什麼?

+0

就個人而言,我會更加努力想出來的一個電話,在詢問Stack Overflow之前。 –

回答

0

提供的代碼未編譯。也許,代碼是這樣的:

// lower case for "public" and "class" 
    public class A { 
    // _buffer is addressed in "capacity" property, let it be an array 
    private int[] _buffer = new int[10]; 

    // this constructor is mandatory for the code below 
    public A(int capacity) { 
    } 

    // Now, your question: 
    // "this(capacity: 10)" calls the costructor above 
    // "public A(int capacity)" 
    // while passing the "capacity" parameter by its name 
    public A() : this(capacity: 10) 
    { 
    } 

    public int capacity { 
    get { 
     return _buffer.Length; 
    } 
    } 

如果我猜得不錯this(capacity: 10)是構造public A(int capacity)同時通過名路過capacity

+0

現在我明白了,謝謝 – Skoochapp