tailieunhanh - Beginning Programming with Java for Dummies 2nd phần 9

Tại thời điểm này trong chương, cuối cùng tôi có thể nói, "Tôi đã nói với bạn như vậy." Đây là một trích dẫn từ Chương 17: Một lớp là một kế hoạch thiết kế. Lớp mô tả cách thức mà bạn có ý định kết hợp và sử dụng các mẩu dữ liệu. | Chapter 18 Using Methods and Variables from a Java Class 313 Combining and using data At this point in the chapter I can finally say I told you so. Here s a quotation from Chapter 17 A class is a design plan. The class describes the way in which you intend to combine and use pieces of data. A class can define the way you use data. How do you use a password and a user s input You check to see if they re the same. That s why Java s String class defines an equals method. An object can be more than just a bunch of data. With object-oriented programming each object possesses copies of methods for using that object. Static Methods You have a fistful of checks. Each check has a number an amount and a payee. You print checks like these with your very own laser printer. To print the checks you use a Java class. Each object made from the Check class has three variables number amount and payee . And each object has one method a print method . You can see all this in Figure 18-7. You d like to print the checks in numerical order. So you need a method to sort the checks. If the checks in Figure 18-7 were sorted the check with number 1699 would come first and the check with number 1705 would come last. Check number amount payee print 1705 The Butcher method to cut the check The Check 1699 TheBaker method to cut the check -Ị class and some check 1702 12-35 The Candlestick method to cut the check Maker objects. 314 Part IV Using Program Units__ The big question is should each check have its own sort method Does the check with number 1699 need to sort itself And the answer is no. Some methods just shouldn t belong to the objects in a class. So where do such methods belong How can you have a sort method without creating a separate sort for each check Here s the answer. You make the sort method be static. Anything that s static belongs to a whole class not to any particular instance of the class. If the sort .

TỪ KHÓA LIÊN QUAN