📄 2.4.txt
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -