Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out

chapter22

VIEWS: 9 PAGES: 24

  • pg 1
									                 M206: Computing: An object Oriented Approach



Chapter 22 - Class Variables and
Class Methods
New Concepts
   Class instance variables

   Class variables

   Class methods

   Classes are objects and instances of metaclasses

   Date class

   Time class

   Character class


Variables
Types of Smalltalk variables

          o Instance variables

                     Used in an object to refer to other objects that make

                      up its state

                     Declared when you create or change a class using a

                      browser

                     Scope: Can only be directly accessed from the

                      methods of the class in which they are defined, and

                      from the methods of its subclasses

          o Local variables



Enas T. Ghandour                                                        1
           M206: Computing: An object Oriented Approach


                Declared by typing them in the workspace and click

                 create it button.

                Scope: only used in the LearningBook section in which

                 they have been declared.

       o Temporary variables

                Scope: Only can be used in the expression series for
                 which they are declared (for a method this means that
                 a temporary variable declared at the beginning of a
                 method can only be accessed within that method.)
                Declared by including them in vertical bars at the

                 beginning of the expression series

       o Global variables

                Scope: the entire Smalltalk environment

                Are used to provide easy access to commonly used

                 objects

                +ve: They are shared throughout the environment, so

                 can be accessed from anywhere and could be used to

                 communicate between objects in a way which is hard

                 to observe

                -ve: Software that uses them are difficult to reason

                 about and hence are generally considered dangerous

                LearningWorks doesn’t provide a simple mechanism for

                 declaring them

                Examples: Green , Red, Purple …. (they are objects of

                 Class ColorValue)

       o Class-instance variables

Enas T. Ghandour                                                      2
              M206: Computing: An object Oriented Approach


                   These are a rarely used means by which classes can

                    have state, they are the class analogy of instance

                    variables

                   They are unique to their object (their class)

                   Scope: They are visible to and can be used directly by

                    the class methods of the class and its subclasses

                   They are not shared like class variables




LB22: Session1:
Frog inspect.




  Shows Frog class-instance variables:
 The classPool is what is important for this chapter. It is a collection
  of names of class variables. The term 'pool' is used to indicate that
  access to the names is shared. In the case of class variables, any class
  method of a class or subclass and any instance method of a class or


Enas T. Ghandour                                                        3
               M206: Computing: An object Oriented Approach


    subclass can access a class variable. The nil indicates there are none
    at present.
   Most of these instance variables can be accessed by a method which
    returns them
   Message subclasses
   Message selectors
   If you were ever to add one of the rarely used class-instance variables
    to Frog, subsequent inspection would show it after sharedPools

          o Class variables

                   Must start with A CAPITAL LETTER.

                   This is the most common means by which classes can

                    have state associated with them

                   They allow class state to be shared by all instances,

                    subclasses, and their instances

                   Uses:

                           to   provide    constants   that     are    generally

                            applicable to all instances of that class

                           to provide a changing value that should be the

                            same for all instances

                           They May be used for a class to keep track of

                            all its instances

                   Scope of class variables:

                           All its class methods

                           all instance methods of its class

                           all the instance of its subclasses

                           class methods of its subclasses

                           all instance methods of its subclasses

Enas T. Ghandour                                                              4
               M206: Computing: An object Oriented Approach


LB22: Session1:
How to define class variable and class method:




now:
       Frog inspect.




 if you were to inspect HoverFrog to check the sharing, you would not
  see FrogCount in the class pool for HoverFrog

Overriding new for Frog:
Enas T. Ghandour                                                     5
            M206: Computing: An object Oriented Approach


    new
    "Answer with an initialised instance of the receiver class; use new
    from a superclass, then initialise instance. In addition, increment
    the class variable FrogCount."

    | aFrog |
    aFrog := super new.
    FrogCount := FrogCount + 1.
    ^aFrog

    Problem: when you close a LearningBook you lose the value of all
    class variables (and class-instance variables) since loading the
    LearningBook with the new version of Frog leaves any class
    variables as nil. Consequently, when the new version of new is next
    used an exception will occur as an inappropriate message is sent to
    the undefined object referenced by nil.
    Solution: write an initialize class method. The environment
    automatically sends a class any initialize message in its class
    protocol -- if such a class method exists.




Enas T. Ghandour                                                      6
          M206: Computing: An object Oriented Approach




Enas T. Ghandour                                    7
          M206: Computing: An object Oriented Approach




    Date Class:
              A subclass of Magnitude class

              instance variables:

                      day

                      year.

              Instance methods:

                      < (overrriden)

                      = (overrriden)

                   daysInMonth
                    Answer the number of days in the month
                    represented by the receiver. instance method
                    daysInMonth makes use of the class variable
                    DaysInMonth which is an instance of the Array;
                    it holds (in order) the number of days for each
                    month
                  daysInYear
                    Answer the number of days in the year
                    represented by the receiver by sending the class
                    message daysInYear: to the class Date
              Class variables:

                           DaysInMonth

                           FirstDayOfMonth,

                           MonthNames,

                           SecondsInDay

                           WeekDayNames

              Class methods:

                           initialize


Enas T. Ghandour                                                 8
          M206: Computing: An object Oriented Approach


                     Sets the values for all its class variables.

                    (Look at it in the class browser)

                     leapYear: yearInteger

                     daysInYear: yearInteger

                     Class methods for creating Date objects.

                          today
                             plays the same role for Date that

                             new plays for Frog

                          newDay: day month: monthName
                           year: year
                       Answer with an instance of Date which is

                       the day'th day of the month named

                       monthName in the year'th year. The

                       year may be specified as the actual

                       number of years since the beginning of

                       the Roman calendar or the number of

                       years since the beginning of the century

                          newDay: day monthNumber:
                           monthIndex year: year
                       Answer with an instance of Date which is

                       the day'th day of the month numbered

                       monthNumber in the year'th year. The

                       year may be specified as the actual

                       number of years since the beginning of

                       the Roman calendar or the number of

Enas T. Ghandour                                                    9
             M206: Computing: An object Oriented Approach


                              years since the beginning of the current

                              century

                                 newDay: dayCount year:
                                  referenceYear
                              Answer with a Date which is dayCount

                              days after the beginning of the year

                              referenceYear

     LB22: Session 2:
     Select Date class and then press view button:




     Examples:

Enas T. Ghandour
10
          M206: Computing: An object Oriented Approach




Enas T. Ghandour
11
          M206: Computing: An object Oriented Approach




Enas T. Ghandour
12
          M206: Computing: An object Oriented Approach




Enas T. Ghandour
13
             M206: Computing: An object Oriented Approach




     Initialize class method of Date class:
     initialize
             "Initialize class variables representing the names of the
     months and
             days and the number of seconds, days in each month, and
     first day of each
             month. "

           "Date initialize."

          MonthNames := #(January February March April May
                                  June July August September
     October November December ).

Enas T. Ghandour
14
            M206: Computing: An object Oriented Approach


           SecondsInDay := 24 * 60 * 60.
           DaysInMonth := #(31 28 31 30 31 30 31 31 30 31 30 31 ).
           FirstDayOfMonth := #(1 32 60 91 121 152 182 213 244 274
     305 335 ).
           WeekDayNames := #(Monday Tuesday Wednesday Thursday
     Friday Saturday Sunday )



     Time Class:




                A subclass of Magnitude class

                It has 24-hour clock.

                The print-string for Time uses am or pm rather than

                 the 24-hour notation

Enas T. Ghandour
15
            M206: Computing: An object Oriented Approach


                Instance variables:

                        hour
                        minute
                        second
                Class   methods: (to create instance of class Time)

                      now
                 Answer a Time representing the time right now--this
                 is a 24 hour clock
                      fromSeconds: secondCount Answer a Time that
                        is secondCount seconds since midnight
                fromSeconds: method is related to //(integer division)
                 and \\ (modulo division) instance methods of
                 SmallInteger class for example: 10.2//3=3 and
                 10.2\\3=1.2)
                Instance methods:

                        < (overriden)
                        = (overriden)
                        hours: anInteger
                        hours: hourInteger minutes: minInteger
                         seconds: secInteger

Examples:




Enas T. Ghandour
16
          M206: Computing: An object Oriented Approach




Character class:
              A subclass of Magnitude class

              A reference to a character object can be constructed

               by putting the dollar symbol ($) in front of the

               desired character, for example, $D, $i, $s and $k.

              The   American    Standard    Code   for   Information

               Interchange (ASCII) is the most widely used coding

               scheme for storing characters



Enas T. Ghandour
17
          M206: Computing: An object Oriented Approach


              The original ASCII code used sequences of seven bits

               of computer memory to represent a total of 128

               characters

              The binary ASCII codes can be translated to an

               integer in the range 0 to 127

              Instance methods:

                   < (overriden)
                   = (overriden)
                   asInteger
                  Answer the value of the receiver (equivalent to its
                  binary representation)
                   asUppercase
                   asLowercase
                   isAlphabetic
                   isDigit
                   isAlphaNumeric
                   isLowercase
                   isUppercase
                   isVowel
                   isSeparator
              class methods:

                      value: anInteger to create instances
                       Individual characters can be accessed by their

                       ASCII code by sending the value: message to

                       the Character class. Example: Character value:

                       65 returns $A

                       cr, lf and tab, create characters which are
                       used for formatting text, and correspond to the
                       codes for carriage return, line feed and tab,
                       respectively.

Enas T. Ghandour
18
              M206: Computing: An object Oriented Approach


LB22: Session 3:

$H asInteger
textual representation: 72

explanation: The reason that these numbers are relevant is that the
binary representation of characters can conveniently be interpreted as
small integers. The character H ($H in Smalltalk) is represented by the
binary 1001000 which is the way the number 72 is represented in binary

 printStringRadix:, a message in the protocol of Integer.




 How to create a new instance from Character class?
Enas T. Ghandour
19
              M206: Computing: An object Oriented Approach


new -> no
| ch |
ch := Character new.
ch

Will give




since The new class method had been overridden so that the exception
reported occurs if it is used. The exception advises the programmer to
use value:
so the following is correct
       | ch |
       ch := Character value: 67.
       ch
 do practical 19
 result is local to the page and points to the last message answer.
 CharTable class (practical 21)


Classes as instances of metaclasses:




Enas T. Ghandour
20
            M206: Computing: An object Oriented Approach




       o Classes are objects which, in the same time as their

          instances, can have methods and variables to hold state

       o Every class is the only instance of a special class called its

          metaclass




       o A class effectively obtains its class variables and class

          methods from these metaclasses (in just the same way as

          instances get them from classes). Hence, when a message is

          sent   to   a   class   its   metaclass   is   searched;   if   the


Enas T. Ghandour
21
            M206: Computing: An object Oriented Approach


          corresponding class method is not found, the superclass of

          the metaclass is searched until the method is found, or until

          an exception occurs.




       o All metaclasses are instances of the class Metaclass

       o All metaclasses are subclasses of the class Class, which is an

          indirect subclass of Object




Enas T. Ghandour
22
             M206: Computing: An object Oriented Approach




        o Object is the sole class which doesn’t have a superclass

        o Classes work in just the same way as objects, particularly

           when methods from superclasses are searched for, because

           of metaclasses and the combination of class and metaclass

           hierarchies


LB22: Session4:




Enas T. Ghandour
23
          M206: Computing: An object Oriented Approach




Enas T. Ghandour
24

								
To top