Đang chuẩn bị liên kết để tải về tài liệu:
Lecture An introduction to Object-Oriented Programming with Java - Chapter 4: Defining your own classes

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Chapter 4 - Defining your own classes. Learning how to define your own classes is the first step toward mastering the skills necessary in building large programs. In this chapter we will learn how to define instantiable classes and different types of methods included in the instantiable classes. A class is instantiable if we can create instances of the class. The DecimalFormat, GregorianCalendar, and String classes are all instantiable classes while the Math class is not. | Chapter Four Defining Your Own Classes Chapter 4 Objectives After you have read and studied this chapter, you should be able to Define an instantiable class with multiple methods and constructors. Differentiate the local and instance variables. Define and use value-returning methods. Distinguish private and public methods. Distinguish private and public data members. ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 4 Objectives, cont. After you have read and studied this chapter, you should be able to Describe how the arguments are passed to the parameters in method definitions. Describe how the result is returned from a method. Define a reusable class for handling input routines. Define an instantiable main class. ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 4.1 Defining Instantiable Classes Learning how to define instantiable classes is the first step toward mastering the skills necessary in building large programs. A class is instantiable if we can create instances of the class. The DecimalFormat, GregorianCalendar, and String classes are all instantiable classes, while the Math class is not. ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 4.1 Defining Instantiable Classes Currency converter example: We need two methods for conversion: fromDollar and toDollar. CurrencyConverter yenConverter; double amountInYen, amountInDollar; yenConverter = new CurrencyConverter( ); . amountInYen = yenConverter.fromDollar(200); //from dollar to yen amountInDollar = yenConverter.toDollar(15000); //from yen to dollar ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 4.1 Defining Instantiable Classes Since the exchange rate fluctuates, we need a method to set the exchange rate. CurrencyConverter yenConverter; yenConverter = new CurrencyConverter( ); yenConverter.setExchangeRate(130.77); ©TheMcGraw-Hill Companies, Inc. Permission required for | Chapter Four Defining Your Own Classes Chapter 4 Objectives After you have read and studied this chapter, you should be able to Define an instantiable class with multiple methods and constructors. Differentiate the local and instance variables. Define and use value-returning methods. Distinguish private and public methods. Distinguish private and public data members. ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 4 Objectives, cont. After you have read and studied this chapter, you should be able to Describe how the arguments are passed to the parameters in method definitions. Describe how the result is returned from a method. Define a reusable class for handling input routines. Define an instantiable main class. ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 4.1 Defining Instantiable Classes Learning how to define instantiable classes is the first step toward mastering the skills necessary in building large .