1. Trang chủ
  2. » Công Nghệ Thông Tin

Chapter 6 A first look at classes

82 2 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Chapter 6 Chapter 6 A First Look at Classes 2 Contents 1 Classes and objects 2 Instance Fields and Methods 3 Constructors 4 Overloading Methods and Constructors 5 Scope of Instance Fields 6 Packages a.

Chapter A First Look at Classes Contents Classes and objects Instance Fields and Methods Constructors Overloading Methods and Constructors Scope of Instance Fields Packages and import Statements Focus on Object-Oriented Design: Finding the Classes and Their Responsibilities Classes and Objects Procedures typically operate on data items that are separate from the procedures  Procedural programming is centered on creating procedures  Object oriented programming is centered on creating objects  An object is a software entity that contains data and procedures  Data: Fields  Procedures: Methods  Classes and Objects An object's fields are called attributes  A class is the blueprint for an object It specifies the fields and methods a particular type of object has  From the class, one or more objects may be created  Classes and Objects Object oriented programming addresses the problem of code/data separation through:  Encapsulation  Data Hiding  Encapsulation refers to the combining of data and code into a single object  Data hiding refers to an object's ability to hide its data from code that is outside the object  Only the object's methods may directly access and make changes to the object's data  Classes and Objects Object reusability  An object is not a stand-alone program, but is used by programs that its services  An everyday example of an object  Your alarm clock: An object  The current second (0-59)  The current minute (0-59)  The current hour (1-12)  The time the alarm is set for (a valid hour and  Classes and Objects Fields are merely data values that define the state of the alarm clock is currently in  The user cannot directly manipulate these fields because they are private  To change a field's value, we must use one of the object's methods  Some of the alarm clock object's methods  Set time  Set alarm time  These methods can be activated by users, who are outside the alarm clock They are public methods Classes and Objects The alarm clock also has private methods, which are parts of the object's private, internal workings  External entities (such as you, the user of the alarm clock) not have directly access to the alarm clock's private methods  The object is designed to execute these methods automatically and hide the details from you  Some of the alarm clock object's private methods  Increment the current second  Increment the current minute  Classes and Objects Before an object is created, it must be designed by a programmer  The programmer determines the fields and the methods that are necessary, and then creates a class  Think of a class as a “blueprint” that objects may be created from  A class is not an object, but it is a description of an objects  When the program is running, it can use the class to create, in memory, as many objects as needed  Each object that is created from a class is called an instance of the class  Classes and Objects The Insect class describes the fields and methods that a particular type of object may have housefly object The housefly object is an instance of the Insect class It has the fields and methods described by the Insect class mosquito object The mosquito object is an instance of the Insect class It has the fields and methods described by the Insect class Insect class All of the objects that are created from the same class will have the fields ad methods described by the class 10 The BankAccount Class 68 The BankAccount Class 69 The BankAccount Class 70 The BankAccount Class 71 The BankAccount Class 72 The BankAccount Class 73 Scope of Instance Fields A variable's scope is the part of a program where the variable may be accessed by its name  Location of a variable's declaration determines the variable's scope  Instance fields can be accessed by any instance method in the same class as the field  If the instance field is declared with the public access specifier, it can also be accessed by the code outside the class  74 Shadowing We cannot have two local variables with the same name in the same scope  A parameter variable is, in essence, a local variable  We cannot give a parameter variable and a local variable in the same method the same name  However, we can have a local variable or a parameter variable with the same name as a field  The name of the local variable or parameter variable shadows the name of the field  75 Shadowing For example, assume that the Rectangle class's setLength method had been written in the following manner:  public void setLength(double len) { double length; length = len; variable } // local variable // length : local 76 Checkpoint 6.14 Is it required that overloaded methods have different return values, different parameter lists, or both?  Different parameter lists  6.15 What is a method's signature?  A method's signature is used to distinguish it from other methods of the same name  A method's signature consists of  Method name  77 Checkpoint Look at the following class:  public class Checkpoint { public void message(int x) { System.out.print(“This is the 1st version ”); System.out.print(“of the method.”); } } public void message(String x) { System.out.print(“This is the 2nd version ”); System.out.print(“of the method.”); } 78 Checkpoint What will the following code display?  Checkpoint cp = new Checkpoint(); cp.message(“1”); cp.message(1);  This is the 2nd version of the method  This is the 1st version of the method  79 Checkpoint 6.17 How many default constructors may a class have?   80 Packages and import Statements The classes in the Java API are organized into packages An import statement tells the compiler which package a class is located in  A package is simply a group of related classes  Each package has a name  We have to import an API class in order to use it  81 Explicit and Wildcard import Statements An explicit import statement identifies the package location of a single class  import java.util.Scanner; A wildcard import statement tells the compiler to import all of the classes in a package  import java.util.*; 82 ... getWidth() getArea() 29 Data Type and Parameter Notation in UML Diagrams The UML diagram also provides notation that you may use to indicate  Data types of fields, methods  Parameter variables ... Specification in UML Diagrams In a UML diagram,  A – character before a member name indicates that it is private  A + character before a member name indicates that it is public  Rectangle -... oriented programming addresses the problem of code/data separation through:  Encapsulation  Data Hiding  Encapsulation refers to the combining of data and code into a single object  Data hiding

Ngày đăng: 27/09/2022, 22:19

Xem thêm:

w