本帖最後由 李泳霖 於 2024-1-9 11:20 編輯
定義一 Person類別,並在 Person類別下建立兩個實體物件,分別為 person1、person2。
執行參考畫面如下:
- class Person
- {
- public double height;
- public int age;
- public string name;
- }
複製代碼- //class 、 object
- Person person1 = new Person();
- person1.height = 150;
- person1.age = 42;
- person1.name = "大大";
- Person person2 = new Person();
- person2.height = 180;
- person2.age = 20;
- person2.name = "小小";
- System.Console.WriteLine("姓名: "+person1.name);
- System.Console.WriteLine("年齡: " + person1.age);
- System.Console.WriteLine("身高: " + person1.height);
- System.Console.WriteLine("==================================");
- System.Console.WriteLine("姓名: " + person2.name);
- System.Console.WriteLine("年齡: " + person2.age);
- System.Console.WriteLine("身高: " + person2.height);
複製代碼 |