题目
新建类:时间,包含属性:月份
该属性可以从外部输入数字,并自动转化数字为中文月份的输出
解析
1. 创建项目,首先为简洁,创建一个控制台项目
2. 创建类
在C#语言中类是一种数据类型,使用前必须声明,格式如下:
[访问修饰符] class 类名: 基类名
{
类的成员
}
根据要求第一步声明Time类
class Time
{
// 类成员
}
3. 完善类的成员,声明month字段。
声明字段的方法与普通变量的方法基本相同,格式如下:
[访问修饰符] 数据类型 字段名
根据题目,声明一个int字段,
完善后的Time类应该为
class Time
{
public int month;
}
4. 增加一个属性,把数字转为字符串
在类中定义属性的格式如下:
[访问修饰符] 数据类型 属性名
{
get
{
return 字段名;
}
set
{
字段名=value;
}
}
这里我们只需要get方法,根据month字段,返回中文月分名。最直接的方式是采用if...else判断
由于month类型为int,因此我们可以使用switch...case简化。
增加属性方法如下:
class Time
{
public int month;
public string CN_Month
{
get
{
switch (month)
{
case 1: return "一";
case 2: return "二";
case 3: return "三";
case 4: return "四";
case 5: return "五";
case 6: return "六";
case 7: return "七";
case 8: return "八";
case 9: return "九";
case 10: return "十";
case 11: return "十一";
case 12: return "十二";
default: return "转换失败!";
}
}
}
}
5.通过进一步分析, 因为月分的取值范围为1-12,为month的输入增加判断。
同样使用属性方法
class Time
{
public int month;
public int Month
{
set
{
if (value <= 0 || value > 12)
{
Console.WriteLine("你输入的月份有误,请核对再输!");
}
else
{
month = value;
}
}
}
public string CN_Month
{
get
{
switch (month)
{
case 1: return "一";
case 2: return "二";
case 3: return "三";
case 4: return "四";
case 5: return "五";
case 6: return "六";
case 7: return "七";
case 8: return "八";
case 9: return "九";
case 10: return "十";
case 11: return "十一";
case 12: return "十二";
default: return "转换失败!";
}
}
}
}6.完整的控制台程序为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Time
{
class Time
{
public int month;
public int Month
{
set
{
if (value <= 0 || value > 12)
{
Console.WriteLine("你输入的月份有误,请核对再输!");
}
else
{
month = value;
}
}
}
public string CN_Month
{
get
{
switch (month)
{
case 1: return "一";
case 2: return "二";
case 3: return "三";
case 4: return "四";
case 5: return "五";
case 6: return "六";
case 7: return "七";
case 8: return "八";
case 9: return "九";
case 10: return "十";
case 11: return "十一";
case 12: return "十二";
default: return "转换失败!";
}
}
}
}
class Program
{
static void Main(string[] args)
{
string tmp = Console.ReadLine();
int m = Convert.ToInt16(tmp);
Time t = new Time();
t.Month = m;
Console.WriteLine("当前月份为:" + t.CN_Month);
}
}
}
7. 由于转换的数字具有连续性,可以通过数组进行Hash完成转换。上面代码可以简化为
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Time
{
class Time
{
public int month;
string[] months = new string[] { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二" };
public int Month
{
set
{
if (value <= 0 || value > 12)
{
Console.WriteLine("你输入的月份有误,请核对再输!");
}
else
{
month = value;
}
}
}
public string CN_Month
{
get
{
if ((month - 1) >= 0 && (month - 1) < 12)
{
return months[month - 1];
}
else
{
return "转换失败!";
}
}
}
}
class Program
{
static void Main(string[] args)
{
string tmp = Console.ReadLine();
int m = Convert.ToInt16(tmp);
Time t = new Time();
t.Month = m;
Console.WriteLine("当前月份为:" + t.CN_Month);
}
}
}8. 修改成Windows版本。
8.1把类拷贝至新建的windows项目中。在界面上增加按钮button, 文本输入框Textbox和标签Label
8.2双击button,增加消息响应代码。
Time t = new Time();
t.Month = Convert.ToInt16(textBox1.Text);
label1.Text = "当前月份为:" + t.CN_Month;总结
属性方法可以让字段的输入输出更容易控制,很方便地完成数据的转换。在实际中,往往使用属性方法做数据的转换任务。但是学习时注意不要和字段相混淆。

本文介绍如何在C#中创建一个时间类,该类包含一个月份属性,能够将数字输入转换为对应的中文月份名称,并对输入进行有效性检查。
770

被折叠的 条评论
为什么被折叠?



