C# - C Sharp: Truy cập hàm tạo của lớp cha từ lớp con
Giải phóng thời gian, khai phóng năng lực
Từ lớp con (lớp thừa kế) ta có thể truy cập đến hàm tạo của lớp cha bằng cách sử dụng từ khóa base. Nếu là hàm tạo không tham số thì hệ thống sẽ ngầm định tự động gọi cho ta.
Ví dụ ta có một lớp Animal2 trong đó có hai hàm tạo là hàm tạo không tham số và hàm tạo đầy đủ tham số như sau:
class Animal2 { int id; string? name; float weight; string? gender; public int Id { get => id; set => id = value; } public string? Name { get => name; set => name = value; } public float Weight { get => weight; set => weight = value; } public string? Gender { get => gender; set => gender = value; } //hàm tạo không tham số public Animal2() { id = 1234; name = "Moon"; weight = 1.5f; gender = "Female"; } //hàm tạo đầy đủ tham số public Animal2(int id, string name, float weight, string gender) { this.id = id; this.name = name; this.weight = weight; this.gender = gender; } public void InputInfo() { Console.WriteLine("\nInput information for cat:"); Console.Write("Input id: "); id = int.Parse(Console.ReadLine()); Console.Write("Input name: "); name = Console.ReadLine(); Console.Write("Input weight: "); weight = float.Parse(Console.ReadLine()); Console.Write("Input gender: "); gender = Console.ReadLine(); } public void ShowInfo() { Console.WriteLine("\nShow information of cat:"); Console.WriteLine($"Id: {id}"); Console.WriteLine($"Name: {name}"); Console.WriteLine($"Weight: {weight}"); Console.WriteLine($"Gender: {gender}"); } }
Và ta có lớp Cat2 thừa kế từ lớp Animal2:
class Cat2 : Animal2 { string? color; public string? Color { get => color; set => color = value; } public new void InputInfo() { base.InputInfo(); Console.Write("Input color: "); color = Console.ReadLine(); } public new void ShowInfo() { base.ShowInfo(); Console.WriteLine($"Color: {color}"); } }
Nhắc lại rằng lớp con không được thừa kế các hàm tạo từ lớp cha.
Thì khi ta tạo một thể hiện (đối tượng) từ lớp Cat2 và đối tượng này gọi tới hàm tạo không tham số nhưng ta không xây dựng hàm tạo không tham số của Cat2 thì hệ thống sẽ tự tạo hàm tạo không tham số Cat2() và đồng thời cũng tự động gọi đến hàm tạo không tham số Animal2() của lớp Animal2.
Lúc này ở lớp chứa phương thức Main() ta làm như sau:
internal class Demo { static void Main(string[] args) { Cat2 cat = new Cat2(); cat.ShowInfo();//hiển thị thông tin của cat } }
Thì khi chạy ta sẽ được kết quả:
Show information of cat: Id: 1234 Name: Moon Weight: 1.5 Gender: Female Color:
Khi này ta thấy rằng id, name, weight và gender của đối tượng cat sẽ vẫn nhận được giá trị khởi tạo ban đầu từ hàm tạo không tham số Animal2() của lớp cha Animal2. Nhưng trường color chỉ nhận được chuỗi rỗng.
Nếu như ta muốn trường color cũng nhận được giá trị khởi tạo ban đầu thì ta cần định nghĩa hàm tạo không tham số Cat2() cho lớp Cat2 như sau:
class Cat2 : Animal2 { string? color; public string? Color { get => color; set => color = value; } //hàm tạo không tham số của Cat2 public Cat2() { color = "White"; } public new void InputInfo() { base.InputInfo(); Console.Write("Input color: "); color = Console.ReadLine(); } public new void ShowInfo() { base.ShowInfo(); Console.WriteLine($"Color: {color}"); } }
Khi này ta chạy lại chương trình ta được kết quả như sau:
Show information of cat: Id: 1234 Name: Moon Weight: 1.5 Gender: Female Color: White
Và nếu muốn ta cũng phải xây dựng hàm tạo đầy đủ tham số (bốn tham số id, name, weight, gender thừa kế từ lớp cha Animal2 và một tham số color của Cat2) cho lớp con Cat2.
Tuy nhiên, nếu ta muốn tận dụng hàm tạo đầy đủ tham số của lớp cha Animal2 thay vì phải xây dựng từ đầu, thì lúc này ta có thể làm như sau:
class Cat2 : Animal2 { string? color; public string? Color { get => color; set => color = value; } //hàm tạo không tham số của Cat2 public Cat2() { color = "White"; } //hàm tạo đầy đủ tham số của Cat2 public Cat2(int id, string name, float weight, string gender, string color) : base(id, name, weight, gender) { this.color = color; } public new void InputInfo() { base.InputInfo(); Console.Write("Input color: "); color = Console.ReadLine(); } public new void ShowInfo() { base.ShowInfo(); Console.WriteLine($"Color: {color}"); } }
Trong đoạn code trên, từ hàm tạo đầy đủ tham số của lớp con Cat2 ta đã gọi tới hàm tạo đầy đủ tham số của lớp cha Animal2 theo cách thức như sau: base(id, name, weight, gender). Với lời gọi này thì các trường id, name, weight và gender đã được khởi tạo giá trị; còn lại trường color thì ở thân của hàm tạo đầy đủ tham số của lớp Cat2 đã chứa lệnh để khởi tạo: this.color = color;
Sau đây là ví dụ đầy đủ, bạn có thể copy và chạy thử:
namespace ConsoleApp1 { class Animal2 { int id; string? name; float weight; string? gender; public int Id { get => id; set => id = value; } public string? Name { get => name; set => name = value; } public float Weight { get => weight; set => weight = value; } public string? Gender { get => gender; set => gender = value; } //hàm tạo không tham số public Animal2() { id = 1234; name = "Moon"; weight = 1.5f; gender = "Female"; } //hàm tạo đầy đủ tham số public Animal2(int id, string name, float weight, string gender) { this.id = id; this.name = name; this.weight = weight; this.gender = gender; } public void InputInfo() { Console.WriteLine("\nInput information for cat:"); Console.Write("Input id: "); id = int.Parse(Console.ReadLine()); Console.Write("Input name: "); name = Console.ReadLine(); Console.Write("Input weight: "); weight = float.Parse(Console.ReadLine()); Console.Write("Input gender: "); gender = Console.ReadLine(); } public void ShowInfo() { Console.WriteLine("\nShow information of cat:"); Console.WriteLine($"Id: {id}"); Console.WriteLine($"Name: {name}"); Console.WriteLine($"Weight: {weight}"); Console.WriteLine($"Gender: {gender}"); } } class Cat2 : Animal2 { string? color; public string? Color { get => color; set => color = value; } //hàm tạo không tham số của Cat2 public Cat2() { color = "White"; } //hàm tạo đầy đủ tham số của Cat2 public Cat2(int id, string name, float weight, string gender, string color) : base(id, name, weight, gender) { this.color = color; } public new void InputInfo() { base.InputInfo(); Console.Write("Input color: "); color = Console.ReadLine(); } public new void ShowInfo() { base.ShowInfo(); Console.WriteLine($"Color: {color}"); } } internal class Demo { static void Main(string[] args) { //dùng hàm tạo 0 tham số Cat2 cat = new Cat2(); cat.ShowInfo(); //dùng hàm tạo đầy đủ tham số cat = new Cat2(5678, "Bengal", 2.5f, "Male", "Spotted brown"); cat.ShowInfo(); } } }
Kết quả:
Show information of cat: Id: 1234 Name: Moon Weight: 1.5 Gender: Female Color: White Show information of cat: Id: 5678 Name: Bengal Weight: 2.5 Gender: Male Color: Spotted brown
Lưu ý: Nếu hàm tạo không gọi tường minh tới hàm tạo của lớp cha thì trình dịch C# sẽ tự động chèn một lời gọi tới hàm tạo không đối số của lớp cha. Lúc này, nếu lớp cha mà không có hàm tạo không tham số thì bạn sẽ nhận được một lỗi.
Giải phóng thời gian, khai phóng năng lực