📄 2.3.txt
字号:
Listing 2.3 Creating and Using a Class
using System;
namespace _4_UsingObjects
{
public class Television
{
public Television(){}
private int channel = 2;
private static bool on = false;
// increments channel if tv is on
public void ChannelUp()
{
if( on == false )
return;
++channel;
}
// decrements channel if tv is on
public void ChannelDown()
{
if( on == false )
return;
--channel;
}
// gets or set current channel
public int Channel
{
get
{
return channel;
}
set
{
channel = value;
}
}
// get status of TV and allows you to
// turn it on or off
public static bool On
{
get
{
return on;
}
set
{
on = value;
}
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
Television tv = new Television();
// turn television on
Television.On = true;
// change channel
tv.Channel = 42;
Console.WriteLine( “Channel = {0}”, tv.Channel );
tv.ChannelUp();
Console.WriteLine( “Channel = {0}”, tv.Channel );
tv.ChannelDown();
Console.WriteLine( “Channel = {0}”, tv.Channel );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -