定義一 Family 類別,並在Family類別下建立四個實體物件,分別為man、woman、boy 與 girl。同時,於Family類別下定義 showProfile() 方法用來顯示每位家庭成員的描述,並於 main 方法中呼叫它。
執行參考畫面如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1
- {
- class Family
- {
- public string name;
- public int age;
- public string hobby;
- public Family(string n, int a, string h)
- {
- name = n;
- age = a;
- hobby = h;
- }
- public void showProfile()
- {
- Console.WriteLine(name + "今年" + age + "歲,喜歡" + hobby);
- }
- }
- }
複製代碼- using ConsoleApp1;
- using System;//程式庫呼叫
- using System.ComponentModel.DataAnnotations;
- using System.Linq.Expressions;
- namespace HelloWorld//創建一個程式庫(自己定義)
- {
- class Program//負責一部分工作的人
- {
-
- static void Main()//method ..Entry Point 程式進入點
- {
- Family man = new Family("爸爸", 40, "爬山");
- Family woman = new Family("媽媽", 38, "逛街");
- Family boy = new Family("男孩", 10, "打電動");
- Family girl = new Family("女孩", 8, "看書");
- man.showProfile();
- woman.showProfile();
- boy.showProfile();
- girl.showProfile();
- }
- }
- }
複製代碼 |