Home [C# 筆記] 屬性與字段
Post
Cancel

[C# 筆記] 屬性與字段

屬性與字段

  • 字段(Field):在class類別內定義的變量,用於確定數據在內存中的儲存
1
2
3
public class Student {
    private string name; //字段field
}
  • 屬性(Property):提供對字段的訪問器(Accessor)
1
2
3
4
5
6
7
public class Student {
    private string name; //字段field
    public string Name { //屬性Property
        get { return name; }
        set { name = value; } //value就是調用set方法時傳入的字串參數
    }
}

屬性用途(一)

  • 屬性值驗証:通過get/set方法,對傳入的數據進行參數合法性的驗証

情況1:用於對數據進行校驗

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
class Student {
    private string name = "";
    public string Name { get; set; }

    private int score = 0;
    //情況1:用於對數據進行校驗
    public int Score
    {
        get { return score; }
        set {
            //傳人的分數必須介於0-100之間
            if (value < 0||value >100) {
                Console.WriteLine("分數必須介於0-100之間");
                return;
            }
            score = value;
        }
    }
}
internal class Program
{
    static void Main(string[] args)
    {
        Student student = new Student();
        student.Score = -100;
        Console.WriteLine(student.Score);
    }
}

情況2:對數據的輸入/輸出格式進行設置

  • 字段訪問形式:通過get/set方法,可以給到用戶合適的數據格式

需求:

  • 設計 Clock類 (字段:秒 seconds)
  • 訪問方法:
    • Hours: 設置/獲取當前是多少小時
    • Minutes: 設置/獲取當前是多少分鐘
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
class Clock
{
    private double seconds = 0.0;

    //Hours獲取小時數據/設置小時數據
    public double Hours
    {
        get { return seconds / 3600; }
        set { seconds = value * 3600; }
    }
    //Minutes獲取分鐘數據/設置分鐘數據
    public double Minutes
    {
        get { return seconds / 60; }
        set { seconds = value * 60; }
    }
}
internal class Program
{
    static void Main(string[] args)
    {
        Clock clock = new Clock();
        clock.Hours = 10;
        Console.WriteLine(clock.Hours);
        Console.WriteLine(clock.Minutes);
    }
}

完整程式碼

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
namespace HelloWorld
{
    //情況2:對數據的輸入/輸出格式進行設置
    class Clock
    {
        private double seconds = 0.0;

        //Hours獲取小時數據/設置小時數據
        public double Hours
        {
            get { return seconds / 3600; }
            set { seconds = value * 3600; }
        }
        //Minutes獲取分鐘數據/設置分鐘數據
        public double Minutes
        {
            get { return seconds / 60; }
            set { seconds = value * 60; }
        }
    }

    //情況1:用於對數據進行校驗
    class Student
    {
        public string Name { get; set; }

        private int score = 0;
        //情況1:用於對數據進行校驗
        public int Score
        {
            get { return score; }
            set
            {
                //傳人的分數必須介於0-100之間
                if (value < 0 || value > 100)
                {
                    Console.WriteLine("分數必須介於0-100之間");
                    return;
                }
                score = value;
            }
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Clock clock = new Clock();
            clock.Hours = 10;
            Console.WriteLine(clock.Hours);
            Console.WriteLine(clock.Minutes);

            //Student student = new Student();
            //student.Score = -100;
            //Console.WriteLine(student.Score);
        }
    }
}

https://www.bilibili.com/video/BV1Ph4y1W7Ge

This post is licensed under CC BY 4.0 by the author.