Chapter 2 Class Definitions - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Chapter 2 Class Definitions

Description:

Chapter 2. Class Definitions. Project naive TicketMachine. Test. Fields, Constructors, Methods. ... Define data to be stored in an object, i.e. the state of the object ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 10
Provided by: elvis2
Category:

less

Transcript and Presenter's Notes

Title: Chapter 2 Class Definitions


1
Chapter 2Class Definitions
  • Project naive TicketMachine
  • Test
  • Fields, Constructors, Methods.

2
Fields, Constructors, Methods
  • Fields
  • Define data to be stored in an object, i.e. the
    state of the object
  • Specified as private
  • Define data to be stored in an object, i.e. the
    state of the object
  • Usually come at the beginning of the class
    definition
  • NOT defined inside a method.

3
Fields, Constructors, Methods
  • Constructors
  • Same name as the class
  • No return type, not even void
  • Automatically called when an object of the class
    is created
  • Usually used to initialize values of the fields
  • Usually come after the field definitions, but
    before the methods.

4
Fields, Constructors, Methods
  • Methods
  • Define the object's behavior
  • May specify a return type, or void
  • May have parameters, separated by commas
  • Must have the parentheses, even if there are no
    parameters
  • May be public or private.

5
Passing Data Via Parameters
  • Parameters
  • Are used to send data into a called method
  • Parameters in the calling method are actual
    parameters
  • Parameters in the method being called are formal
    parameters
  • Actual parameters may be expressions of the
    correct type 3.5 p p/3.0
  • Formal parameters must be simple variables p
  • Formal parameters die when the method terminates.

6
Passing Data Via Parameters
  • When a method is called from another method
  • The actual parameters are evaluated
  • The value of each actual parameter is copied
    into the corresponding formal parameter
  • With primitive types (int, float, char, boolean)
    this is straightforward
  • With object types (String, Color, ...) a
    reference to the data is copied
  • Actual parameters and formal parameters must
    agree in number and type.

7
Assignment
  • Syntax
  • variable expression
  • Examples
  • interest balance 0.05
  • price ticketCost
  • age 18
  • age age 1
  • x y
  • Semantics
  • The expression is evaluated
  • Its value is stored into the variable
  • The value assigned is the result!

8
Assignment
  • Object types are different
  • Circle sun
  • Circle sun2
  • sun new Circle()
  • sun2 sun
  • sun.changeColor(red)
  • // sun2 is now red!

9
Expressions
  • An expression may be
  • A variable
  • A constant
  • expression expression
  • expression - expression
  • expression expression
  • expression / expression
  • ( expression )
Write a Comment
User Comments (0)
About PowerShow.com