C#(class,プロパティ)

C#

Javaとの違いに注目しながらクラスを作ってみよう。

●Person.cs

01using System;
02namespace ClassLesson
03{
04    public class Person
05    {
06        private string name;
07        private int age;
08 
09        //プロパティ
10        public string Name
11        {
12            set
13            {
14                this.name = value;
15            }
16            get
17            {
18                return this.name;
19            }
20 
21        }
22 
23        public int Age
24        {
25            set
26            {
27                this.age = value;
28            }
29            get
30            {
31                return this.age;
32            }
33        }
34        public void ShowInfo()
35        {
36            Console.WriteLine($"名前:{this.name},年齢:{this.age}");
37        }
38 
39    }
40}

●Program.cs

01using System;
02 
03namespace ClassLesson
04{
05    class Program
06    {
07        static void Main(string[] args)
08        {
09            //オブジェクト初期化子で初期化
10            Person person = new Person()
11            {
12                Name = "山田",
13                Age = 18
14            };
15            Console.WriteLine(person.Name);
16            Console.WriteLine(++person.Age);
17            person.ShowInfo();
18 
19        }
20    }
21}

プロパティにアクセス制限

01using System;
02namespace ClassLesson
03{
04    public class Person
05    {
06        private string name;
07        private int age;
08 
09        //コンストラクタ
10        public Person(string name){
11            this.name = name;
12        }
13 
14        public Person(string name, int age):this(name)
15        {
16            this.age = age;
17        }
18 
19        //プロパティ
20        public string Name
21        {
22            set
23            {
24                this.name = value;
25            }
26            get
27            {
28                return this.name;
29            }
30 
31        }
32 
33        public int Age
34        {
35            //setterをprivateにする
36            private set
37            {
38                this.age = value;
39            }
40            get
41            {
42                return this.age;
43            }
44        }
45        public void AgePlus()
46        {
47            this.age++;
48        }
49        public void ShowInfo()
50        {
51            Console.WriteLine($"名前:{this.name},年齢:{this.age}");
52        }
53 
54 
55    }
56}
01using System;
02 
03namespace ClassLesson
04{
05    class Program
06    {
07        static void Main(string[] args)
08        {
09            //コンストラクタで初期化
10            Person person = new Person("田中", 18);
11 
12            Console.WriteLine(person.Name);
13            // person.Age++;
14            person.AgePlus();
15            Console.WriteLine(person.Age);
16 
17        }
18    }
19}

setter,getterで特にやる処理がない場合は自動プロパティが便利

01using System;
02namespace ClassLesson
03{
04    public class Person
05    {
06        //自動プロパティ
07        public string Name { get; set; }
08        //初期値も指定できる
09        //public string Name { get; set; } = "ごんべ";
10        public int Age { get; set; }
11        public void ShowInfo()
12        {
13            Console.WriteLine($"名前:{this.Name},年齢:{this.Age}");
14        }
15 
16    }
17}

コメント

タイトルとURLをコピーしました