static 屬性為屬於class本身的,利用static物件來記錄總共創建了幾個video物件
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- class Video
- {
- public string title;
- public string author;
- //影片類型有四種:教育、娛樂、音樂、其他
- //public string type;//類型,限制屬性存取
- private string type;
- //static 屬性為屬於class本身的
- //例如:表總共幾個video物件
- public static int video_count = 0;
- public Video(string title, string author, string type)
- {
- this.title = title;
- this.author = author;
- Type = type;
- video_count++;
- }
- //Type為type的對外代理人,想要在Video以外的類別存取,就需要透過Type
- public string Type
- {
- get//取得影片類型
- {
- return type;
- }
- set//限制只能有四種類型
- {//value為設定的值
- if (value == "教育" || value == "娛樂" || value == "音樂" || value == "其它")
- {
- type = value;
- }
- else//若不再以上四種,強制改為其它
- {
- type = "其它";
- }
- }
- }
- }
- }
複製代碼- using ConsoleApp1;
- using System;
- using System.Linq.Expressions;
- class Program
- {
-
- static void Main()
- {
- string v1, a1, t1;
- Console.Write("請輸入影片名稱:");
- v1 = System.Console.ReadLine();
- Console.Write("請輸入作者:");
- a1 = System.Console.ReadLine();
- Console.Write("請輸入影片類型(教育、娛樂、音樂、其它): ");//需控制亂輸入
- t1 = System.Console.ReadLine();
- Video video1 = new Video(v1, a1, t1);
- Console.WriteLine("==========================================");
- Console.WriteLine("影片名稱為:" + video1.title);
- Console.WriteLine("影片作者為:" + video1.author);
- //Console.WriteLine("影片類型為:" + video1.type);//出錯,type為private
- Console.WriteLine("影片類型為:" + video1.Type);
- //Video video2 = new Video("獅子王", " 羅伯‧民可夫", "娛樂");
- // Console.WriteLine(video1.Type);//不合理不可存取
- Console.WriteLine("總共" + Video.video_count + "個物件");
- }
- }
複製代碼 |