Programming C# 2nd Edition phần 3

cung cấp hai hương vị của các nhà khai thác tăng và lượng giảm: tiền tố và hậu tố. Vì vậy bạn có thể viết: tăng đầu tiên, và sau đó giao (firstValue = 11, secondValue = 11). Điều quan trọng là để hiểu các tác động khác nhau của tiền tố và hậu tố, như | Programming C 2nd Edition public class Tester static void Main Fraction fl new Fraction 3 4 f1 0 fa new f1 The nested class is shown in bold. The FractionArtist class provides only a single member the Draw method. What is particularly interesting is that Draw has access to the private data members and to which it would not have had access if it were not a nested class. Notice in Main that to declare an instance of this nested class you must specify the type name of the outer class fa new FractionArtist is scoped to within the Fraction class. 113 Programming C 2nd Edition Chapter 6. Operator Overloading It is a design goal of C that user-defined classes have all the functionality of built-in types. For example suppose you have defined a type to represent fractions. Ensuring that this class has all the functionality of the built-in types means that you must be able to perform arithmetic on instances of your fractions . add two fractions multiply etc. and convert fractions to and from built-in types such as integer int . You could of course implement methods for each of these operations and invoke them by writing statements such as Fraction theSum secondFraction Although this will work it is ugly and not how the built-in types are used. It would be much better to write Fraction theSum firstFraction secondFraction Statements like this are intuitive and consistent with how built-in types such as int are added. In this chapter you will learn techniques for adding standard operators to your user-defined types. You will also learn how to add conversion operators so that your user-defined types can be implicitly and explicitly converted to other types. Using the operator Keyword In C operators are static methods whose return values represent the result of an operation and whose parameters are

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
45    75    2    22-05-2024
6    106    2    22-05-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.