2.4.txt
来自「《Microsoft Visual C# .NET 2003开发技巧大全》源代码」· 文本 代码 · 共 37 行
TXT
37 行
Listing 2.4 Overloading the ++ Operator to Increment an Internal Member Variable
public class Television
{
/// <summary>
/// current channel tv is set to
/// </summary>
private static int channel = 2;
private const int maxChannels = 200;
/// <summary>
/// changes the channel to a specified channel
/// </summary>
public bool ChangeChannel(int newChannel)
{
if( newChannel > maxChannels )
return false;
channel = newChannel;
return true;
}
public static Television operator ++( Television tv )
{
tv.ChangeChannel(++channel);
return tv;
}
public int GetChannel()
{
return channel;
}
static void Main(string[] args)
{
Television tv = new Television();
for( int i = 0; i < 20; ++i )
{
tv++;
Console.WriteLine( “{0}”, tv.GetChannel());
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?