本帖最後由 李泳霖 於 2024-1-17 22:24 編輯
定義一 Car 類別,並在 Car 類別下建立三個實體物件,分別為 bus、truck 與 taxi。
執行參考畫面如下:
- using ConsoleApp1;
- using System;//程式庫呼叫
- using System.ComponentModel.DataAnnotations;
- using System.Linq.Expressions;
- namespace HelloWorld//創建一個程式庫(自己定義)
- {
- class Program//負責一部分工作的人
- {
-
- static void Main()//method ..Entry Point 程式進入點
- {
- Car bus = new Car(); //在Car類別下新增一個名為bus的物件, 該物件尚未初始化
-
- bus.name = "公車"; //對bus進行初始化
- bus.wheel = 6;
- bus.load = 40;
- Car truck = new Car();
- truck.name = "卡車";
- truck.wheel = 8;
- truck.load = 3;
- Car taxi = new Car();
- taxi.name = "計程車";
- taxi.wheel = 4;
- taxi.load = 5;
- Console.WriteLine(bus.name + "有" + bus.wheel + "個輪子,可載" + bus.load + "人.");
- Console.WriteLine(truck.name + "有" + truck.wheel + "個輪子,可載" + truck.load + "人.");
- Console.WriteLine(taxi.name + "有" + taxi.wheel + "個輪子,可載" + taxi.load + "人.");
- }
- }
- }
複製代碼- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- class Car
- {
- public string name; //宣告該類別擁有哪些屬性
- public int wheel;
- public int load;
- }
- }
複製代碼 |