您好
关于名为Square的类中的此部分代码:
public Square( int i_RowIndex, eColumn i_ColIndex)
{
m_RowIndex = i_RowIndex;
m_ColIndex = i_ColIndex;
**new Square(i_RowIndex, i_ColIndex, eCoinType.NoCoin);**
}
public Square(int i_RowIndex, eColumn i_ColIndex, eCoinType i_CoinType)
{
m_RowIndex = i_RowIndex;
m_ColIndex = i_ColIndex;
m_Coin = i_CoinType;
}
Run Code Online (Sandbox Code Playgroud)
在其他C'tor中调用过载的C'tor以及用粗体看到的"新"声明是不是很好?我认为这是错误的,每次我们调用new时我们都会分配一个新实例,从C'tor分配2个重复实例是不对的,这意味着从第一个位置分配一个实例.
我错了吗?
谢谢
您不应该在构造函数中调用重载构造函数,而是创建一个新实例.
应该更像是:
public Square( int i_RowIndex, eColumn i_ColIndex)
: this(i_RowIndex, i_ColIndex, eCoinType.NoCoin)
{
}
public Square(int i_RowIndex, eColumn i_ColIndex, eCoinType i_CoinType)
{
m_RowIndex = i_RowIndex;
m_ColIndex = i_ColIndex;
m_Coin = i_CoinType;
}
Run Code Online (Sandbox Code Playgroud)