Using Objects and Classes - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Using Objects and Classes

Description:

... any objects, we just draw them and then send messages to them. ... send a message to any object that we have drawn simply by pointing a message arrow at it. ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 27
Provided by: yuan80
Category:

less

Transcript and Presenter's Notes

Title: Using Objects and Classes


1
Using Objects and Classes
  • Object creation
  • Object reference
  • Class definition
  • constructors
  • methods
  • variables

2
Object Creation
  • When we express a computation using a diagram, we
    never have to create any objects, we just draw
    them and then send messages to them.
  • In a written program, we must provide some
    instructions to create objects before we can send
    any messages to them.

3
Object Creation
  • Every object must be created before it can be
    used.
  • An object is created by a constructor
    specified in the corresponding class.
  • A class may have more than one constructor.
  • In many languages, the name of the constructor is
    the name of the class.

4
Example
import java.io. import java.util. / to
demo the use of constructors / public class
date_demo public static void main( String
args ) Date today new Date()
Date otherday new Date(20000000 )
System.out.println(" Today is "
today.toString() ) System.out.println(" The
other day is " otherday.toString() )
5
Example
gtjava date_demo Today is Sun Sep 19 190240 MDT
1999 The other day is Wed Dec 31 223320 MST
1969
6
Constructor for Date( from jdk )
  • Date()
  • Allocates a Date object and initializes it so
    that it represents the time at which it was
    allocated measured to the nearest millisecond. .
  • Date(long)
  • Allocates a Date object and initializes it to
    represent the specified number of milliseconds
    since January 1, 1970, 000000 GMT.
  • Date(String)
  • Allocates a Date object and initializes it so
    that it represents the date and time indicated by
    the string s, which is interpreted as if by the

7
Object Reference
  • In a diagram, we can send a message to any object
    that we have drawn simply by pointing a message
    arrow at it.
  • In a written program, we need to have some
    notation for referring to objects so we can send
    messages to them.
  • If we create an object and dont have a reference
    to it, we can never use it.

8
Object References
  • An object reference is a language expression that
    refers to an object.
  • literal reference
  • Constant reference
  • Variable reference

9
Literal Object References
  • The simplest kind of object reference is a
    literal object reference.
  • A literal object reference refers to the same
    object at all times.
  • You can think of a literal object reference as a
    nameplate attached to an object.

10
Example
import java.io. / to demo the literal
reference note that "this is to ... " is an
literal string object. / public class literal
public static void main( String args )
System.out.println("this is to demo literal
reference".toUpperCase() )
gtjava literal THIS IS TO DEMO LITERAL REFERENCE
11
Variable Reference
  • A variable object reference is an object
    reference that may refer to different objects at
    different times.
  • A variable can be re-bound to a different object
    or value.
  • A variable is restricted to objects (or value) of
    a particular type called its declared type.
  • More than one variable can be bound to the same
    object (or value) at the same time.

12
Example
import java.io. / to demo the variable
reference / public class variable public
static void main( String args ) String
aString "A string of chars" String bString
aString System.out.println(aString)
System.out.println(bString.toUpperCase() )
aString "another string of chars"
System.out.println(aString)
gtjava variable A string of chars A STRING OF
CHARS another string of chars
13
Constants
  • A constant is like a variable, but once bound to
    an object (or value) it cannot be rebound.
  • It is different than a literal since
  • it is not restricted to those types that have
    literals
  • its language notation is like a variable

14
Constants - Example
Constants cannot be re-bound
constant object reference
birthDate
friend
friend
Oct 14 1979
birthDate
Fred
Barney
Dec 15 2010
constant value reference
taxRate
taxRate
now
29
later
26
15
General Object References
  • Recall that an object reference is any language
    expression that refers to an object and a value
    reference is any language expression that refers
    to a value.
  • There are many other kinds of references in
    addition to literals, variables and constants.
  • For example, since a message expression returns
    an object or value, the message expression itself
    is also a reference.

16
Class Definitions
  • Each class must have
  • at least one constructor
  • any number of methods
  • any number of various variables

17
Constructor
  • A constructor is used to create an object of the
    given class.
  • A class has at least one and upto any number of
    constructors
  • All constructors have the same name, but differ
    in the types of the argument objects.

18
Example
/ constructor demo / public class sports_car
int max_speed int price public
sports_car () public sports_car (int m
) max_speed m public
sports_car ( int m, int p ) max_speed
m price p
19
Method
  • A class has any number of methods
  • A method is a block of code that specifies a
    behavior of a message
  • A method is specified by
  • name
  • parameter list
  • return value
  • modifiers
  • body

20
Example
Name
Modifier
public void run ( ) System.out.println("keep
going, keep going, ... ") public String
bigCase( String s ) return( s.toUppperCase
() )
Parameter
Return tupe
Body
21
Variables
  • Class variables ( static variables )
  • variables declared for the class
  • Instance variables ( object variables )
  • variables declared in the class definition but
    not within any method
  • variables for the object
  • Local variables
  • declared within a method
  • Paremeters
  • declared in the parameter list of a method

22
import java.io. / to demo class definitions
/ public class sports_car int max_speed
int price public sports_car ()
public sports_car (int m ) max_speed m
public sports_car ( int m, int p )
max_speed m price p
public void run ( String slogan )
System.out.println( slogan ) public
static void main( String args ) String
slogan "Keep going, keep going, ..., "
sports_car my_baby new sports_car( 300, 120000
) my_baby.run( slogan )
23
Scope and Lifetime of Variables
  • Every variable has two characteristics.
  • The scope or visibility is the region of program
    code that can use the variable.
  • The lifetime is the time that the variable exists
    (can be used).

24
Scope of static/instance variables
  • The exporting class is the class in which it is
    declared.
  • The using class is the class in which it is used
    to reference an object or value.
  • private
  • valid only within the exporting class
  • public
  • valid in the exporting class and all the using
    classes

25
Local Variables
  • A local variable or temporary variable or method
    variable is declared in a block of code called a
    method.
  • Its lifetime is the time that the method is
    running.
  • Its scope is the method it is declared in.
  • Local constants are also allowed.

26
Message Parameters
  • A message parameter is declared at the start of a
    block of code called a method.
  • A message parameter is bound to an argument
    object or value when the method is called and
    cannot be re-bound.
  • Its lifetime is the time that the method is
    running.
  • Its scope is the method it is declared in.
Write a Comment
User Comments (0)
About PowerShow.com