VisualStudio_類別中屬性欄位的封裝快捷鍵_Ctrl按著不放R加E

Ctrl按著不放R加E


透過屬性封裝
解決屬性數值邏輯上的過濾


像是   性別字串、年齡等等資料數值範圍等等







屬性封裝過後的C#程式碼

Student.cs


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace School
{
    public class Student
    {
        private string name;
        private int age;
        private string stuNo;
        private string sex;
        private string phone;

        #region age屬性封裝
        /// <summary>
        /// age屬性封裝 : Age
        /// </summary>
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                if (value > 0 || value < 100)
                {
                    age = value;
                }
                else
                {
                    Console.WriteLine("年齡超出範圍,重置為18");
                    age = 18;
                }
            }
        }
        #endregion

        #region name之封裝
        /// <summary>
        /// name之封裝 : Name
        /// </summary>
        public string Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
            }
        }
        #endregion

        #region sex屬性封裝
        /// <summary>
        /// //sex屬性封裝: Sex
        /// </summary>
        public string Sex
        {
            get { return sex; }
            set
            {
                if (value == "男" || value == "女")
                    sex = value;
                else
                {
                    Console.WriteLine("性別不存在");
                    sex = "未知";
                }
            }
            //set { sex = value; }
        }
        #endregion

        #region stuNo屬性封裝
        /// <summary>
        /// stuNo屬性封裝 :StuNo屬性封裝
        /// </summary>
        public string StuNo
        {
            get
            {
                return stuNo;
            }

            set
            {
                stuNo = value;
            }
        }
        #endregion

        #region phone 屬性封裝
        /// <summary>
        /// phone 屬性封裝 : Phone
        /// </summary>
        public string Phone
        {
            get
            {
                return phone;
            }

            set
            {
                phone = value;
            }
        }
        #endregion



        #region 年齡設置之Method +void SetAge(int num)

        public void SetAge(int num)
        {
            if (num > 0 && num <= 100)
                this.Age = num;
            else
            {
                Console.WriteLine("年齡不合理,將重置為18");
                this.Age = 18;
            }
        }
        #endregion

        #region 年齡獲取之Method +int GetAge()
        /// <summary>
        /// 獲取年齡之方法 
        /// </summary>
        /// <returns></returns>
        public int GetAge()
        {
            return this.Age;//獲取年齡之方法
        }
        #endregion

        #region 學生自我介紹的Method +void Show()
        /// <summary>
        /// 自我介紹
        /// </summary>
        public void Show()
        {
            Console.WriteLine("我的姓名為:{0}\n" +
                              "我的學號為:{1}\n" +
                              "我的年齡是{2}\n" +
                              "我的性別是{3}\n", this.Name, this.StuNo, 
                                                    this.Age,this.sex);
        }
        #endregion
    }
}


Program.cs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace School
{
    class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student();
            //靜態屬性的指派
            stu.Name = "王大陸";
            stu.StuNo = "001";
            //寫法1.動態方法的呼叫
            stu.SetAge(20);
            //寫法2.使用屬性封裝
            stu.Age = 20;
            stu.Sex = "男";
            //動態方法的呼叫
            stu.Show();

            Student stu2 = new Student();
            //靜態屬性的指派
            stu2.Name = "林櫻賢";
            stu2.StuNo = "002";
            stu2.Sex = "人妖";
            //寫法1.動態方法的呼叫
            stu2.SetAge(110);
            //寫法2.使用屬性封裝
            stu2.Age = 190;


            stu2.Show();




            Console.ReadKey();
        }
    }
}









留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題