2017-02-13 82 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