Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

Constructor Overloading ใน C#: Constructor หลายรูปแบบ

Constructor Overloading คือการประกาศ Constructor หลายตัวในคลาสเดียวกัน แต่มี Parameter ต่างกัน (จำนวน, ประเภท, ลำดับ) ทำให้สร้าง Object ด้วยวิธีต่างๆ ได้

ตัวอย่าง: Student Class

using System;

namespace Program
{
    class Student
    {
        private double subjectOne;
        private double subjectTwo;
        private string studentName;
        
        // Constructor 1: Default
        public Student()
        {
            this.subjectOne = 80;
        }
        
        // Constructor 2: 1 Parameter
        public Student(double subjectOne)
        {
            this.subjectOne = subjectOne;
        }
        
        // Constructor 3: 3 Parameters
        public Student(string studentName, double subjectOne, double subjectTwo)
        {
            this.studentName = studentName;
            this.subjectOne = subjectOne;
            this.subjectTwo = subjectTwo;
        }
        
        // Properties/Methods
        public double GetSubjectOneMarks() => this.subjectOne;
        public double GetSubjectTwoMarks() => this.subjectTwo;
        public string GetStudentName() => this.studentName;
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Student s1 = new Student();                    // ใช้ Default
            Student s2 = new Student(90);                  // ใช้ 1 Param
            Student s3 = new Student("Amit", 88, 60);      // ใช้ 3 Params
            
            Console.WriteLine("Student 1:");
            Console.WriteLine($"Subject One: {s1.GetSubjectOneMarks()}");
            
            Console.WriteLine("\nStudent 2:");
            Console.WriteLine($"Subject One: {s2.GetSubjectOneMarks()}");
            
            Console.WriteLine("\nStudent 3:");
            Console.WriteLine($"Name: {s3.GetStudentName()}");
            Console.WriteLine($"Subject One: {s3.GetSubjectOneMarks()}");
            Console.WriteLine($"Subject Two: {s3.GetSubjectTwoMarks()}");
            
            Console.ReadKey();
        }
    }
}

ผลลัพธ์

Student 1:
Subject One: 80

Student 2:
Subject One: 90

Student 3:
Name: Amit
Subject One: 88
Subject Two: 60

Constructor Chaining (ลดโค้ดซ้ำ)

public class Student
{
    public string Name { get; }
    public double SubjectOne { get; }
    public double SubjectTwo { get; }
    
    // Primary Constructor (มากที่สุด Parameter)
    public Student(string name, double subjectOne, double subjectTwo)
    {
        Name = name;
        SubjectOne = subjectOne;
        SubjectTwo = subjectTwo;
    }
    
    // Chaining ไปยัง Primary
    public Student(string name, double subjectOne) : this(name, subjectOne, 0) { }
    public Student(string name) : this(name, 0, 0) { }
    public Student() : this("Unknown", 0, 0) { }
}

ข้อดีของ Constructor Overloading

  • ความยืดหยุ่นในการสร้าง Object
  • Default Values สำหรับ Optional Parameters
  • Backward Compatibility - เพิ่ม Constructor ใหม่ไม่ทำลายโค้ดเก่า
  • ชัดเจน - อ่านชื่อ Parameter รู้ว่าต้องส่งอะไรบ้าง

ทางเลือกสมัยใหม่: ใช้ Optional Parameters หรือ Primary Constructors (C# 12) แทน Overloading หลายกรณี