本帖最后由 §風過無痕§ 于 2013-6-26 19:20 编辑
以下代码是依照苏坤老师培训视频里讲的属性那段 仿写 :- class Program
- {
- static void Main(string[] args)
- {
- Employees zs = new Employees();//构造
- zs.Name = "王二麻子";
- zs.Sex = '女';
- zs.Age = 28;
- zs.Wages = 999;
- zs.Bonus = 200;
- zs.SayHello();
- zs.Shownumber();
- Console.ReadKey();
- }
- }
- /// <summary>
- /// 声明一个类,该类有5个属性
- /// </summary>
- public class Employees
- {
- string name;
- public string Name
- {
- get { return name; }
- set { name = value; }
- }
- char sex;
- public char Sex
- {
- get { return sex; }
- set
- {
- if (Sex == '男' || Sex == '女')
- sex = value;
- else
- sex = '0';
- }
- }
- int age;
- public int Age
- {
- get { return age; }
- set
- {
- age = value > 0 ? value : 0;
- }
- }
- decimal wages;
- public decimal Wages
- {
- get { return wages; }
- set
- {
- wages = value >= 1000M ? value : 1000;
- }
- }
- decimal bonus;
- public decimal Bonus
- {
- get { return bonus; }
- set
- {
- bonus = value >= 0M ? value : 0;
- }
- }
- public void SayHello()// 定义一个方法实现打招呼,若此处修饰符为static则无法访问上面的字段(属性)
- {
- Console.WriteLine("我叫{0},我是{1}生,我的年龄是{2}岁", name, sex, age);
- }
- public void Shownumber()//定义一个方法 实现通报工资
- {
- decimal sum = wages + bonus;
- Console.WriteLine("我叫{0},我的工资是{1},我的奖金是{2},总工资为{3}", name, wages, bonus, sum);
- }
- }
复制代码 运行结果:
- 这段代码有木有问题 请赐教
- char sex;
- public char Sex
- {
- get { return sex; }
- set
- {
- if (Sex == '男' || Sex == '女')
- sex = value;
- else
- sex = '0';
- }
- }
复制代码 |