Trang chủ
Bài viết mới
Diễn đàn
Bài mới trên hồ sơ
Hoạt động mới nhất
VIDEO
Mùa Tết
Văn Học Trẻ
Văn Học News
Media
New media
New comments
Search media
Đại Học
Đại cương
Chuyên ngành
Triết học
Kinh tế
KHXH & NV
Công nghệ thông tin
Khoa học kĩ thuật
Luận văn, tiểu luận
Phổ Thông
Lớp 12
Ngữ văn 12
Lớp 11
Ngữ văn 11
Lớp 10
Ngữ văn 10
LỚP 9
Ngữ văn 9
Lớp 8
Ngữ văn 8
Lớp 7
Ngữ văn 7
Lớp 6
Ngữ văn 6
Tiểu học
Thành viên
Thành viên trực tuyến
Bài mới trên hồ sơ
Tìm trong hồ sơ cá nhân
Credits
Transactions
Xu: 0
Đăng nhập
Đăng ký
Có gì mới?
Tìm kiếm
Tìm kiếm
Chỉ tìm trong tiêu đề
Bởi:
Hoạt động mới nhất
Đăng ký
Menu
Đăng nhập
Đăng ký
Install the app
Cài đặt
Chào mừng Bạn tham gia Diễn Đàn VNKienThuc.com -
Định hướng Forum
Kiến Thức
- HÃY TẠO CHỦ ĐỀ KIẾN THỨC HỮU ÍCH VÀ CÙNG NHAU THẢO LUẬN Kết nối:
VNK X
-
VNK groups
| Nhà Tài Trợ:
BhnongFood X
-
Bhnong groups
-
Đặt mua Bánh Bhnong
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Tìm hiểu về nạp chồng toán tử trong C#
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Trả lời chủ đề
Nội dung
<blockquote data-quote="uocmo_kchodoi" data-source="post: 177536" data-attributes="member: 165510"><p style="text-align: center"><strong><span style="color: #ff0000">NẠP CHỒNG TOÁN TỬ TRONG C#</span></strong></p> <p style="text-align: center"><strong><span style="color: #ff0000"></span></strong></p><p><strong>Khái niệm: </strong>Operator Overloading là nạp chồng toán tử - cho phép các lớp người dùng định nghĩa có thể có các chức năng như kiểu có sẵn do ngôn ngữ lập trình đã định nghĩa trước đó.</p><p></p><p>Nạp chồng toán tử là thay việc gán được sử dụng trực tiếp các toán tử bình thường như: >=, <=, ==, +, -.....giờ phải dùng hàm Operator để gọi đến các toán tử này. </p><p></p><p>Ví dụ, bạn xét hàm sau:</p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3">public static Box operator+ (Box b, Box c)</span></em></p><p><em><span style="color: #5900b3">{</span></em></p><p><em><span style="color: #5900b3"> Box box = new Box();</span></em></p><p><em><span style="color: #5900b3"> box.chieu_dai = b.chieu_dai + c.chieu_dai;</span></em></p><p><em><span style="color: #5900b3"> box.chieu_rong = b.chieu_rong + c.chieu_rong;</span></em></p><p><em><span style="color: #5900b3"> box.chieu_cao = b.chieu_cao + c.chieu_cao;</span></em></p><p><em><span style="color: #5900b3"> return box;</span></em></p><p><em><span style="color: #5900b3">}</span></em></p><p>Hàm trên triển khai toán tử cộng (+) cho một lớp Box tự định nghĩa (user-defined). Nó cộng các thuộc tính của hai đối tượng Box và trả về đối tượng kết quả Box.</p><p></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>1. Triển khai Nạp chồng toán tử trong C#</strong></span></span></p><p><span style="font-size: 18px"></span></p><p>Ví dụ dưới đây minh họa cách triển khai nạp chồng toán tử trong C#: tạo hai lớp có tên lần lượt là Box, TestCsharp như sau:</p><p></p><p>Lớp Box: chứa các thuộc tính và phương thức</p><p></p><p><em><span style="color: #5900b3">using System;</span></em></p><p><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></p><p><em><span style="color: #5900b3">{</span></em></p><p><em><span style="color: #5900b3"> class Box</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> private double chieu_dai; </span></em></p><p><em><span style="color: #5900b3"> private double chieu_rong; </span></em></p><p><em><span style="color: #5900b3"> private double chieu_cao; </span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> public double tinhTheTich()</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> return chieu_dai * chieu_rong * chieu_cao;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> public void setChieuDai(double len)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> chieu_dai = len;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> public void setChieuRong(double bre)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> chieu_rong = bre;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> public void setChieuCao(double hei)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> chieu_cao = hei;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> // nap chong toan tu + de cong hai doi tuong Box.</span></em></p><p><em><span style="color: #5900b3"> public static Box operator +(Box b, Box c)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Box box = new Box();</span></em></p><p><em><span style="color: #5900b3"> box.chieu_dai = b.chieu_dai + c.chieu_dai;</span></em></p><p><em><span style="color: #5900b3"> box.chieu_rong = b.chieu_rong + c.chieu_rong;</span></em></p><p><em><span style="color: #5900b3"> box.chieu_cao = b.chieu_cao + c.chieu_cao;</span></em></p><p><em><span style="color: #5900b3"> return box;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3">}</span></em></p><p>Lớp TestCsharp: chứa phương thức main() để thao tác trên đối tượng Box</p><p></p><p><em><span style="color: #5900b3">using System;</span></em></p><p><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></p><p><em><span style="color: #5900b3">{</span></em></p><p><em><span style="color: #5900b3"> public class TestCsharp</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> static void Main(string[] args)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Nap chong toan tu trong C#");</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Vi du minh hoa nap chong toan tu");</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("--------------------------");</span></em></p><p><em><span style="color: #5900b3"> //tao cac doi tuong Box1, Box2 va Box3</span></em></p><p><em><span style="color: #5900b3"> Box Box1 = new Box(); </span></em></p><p><em><span style="color: #5900b3"> Box Box2 = new Box(); </span></em></p><p><em><span style="color: #5900b3"> Box Box3 = new Box(); </span></em></p><p><em><span style="color: #5900b3"> double the_tich = 0.0; </span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> // nhap thong tin Box1</span></em></p><p><em><span style="color: #5900b3"> Box1.setChieuDai(6.0);</span></em></p><p><em><span style="color: #5900b3"> Box1.setChieuRong(7.0);</span></em></p><p><em><span style="color: #5900b3"> Box1.setChieuCao(5.0);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> // nhap thong tin Box2</span></em></p><p><em><span style="color: #5900b3"> Box2.setChieuDai(12.0);</span></em></p><p><em><span style="color: #5900b3"> Box2.setChieuRong(13.0);</span></em></p><p><em><span style="color: #5900b3"> Box2.setChieuCao(10.0);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> // tinh va hien thi the tich Box1</span></em></p><p><em><span style="color: #5900b3"> the_tich = Box1.tinhTheTich();</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("The tich cua Box1 la: {0}", the_tich);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> // tinh va hien thi the tich Box2</span></em></p><p><em><span style="color: #5900b3"> the_tich = Box2.tinhTheTich();</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("The tich cua Box2 la: {0}", the_tich);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> // con hai doi tuong</span></em></p><p><em><span style="color: #5900b3"> Box3 = Box1 + Box2;</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> // tinh va hien thi the tich Box3</span></em></p><p><em><span style="color: #5900b3"> the_tich = Box3.tinhTheTich();</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("The tich cua Box3 la: {0}", the_tich);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> Console.ReadKey();</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3">}</span></em></p><p></p><p>Nếu bạn không sử dụng lệnh Console.ReadKey(); thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn.</p><p></p><p>Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</p><p></p><p style="text-align: center"><img src="https://vietjack.com/csharp/images/nap-chong-toan-tu.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></p> <p style="text-align: center"></p><p><span style="font-size: 18px"><span style="color: #0000ff"><strong>2. Toán tử có thể nạp chồng và không thể nạp chồng trong C#</strong></span></span></p><p><span style="font-size: 18px"><span style="color: #0000ff"></span></span></p><p>Các toán tử có thể nạp chồng trong C#:</p><p></p><p>1. +, -, !, ~, ++, -- </p><p>Những toán tử một ngôi này nhận một toán hạng và có thể được nạp chồng</p><p>2. +, -, *, /, %</p><p>Những toán tử nhị phân này nhận một toán hạng và có thể được nạp chồng</p><p>3. =, !=, <, >, <=, >= </p><p>Các toán tử so sánh có thể được nạp chồng</p><p>4. &&, || </p><p>Các toán tử logic điều kiện không thể được nạp chồng một cách trực tiếp</p><p>5. +=, -=, *=, /=, %= </p><p>Các toán tử gán không thể được nạp chồng</p><p>6. =, ., ?:, ->, new, is, sizeof, typeof </p><p>Các toán tử này không thể được nạp chồng</p><p></p><p><span style="font-size: 18px"><strong>Ví dụ</strong></span></p><p><span style="font-size: 18px"></span></p><p>Từ các khái niệm trên, chúng ta kế thừa ví dụ trên và nạp chồng thêm một số toán tử trong C#: ở trên chúng ta đã tạo hai lớp Box, TestCsharp, bây giờ chúng ta sửa lại code của mỗi lớp như sau:</p><p></p><p>Lớp Box: chứa các thuộc tính và phương thức</p><p></p><p><em><span style="color: #5900b3">using System;</span></em></p><p><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></p><p><em><span style="color: #5900b3">{</span></em></p><p><em><span style="color: #5900b3"> class Box</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> private double chieu_dai; </span></em></p><p><em><span style="color: #5900b3"> private double chieu_rong; </span></em></p><p><em><span style="color: #5900b3"> private double chieu_cao; </span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> public double tinhTheTich()</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> return chieu_dai * chieu_rong * chieu_cao;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> public void setChieuDai(double len)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> chieu_dai = len;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> public void setChieuRong(double bre)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> chieu_rong = bre;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> public void setChieuCao(double hei)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> chieu_cao = hei;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> // nap chong toan tu +</span></em></p><p><em><span style="color: #5900b3"> public static Box operator +(Box b, Box c)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Box box = new Box();</span></em></p><p><em><span style="color: #5900b3"> box.chieu_dai = b.chieu_dai + c.chieu_dai;</span></em></p><p><em><span style="color: #5900b3"> box.chieu_rong = b.chieu_rong + c.chieu_rong;</span></em></p><p><em><span style="color: #5900b3"> box.chieu_cao = b.chieu_cao + c.chieu_cao;</span></em></p><p><em><span style="color: #5900b3"> return box;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> //nap chong toan tu ==</span></em></p><p><em><span style="color: #5900b3"> public static bool operator ==(Box lhs, Box rhs)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> bool status = false;</span></em></p><p><em><span style="color: #5900b3"> if (lhs.chieu_dai == rhs.chieu_dai && lhs.chieu_cao == rhs.chieu_cao && lhs.chieu_rong == rhs.chieu_rong)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> status = true;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> return status;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> //nap chong toan tu !=</span></em></p><p><em><span style="color: #5900b3"> public static bool operator !=(Box lhs, Box rhs)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> bool status = false;</span></em></p><p><em><span style="color: #5900b3"> if (lhs.chieu_dai != rhs.chieu_dai || lhs.chieu_cao != rhs.chieu_cao || lhs.chieu_rong != rhs.chieu_rong)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> status = true;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> return status;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> //nap chong toan tu <</span></em></p><p><em><span style="color: #5900b3"> public static bool operator <(Box lhs, Box rhs)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> bool status = false;</span></em></p><p><em><span style="color: #5900b3"> if (lhs.chieu_dai < rhs.chieu_dai && lhs.chieu_cao < rhs.chieu_cao && lhs.chieu_rong < rhs.chieu_rong)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> status = true;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> return status;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> //nap chong toan tu ></span></em></p><p><em><span style="color: #5900b3"> public static bool operator >(Box lhs, Box rhs)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> bool status = false;</span></em></p><p><em><span style="color: #5900b3"> if (lhs.chieu_dai > rhs.chieu_dai && lhs.chieu_cao > rhs.chieu_cao && lhs.chieu_rong > rhs.chieu_rong)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> status = true;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> return status;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> //nap chong toan tu <=</span></em></p><p><em><span style="color: #5900b3"> public static bool operator <=(Box lhs, Box rhs)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> bool status = false;</span></em></p><p><em><span style="color: #5900b3"> if (lhs.chieu_dai <= rhs.chieu_dai && lhs.chieu_cao <= rhs.chieu_cao && lhs.chieu_rong <= rhs.chieu_rong)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> status = true;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> return status;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> //nap chong toan tu >=</span></em></p><p><em><span style="color: #5900b3"> public static bool operator >=(Box lhs, Box rhs)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> bool status = false;</span></em></p><p><em><span style="color: #5900b3"> if (lhs.chieu_dai >= rhs.chieu_dai && lhs.chieu_cao >= rhs.chieu_cao && lhs.chieu_rong >= rhs.chieu_rong)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> status = true;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> return status;</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> //nap chong phuong thuc ToString()</span></em></p><p><em><span style="color: #5900b3"> public override string ToString()</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> return String.Format("({0}, {1}, {2})", chieu_dai, chieu_rong, chieu_cao);</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3">}</span></em></p><p>Lớp TestCsharp: chứa phương thức main() để thao tác trên đối tượng Box</p><p></p><p><em><span style="color: #5900b3">using System;</span></em></p><p><em><span style="color: #5900b3">namespace VietJackCsharp</span></em></p><p><em><span style="color: #5900b3">{</span></em></p><p><em><span style="color: #5900b3"> public class TestCsharp</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> static void Main(string[] args)</span></em></p><p><em><span style="color: #5900b3"> {</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Nap chong toan tu trong C#");</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Vi du minh hoa nap chong toan tu");</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("--------------------------");</span></em></p><p><em><span style="color: #5900b3"> //tao cac doi tuong Box1, Box2, Box2, Box4</span></em></p><p><em><span style="color: #5900b3"> Box Box1 = new Box(); </span></em></p><p><em><span style="color: #5900b3"> Box Box2 = new Box(); </span></em></p><p><em><span style="color: #5900b3"> Box Box3 = new Box(); </span></em></p><p><em><span style="color: #5900b3"> Box Box4 = new Box();</span></em></p><p><em><span style="color: #5900b3"> double the_tich = 0.0; </span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> // nhap thong tin Box1</span></em></p><p><em><span style="color: #5900b3"> Box1.setChieuDai(6.0);</span></em></p><p><em><span style="color: #5900b3"> Box1.setChieuRong(7.0);</span></em></p><p><em><span style="color: #5900b3"> Box1.setChieuCao(5.0);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> // nhap thong tin Box2</span></em></p><p><em><span style="color: #5900b3"> Box2.setChieuDai(12.0);</span></em></p><p><em><span style="color: #5900b3"> Box2.setChieuRong(13.0);</span></em></p><p><em><span style="color: #5900b3"> Box2.setChieuCao(10.0);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> //hien thi thong tin cac Box boi su dung phuong thuc nap chong ToString():</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Thong tin Box 1: {0}", Box1.ToString());</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Thong tin Box 2: {0}", Box2.ToString());</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> // tinh the tich Box1</span></em></p><p><em><span style="color: #5900b3"> the_tich = Box1.tinhTheTich();</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("The tich cua Box1 la: {0}", the_tich);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> // tinh the tich Box2</span></em></p><p><em><span style="color: #5900b3"> the_tich = Box2.tinhTheTich();</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("The tich cua Box2 la: {0}", the_tich);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> // cong hai doi tuong</span></em></p><p><em><span style="color: #5900b3"> Box3 = Box1 + Box2;</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Thong tin Box 3: {0}", Box3.ToString());</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> // tinh the tich cua Box3</span></em></p><p><em><span style="color: #5900b3"> the_tich = Box3.tinhTheTich();</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("The tich cua Box3 la: {0}", the_tich);</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> //so sanh cac Box</span></em></p><p><em><span style="color: #5900b3"> if (Box1 > Box2)</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Box1 la lon hon Box2");</span></em></p><p><em><span style="color: #5900b3"> else</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Box1 la khong lon hon Box2");</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> if (Box1 < Box2)</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Box1 la nho hon Box2");</span></em></p><p><em><span style="color: #5900b3"> else</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Box1 la khong nho hon Box2");</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> if (Box1 >= Box2)</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Box1 la lon hon hoac bang Box2");</span></em></p><p><em><span style="color: #5900b3"> else</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Box1 la khong lon hon hoac bang Box2");</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> if (Box1 <= Box2)</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Box1 la nho hon hoac bang Box2");</span></em></p><p><em><span style="color: #5900b3"> else</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Box1 la khong nho hon hoac bang Box2");</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> if (Box1 != Box2)</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Box1 la khong bang Box2");</span></em></p><p><em><span style="color: #5900b3"> else</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Box1 bang Box2");</span></em></p><p><em><span style="color: #5900b3"> Box4 = Box3;</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> if (Box3 == Box4)</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Box3 bamg Box4");</span></em></p><p><em><span style="color: #5900b3"> else</span></em></p><p><em><span style="color: #5900b3"> Console.WriteLine("Box3 la khong bang Box4");</span></em></p><p><em><span style="color: #5900b3"></span></em></p><p><em><span style="color: #5900b3"> Console.ReadKey();</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3"> }</span></em></p><p><em><span style="color: #5900b3">}</span></em></p><p></p><p>Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau:</p><p></p><p style="text-align: center"><img src="https://vietjack.com/csharp/images/nap-chong-toan-tu-1.PNG" alt="" class="fr-fic fr-dii fr-draggable " data-size="" style="" /></p><p></p><p>Trên đây là bài viết giới thiệu về nạp chồng toán tử, hi vọng qua bài viết này các bạn sẽ có thêm nhiều kiến thức để ứng dụng một cách thành thạo vào bài toán của mình. Chúc các bạn học tập tốt!</p><p></p><p style="text-align: right">Nguồn: vietjack.com</p></blockquote><p></p>
[QUOTE="uocmo_kchodoi, post: 177536, member: 165510"] [CENTER][B][COLOR=#ff0000]NẠP CHỒNG TOÁN TỬ TRONG C# [/COLOR][/B][/CENTER] [B]Khái niệm: [/B]Operator Overloading là nạp chồng toán tử - cho phép các lớp người dùng định nghĩa có thể có các chức năng như kiểu có sẵn do ngôn ngữ lập trình đã định nghĩa trước đó. Nạp chồng toán tử là thay việc gán được sử dụng trực tiếp các toán tử bình thường như: >=, <=, ==, +, -.....giờ phải dùng hàm Operator để gọi đến các toán tử này. Ví dụ, bạn xét hàm sau: [I][COLOR=#5900b3] public static Box operator+ (Box b, Box c) { Box box = new Box(); box.chieu_dai = b.chieu_dai + c.chieu_dai; box.chieu_rong = b.chieu_rong + c.chieu_rong; box.chieu_cao = b.chieu_cao + c.chieu_cao; return box; }[/COLOR][/I] Hàm trên triển khai toán tử cộng (+) cho một lớp Box tự định nghĩa (user-defined). Nó cộng các thuộc tính của hai đối tượng Box và trả về đối tượng kết quả Box. [SIZE=5][COLOR=#0000ff][B]1. Triển khai Nạp chồng toán tử trong C#[/B][/COLOR] [B][/B][/SIZE] Ví dụ dưới đây minh họa cách triển khai nạp chồng toán tử trong C#: tạo hai lớp có tên lần lượt là Box, TestCsharp như sau: Lớp Box: chứa các thuộc tính và phương thức [I][COLOR=#5900b3]using System; namespace VietJackCsharp { class Box { private double chieu_dai; private double chieu_rong; private double chieu_cao; public double tinhTheTich() { return chieu_dai * chieu_rong * chieu_cao; } public void setChieuDai(double len) { chieu_dai = len; } public void setChieuRong(double bre) { chieu_rong = bre; } public void setChieuCao(double hei) { chieu_cao = hei; } // nap chong toan tu + de cong hai doi tuong Box. public static Box operator +(Box b, Box c) { Box box = new Box(); box.chieu_dai = b.chieu_dai + c.chieu_dai; box.chieu_rong = b.chieu_rong + c.chieu_rong; box.chieu_cao = b.chieu_cao + c.chieu_cao; return box; } } }[/COLOR][/I] Lớp TestCsharp: chứa phương thức main() để thao tác trên đối tượng Box [I][COLOR=#5900b3]using System; namespace VietJackCsharp { public class TestCsharp { static void Main(string[] args) { Console.WriteLine("Nap chong toan tu trong C#"); Console.WriteLine("Vi du minh hoa nap chong toan tu"); Console.WriteLine("--------------------------"); //tao cac doi tuong Box1, Box2 va Box3 Box Box1 = new Box(); Box Box2 = new Box(); Box Box3 = new Box(); double the_tich = 0.0; // nhap thong tin Box1 Box1.setChieuDai(6.0); Box1.setChieuRong(7.0); Box1.setChieuCao(5.0); // nhap thong tin Box2 Box2.setChieuDai(12.0); Box2.setChieuRong(13.0); Box2.setChieuCao(10.0); // tinh va hien thi the tich Box1 the_tich = Box1.tinhTheTich(); Console.WriteLine("The tich cua Box1 la: {0}", the_tich); // tinh va hien thi the tich Box2 the_tich = Box2.tinhTheTich(); Console.WriteLine("The tich cua Box2 la: {0}", the_tich); // con hai doi tuong Box3 = Box1 + Box2; // tinh va hien thi the tich Box3 the_tich = Box3.tinhTheTich(); Console.WriteLine("The tich cua Box3 la: {0}", the_tich); Console.ReadKey(); } } }[/COLOR][/I] Nếu bạn không sử dụng lệnh Console.ReadKey(); thì chương trình sẽ chạy và kết thúc luôn (nhanh quá đến nỗi bạn không kịp nhìn kết quả). Lệnh này cho phép chúng ta nhìn kết quả một cách rõ ràng hơn. Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau: [CENTER][IMG]https://vietjack.com/csharp/images/nap-chong-toan-tu.PNG[/IMG] [/CENTER] [SIZE=5][COLOR=#0000ff][B]2. Toán tử có thể nạp chồng và không thể nạp chồng trong C#[/B] [B][/B][/COLOR][/SIZE] Các toán tử có thể nạp chồng trong C#: 1. +, -, !, ~, ++, -- Những toán tử một ngôi này nhận một toán hạng và có thể được nạp chồng 2. +, -, *, /, % Những toán tử nhị phân này nhận một toán hạng và có thể được nạp chồng 3. =, !=, <, >, <=, >= Các toán tử so sánh có thể được nạp chồng 4. &&, || Các toán tử logic điều kiện không thể được nạp chồng một cách trực tiếp 5. +=, -=, *=, /=, %= Các toán tử gán không thể được nạp chồng 6. =, ., ?:, ->, new, is, sizeof, typeof Các toán tử này không thể được nạp chồng [SIZE=5][B]Ví dụ[/B] [B][/B][/SIZE] Từ các khái niệm trên, chúng ta kế thừa ví dụ trên và nạp chồng thêm một số toán tử trong C#: ở trên chúng ta đã tạo hai lớp Box, TestCsharp, bây giờ chúng ta sửa lại code của mỗi lớp như sau: Lớp Box: chứa các thuộc tính và phương thức [I][COLOR=#5900b3]using System; namespace VietJackCsharp { class Box { private double chieu_dai; private double chieu_rong; private double chieu_cao; public double tinhTheTich() { return chieu_dai * chieu_rong * chieu_cao; } public void setChieuDai(double len) { chieu_dai = len; } public void setChieuRong(double bre) { chieu_rong = bre; } public void setChieuCao(double hei) { chieu_cao = hei; } // nap chong toan tu + public static Box operator +(Box b, Box c) { Box box = new Box(); box.chieu_dai = b.chieu_dai + c.chieu_dai; box.chieu_rong = b.chieu_rong + c.chieu_rong; box.chieu_cao = b.chieu_cao + c.chieu_cao; return box; } //nap chong toan tu == public static bool operator ==(Box lhs, Box rhs) { bool status = false; if (lhs.chieu_dai == rhs.chieu_dai && lhs.chieu_cao == rhs.chieu_cao && lhs.chieu_rong == rhs.chieu_rong) { status = true; } return status; } //nap chong toan tu != public static bool operator !=(Box lhs, Box rhs) { bool status = false; if (lhs.chieu_dai != rhs.chieu_dai || lhs.chieu_cao != rhs.chieu_cao || lhs.chieu_rong != rhs.chieu_rong) { status = true; } return status; } //nap chong toan tu < public static bool operator <(Box lhs, Box rhs) { bool status = false; if (lhs.chieu_dai < rhs.chieu_dai && lhs.chieu_cao < rhs.chieu_cao && lhs.chieu_rong < rhs.chieu_rong) { status = true; } return status; } //nap chong toan tu > public static bool operator >(Box lhs, Box rhs) { bool status = false; if (lhs.chieu_dai > rhs.chieu_dai && lhs.chieu_cao > rhs.chieu_cao && lhs.chieu_rong > rhs.chieu_rong) { status = true; } return status; } //nap chong toan tu <= public static bool operator <=(Box lhs, Box rhs) { bool status = false; if (lhs.chieu_dai <= rhs.chieu_dai && lhs.chieu_cao <= rhs.chieu_cao && lhs.chieu_rong <= rhs.chieu_rong) { status = true; } return status; } //nap chong toan tu >= public static bool operator >=(Box lhs, Box rhs) { bool status = false; if (lhs.chieu_dai >= rhs.chieu_dai && lhs.chieu_cao >= rhs.chieu_cao && lhs.chieu_rong >= rhs.chieu_rong) { status = true; } return status; } //nap chong phuong thuc ToString() public override string ToString() { return String.Format("({0}, {1}, {2})", chieu_dai, chieu_rong, chieu_cao); } } }[/COLOR][/I] Lớp TestCsharp: chứa phương thức main() để thao tác trên đối tượng Box [I][COLOR=#5900b3]using System; namespace VietJackCsharp { public class TestCsharp { static void Main(string[] args) { Console.WriteLine("Nap chong toan tu trong C#"); Console.WriteLine("Vi du minh hoa nap chong toan tu"); Console.WriteLine("--------------------------"); //tao cac doi tuong Box1, Box2, Box2, Box4 Box Box1 = new Box(); Box Box2 = new Box(); Box Box3 = new Box(); Box Box4 = new Box(); double the_tich = 0.0; // nhap thong tin Box1 Box1.setChieuDai(6.0); Box1.setChieuRong(7.0); Box1.setChieuCao(5.0); // nhap thong tin Box2 Box2.setChieuDai(12.0); Box2.setChieuRong(13.0); Box2.setChieuCao(10.0); //hien thi thong tin cac Box boi su dung phuong thuc nap chong ToString(): Console.WriteLine("Thong tin Box 1: {0}", Box1.ToString()); Console.WriteLine("Thong tin Box 2: {0}", Box2.ToString()); // tinh the tich Box1 the_tich = Box1.tinhTheTich(); Console.WriteLine("The tich cua Box1 la: {0}", the_tich); // tinh the tich Box2 the_tich = Box2.tinhTheTich(); Console.WriteLine("The tich cua Box2 la: {0}", the_tich); // cong hai doi tuong Box3 = Box1 + Box2; Console.WriteLine("Thong tin Box 3: {0}", Box3.ToString()); // tinh the tich cua Box3 the_tich = Box3.tinhTheTich(); Console.WriteLine("The tich cua Box3 la: {0}", the_tich); //so sanh cac Box if (Box1 > Box2) Console.WriteLine("Box1 la lon hon Box2"); else Console.WriteLine("Box1 la khong lon hon Box2"); if (Box1 < Box2) Console.WriteLine("Box1 la nho hon Box2"); else Console.WriteLine("Box1 la khong nho hon Box2"); if (Box1 >= Box2) Console.WriteLine("Box1 la lon hon hoac bang Box2"); else Console.WriteLine("Box1 la khong lon hon hoac bang Box2"); if (Box1 <= Box2) Console.WriteLine("Box1 la nho hon hoac bang Box2"); else Console.WriteLine("Box1 la khong nho hon hoac bang Box2"); if (Box1 != Box2) Console.WriteLine("Box1 la khong bang Box2"); else Console.WriteLine("Box1 bang Box2"); Box4 = Box3; if (Box3 == Box4) Console.WriteLine("Box3 bamg Box4"); else Console.WriteLine("Box3 la khong bang Box4"); Console.ReadKey(); } } }[/COLOR][/I] Biên dịch và chạy chương trình C# trên sẽ cho kết quả sau: [CENTER][IMG]https://vietjack.com/csharp/images/nap-chong-toan-tu-1.PNG[/IMG][/CENTER] Trên đây là bài viết giới thiệu về nạp chồng toán tử, hi vọng qua bài viết này các bạn sẽ có thêm nhiều kiến thức để ứng dụng một cách thành thạo vào bài toán của mình. Chúc các bạn học tập tốt! [RIGHT]Nguồn: vietjack.com[/RIGHT] [/QUOTE]
Tên
Mã xác nhận
Gửi trả lời
CÔNG NGHỆ
Công Nghệ Thông Tin
Code
Tìm hiểu về nạp chồng toán tử trong C#
Top