C#程序设计实验--类与属性

题目

新建类:时间,包含属性:月份

该属性可以从外部输入数字,并自动转化数字为中文月份的输出

解析

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#程序设计练习 一、实验目的 1.掌握C#语言的基本语法、控制语句及异常处理。 2.掌握C#的基本使用方法以及C#语言面向对象的基本特性。 二、实验内容 1.编写一个函数,用于计算1!+2!+3!+4!+5!,在控制台或页面输出运行结果。 2.在控制台或页面输出九九乘法表。 3.输入10个以内的整数,输出该组整数的降序排列,要求采用数组实现。 4.计算两个数的商,在控制台或页面输出结果,要求包含异常处理。 5.定义一个汽车,该具有重量速度属性;再定义一个跑车,该继承汽车属性,并拥有自己的颜色属性;然后声明一个汽车的对象一个跑车的对象,并把它们的属性输出到控制台上。 6.假设某动物园管理员每天需要给他所负责饲养的狮子、猴子鸽子喂食。请用一个程序来模拟他喂食的过程。 要求: (1)饲养员喂食时,不同动物执行不同的吃的功能,例如狮子吃肉、猴子吃香蕉、鸽子吃大米等。 (2)饲养员喂动物时,不能使用判断语句判断动物型。 (3)使用虚方法或抽象方法实现喂养不同动物的多态,不能使用方法重载。 提示:需要建一个动物,动物有一个虚的或抽象的吃方法,动物下面有几个子,不同的子重写父的吃方法。饲养员提供喂食方法。然后,在Main方法中一一调用吃的方法。 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Add2._1 { class Program { static void Main(string[] args) { int sum = 0; for (int i = 1; i < 6; i++) { int tmp = 1; for (int j = 1; j <= i; j++) { tmp = tmp * j; } sum += tmp; } Console.WriteLine("1!+2!+3!+4!+5!={0}\r\n", sum.ToString()); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值