Tải bản đầy đủ (.pdf) (39 trang)

Lecture An introduction to computer science using java (2nd Edition): Chapter 7 - S.N. Kamin, D. Mickunas, E. Reingold

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (452.63 KB, 39 trang )

Chapter 7

Classes and Methods III:
Static Methods and Variables
Lecture Slides to Accompany

An Introduction to Computer Science
Using Java (2nd Edition)
by
S.N. Kamin, D. Mickunas, E. Reingold

 

 


Chapter Preview
In this chapter we will:
• describe user-defined classes
– instance variables
– constructors
– instance methods

• present several examples of classes
• discuss the concepts of mutability and
visibility
• describe method overloading
 

 



Object-Oriented Programming
• OOP supports the view that programs are composed
of interacting objects
• Objects are composed of
– values known as attributes or instance variables
– operations (actions) that can be performed on these values
know as instance methods

• Messages requesting an action or value are sent to
objects
• Objects respond to messages that are in their
protocols or interfaces
 

 


Objects
• Encapsulate data values within a single entity
• Their behavior is often general enough to
allow reuse in a variety of situations
• Often form a basis from which other objects
can be derived using a mechanism known as
inheritance
• Are a type container that is stored on the
system heap
• A client is program that uses an object
 


 


 

 


Client Rights
• To declare variables of the class type
• To create instances of the class using constructors
• To send messages to instances of the class by
invoking class instance methods
• To know the class public interface
– instance method names
– parameter number and types
– return types

• To know the instance methods that alter (mutate) the
instance
 

 


Class Rights
• To define the class public interface
• To hide all the implementation details from
the client
• To protect internal data from client access

• To change implementation details at any
time, provided the public interface remains
intact
• To change the public interface with client
concurrence
 

 


Revised Class Definition
public class name {
declarations of instance variables
constructor definitions
method definitions
}

• Every class needs one or more constructor
definitions
 

 


Revised Class Definition
• Instance variables
– local data contained in class

• Method definitions
– describe the class interface and how it responds to

each client message

• Constructors definitions
– describe how to initialize instance variables in a
new object

 

 


Constructors
• Look like regular instance methods
• Never have a return type
• Always have the same name as the class
name
• May have parameters
• Default constructors have no parameters
• Constructors can be overloaded (more than
one definition in the same class)
 

 


Constructors
public class Clock {
int hour,
minute;
// constructor

public Clock( ) {
hour = 12;
minute = 0;
}
// other methods follow

}

 

 


Using Constructors
// c1 set to 12:00
Clock c1 = new Clock();
// c1 set to 8:30
c1.setHour(8);
c1.setMinute(30);
// c2 set to 12:00 c1 still 8:30
Clock c2 = new Clock();
 

 


Overloading Constructors






 

Classes can have more than one
constructor
All constructors have the same name (the
class name)
Each constructor differs from the others in
either the number or types of its arguments
new is used when using a constructor to
create a new object
 


Overloading Constructors


We could add the following to Clock
public Clock(int h, int m) {
hour = h;
minute = m;
}



A client program could contain
Clock c1 = new Clock(8, 20);




Which is the same as writing
Clock c1 = new Clock( );
c1.setHour(8);
c1.setMinute(20);

 

 


Overloaded Clock Constructors
public class Clock {
int hour, minute;
public Clock () {
hour = 12;
minute = 0;
}
public Clock (int h, int m){
hour = h;
minute = m;
}

}

 

 



Using Constructors
Clock c1 = new Clock( );

// c1 set to 12:00

Clock c2 = new Clock(8, 20); // c2 set to 8:20
Clock c3 = new Clock();
c3.setHour(8);
C3/setMinute(20);

 

 

// c3 set to 8:20


Overloading Methods
• Methods can also be overloaded
• This allows different versions of the method in
the same class
• Each method variant must differ from the
others by the number or types of its
parameters
• Overloading allows methods with the same
name to have different return types
 

 



Methods Calling Other Methods
• Methods are allowed to call other methods in the
same class without specifying an explicit receiver
• This allows overloaded methods to call one
another without repeating redundant code
• Example:
public void display (DrawingBox d, int r) {
display(d, d.getDrawableWidth()/2,
d.getDrawableHeight()/2, r);
}
 

 


Dot Notation
• We can also use dot notation to view instance
variables of the same class that are different
from the receiver
• Example:
public boolean priorTo (Clock c) {
return (hour < c.hour ||
hour == c.hour &&
minute < c.minute);
}
 

 



this – Avoiding Variable Name
Collisions
• “this” can be used to distinguish between references
to instance variables and local identifiers or
arguments
public void set (int hour, int minute) {
int totalMinutes = (hour * 60 + minutes);
this.minute = totalMinutes % 60;
}

• this.minute refers to the instance variable
minute not the method argument
 

 


this – Passing the Receiver as
an Argument
• “this” can be used to send a message to the current
receiver of the message without explicitly naming the
receiver
public boolean after (Clock c) {
return c.priorTo(this);
}

• this is used as if it were a variable referring to the
receiver of the message
 


 


this – Chaining Constructors
• “this” can be used to simplify constructor code by
allowing one constructor to call another
• We can rewrite the clock constructors as:
public Clock ( ) {
this(12,0);
}
public Clock (int hour, int minute) {
set(hour, minute);
}

 

 


Visibility Qualifiers
public int x;

// client creating instance o of this
// class can access x by writing o.x

private int y; // no can access y directly, access
// provided though class methods

• To enforce complete information hiding all instance

variables should be declared using private
• The default visibility of instance variables lies between
private and public (explained later in the text)
 

 


Visibility Qualifiers and
Methods
• By default class methods are also accessible to some
classes but not others
• Visibility qualifiers should also be used in method
declarations
• Examples:
public void f( )

{ //
//
//
private void g( ) { //
//
//

 

 

Any client using object o
can send it a message

by writing o.f( )
No client can send g to
the object except another
method from this class


Visibility and UML Diagrams
• In UML diagrams
– private variables and methods are
indicated using a leading minus sign as a
prefix
– public variables and methods are indicates
using a leading plus sign as a prefix
– a blank prefix means that the variables and
methods are not annotated and will have
their default visibility
 

 


×