本帖最後由 李泳霖 於 2024-1-9 11:20 編輯
建構方法-->每創建一個物件都會呼叫的方法,運用在初始化內
建立Person類別,並使用建構方法進行初始化
- //class 、 object
- using System;
- class Program
- {
- static void Main()
- {
- Person person1 = new Person(180.3,30,"Kevin");
- Person person2 = new Person(158.5, 16, "Amy");
- Console.WriteLine(person1.name);
- Console.WriteLine(person1.age);
- Console.WriteLine(person1.height);
- Console.WriteLine("=========================");
- Console.WriteLine(person1.name);
- Console.WriteLine(person1.age);
- Console.WriteLine(person1.height);
- }
- }
複製代碼- using System;
- class Person
- {
- public double height;
- public int age;
- public string name;
- public Person(double height, int age, string name)//建構方法,並不可回傳資料
- {
- this.height = height;
- this.age = age;
- this.name = name;
- }
- }
複製代碼 |