Introduction to Software Development - Defining Classes | CSSE 120, Study notes of Software Engineering

Material Type: Notes; Class: Intro to Software Development; Subject: Computer Sci & Software Engr; University: Rose-Hulman Institute of Technology; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-fux
koofers-user-fux 🇺🇸

9 documents

1 / 18

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
DEFINING CLASSES
CSSE 120Rose Hulman Institute of Technology
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Introduction to Software Development - Defining Classes | CSSE 120 and more Study notes Software Engineering in PDF only on Docsity!

DEFINING CLASSES

CSSE 120—Rose Hulman Institute of Technology

Review: Using Objects

WIDTH = 400

HEIGHT = 50

REPEAT_COUNT = 20

PAUSE_LENGTH = 0.

win = GraphWin( 'Cubs Win!' , WIDTH, HEIGHT) p = Point(WIDTH/2, HEIGHT/2) t = Text(p, 'Chicago Cubs--National League Central Champs!' ) t.setStyle( 'bold' ) t.draw(win) nextColorIsRed = True t.setFill( 'blue' ) for i in range(REPEAT_COUNT): sleep(PAUSE_LENGTH) if nextColorIsRed: t.setFill( 'red' ) else : t.setFill( 'blue' ) nextColorIsRed = not nextColorIsRed win.close()

Key Concept!

 A class is like an "object factory"

 Calling the constructor tells the classes to make a new

object

 Parameters to constructor are like "factory options",

used to set instance variables

 Or think of class like a "rubber stamp"

 Calling the constructor stamps out a new object shaped

like the class

 Parameters to constructor "fill in the blanks". That is,

they are used to set instance variables.

Example

 Consider:

p = Point(200, 100)

t = Text(p, 'Go Cubs!' )

Point

x _______

y _______

fill _______

outline _______

getX() …

getY() …

'black'

'black'

Text

anchor _______

text _______

getAnchor() …

getText() …

setText(text)

setStyle(style)

p

'Go Cubs'

Point

x _______

y _______

fill _______

outline _______

getX() …

getY() …

'black'

'black'

t

This is a clone of p

class Card: """This class represents a card from a standard deck."""

Code to Define a Class

Declares a class named Card

docstring describes class, used by help() function and by Eclipse help

class Card: """This class represents a card from a standard deck.""" def init( self, card, suit): self.cardName = card self.suitName = suit

Code to Define a Class

Special name, init declares a constructor

Special self parameter is the first formal parameter of each method in a class. self always refers to the current object

Card

def init(self,card,suit):

self.cardName = card

self.suitName = suit

Create instance variables just by assigning to them

A sample constructor call:

c = Card('Ace', 'Hearts')

'Ace'

'Hearts'

cardName ______

suitName ______

c

class Card: """This class represents a card from a standard deck.""" def init( self, card, suit): self.cardName = card self.suitName = suit

def getValue( self): """Returns the value of this card in BlackJack. Aces always count as one, so hands need to adjust to count aces as 11.""" pos = cardNames.index( self.cardName) if pos < 10: return pos + 1 return 10

def str( self): return self.cardName + " of " + self.suitName

Code to Define a Class

Special str method returns a string representation of an object

Sample uses of str method:

print c

msg = "Card is " + str(c)

class Card: """This class represents a card from a standard deck.""" def init( self, card, suit): self.cardName = card self.suitName = suit

def getValue( self): """Returns the value of this card in BlackJack. Aces always count as one, so hands need to adjust to count aces as 11.""" pos = cardNames.index( self.cardName) if pos < 10: return pos + 1 return 10

def str( self): return self.cardName + " of " + self.suitName

Stepping Through Some Code

Sample use:

card = Card( '7','Clubs')

print card.getValue()

print card

Another Example:

Lists of Objects, Lists in Object

class CardCollection: """This class represents a collection of cards, either a single hand or the whole deck.""" def init( self, newDeck = False): """Creates a new collection of cards. By default it's empty, but if newDeck is True then the collection is a full standard deck.""" self.name = None self.cardList = [] self.hideFirst = True if newDeck: # Create an entire deck of cards for s in suits: for c in cardNames: self.insert(Card(c, s)) ...

Optional formal parameter , can construct a CardCollection three ways: deck = CardCollection(True) hand = CardCollection(False) hand = CardCollection()

Instance variable containing a list

Self call , constructor calls another method of the CardCollection class

Another Example (continued):

Lists of Objects, Lists in Object

class CardCollection: """…""" def init( self, newDeck = False): """…""" self.name = None self.cardList = [] self.hideFirst = True if newDeck: # Create an entire deck of cards for s in suits: for c in cardNames: self.insert(Card(c, s))

def insert( self, card): """Adds the given card to this collection.""" self.cardList.append(card)

insert method uses append() method to mutate the list instance variable

We can put objects into lists.

Encapsulation

 Insulating the rest of a program from some details

by "hiding" them inside some structure

 Top-down design encapsulates operations inside

functions

 Object-oriented design encapsulates data and

operations inside classes

 Encapsulation is a form of abstraction—ignoring

irrelevant details

Check It Out

 Use Eclipse SVN Repository Exploring perspective

 Go to your repository:

http://svn.cs.rose-hulman.edu/repos/csse120-fall07- username

 Check-out the BallSim project

 Switch back to the PyDev perspective

 The blackjackWithClasses module is there for you

to review as an example of defining classes