1 / 27

Classes The Static Structure

Classes The Static Structure. Person Class – 1. indexing -- For class level documentation description: "A simple person" author: "Gunnar Gotshalks" date:"2000 Jan 9" class PERSON creation -- list construction features make feature name : STRING -- attributes are usually first

ltalty
Download Presentation

Classes The Static Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes The Static Structure

  2. Person Class – 1 • indexing -- For class level documentation • description: "A simple person" author: "Gunnar Gotshalks" date:"2000 Jan 9" • class PERSON • creation -- list construction features • make • feature • name : STRING -- attributes are usually first • sex : GENDER • age : INTEGER PERSON is a client of the supplier STRING

  3. Person Class – 2 • make( n : STRING ; s : GENDER ; a : INTEGER ) is • -- Create a complete non default person • do • -- Empty body for this example creation procedure • end • set_name ( s : STRING ) is • -- Need to explicitly set attribute values • do • name := s • end

  4. Person Class – 3 PERSON is a client of the supplier INTEGER older ( a : INTEGER ) : BOOLEAN is • -- Are you older than me? • do • if a > age then print ( "You are older than me. %N") • Result := trueelse print ( "I am older than you. %N") • Result := falseend • end • end -- PERSON

  5. Client–Supplier BON diagram • BON stands for B-usiness O-bject N-otation STRING PERSON INTEGER uses GENDER BOOLEAN

  6. Inheritance • class PERSON inherit • HOMOSAPIEN • feature • .... • end -- class PERSON Eiffel text BON diagram HOMOSAPIEN Inherits from PERSON

  7. Feature Call • object . function ( arguments ) • Evaluate the arguments to the function • Then apply the function to the object

  8. Uniform Access Principle • All services offered by a class should be available through a uniform notation, which does not betray whether they are implemented through storage or through computation. • In other words, in the followinga: ACCOUNT print(a.balance)you should not have to worry whether balance is a function or an attribute (it is only an implementation detail) • Allows the supplier (the ACCOUNT class) to modify its implementation without affecting its customers • Java violates the Uniform Access Principle

  9. Current Instance • Instance calling the feature is named Current locally p1 . distance_to ( p2 ) -- example call • distance_to( p : POINT ) : REAL is • -- Distance between current point and p • do • if ( p /= Current ) then • Result := sqrt( ( x - p.x )^2 + ( y - p.y )^2 ) end • End • could write as • Result := sqrt( ( Current.x - p.x )^2 • + ( Current.y - p.y )^2 ) bound to p2 bound to p1

  10. Current Instance – 2 • Current can be used in the following contexts • Passing instance as a parameter • a.f ( Current ) • Comparing with another reference • x = Current • Use as an anchor in anchored declarations • object : like Current • Will see this again in inheritance

  11. Selective Exports • Need to restrict access by clients • In Java have public, protected and private • In Eiffel can be more selective • class S feature • -- all features exported -- public • feature { A , B } • -- export only to A and B -- protected • feature { NONE } • -- export to no one -- private, secret-- NOT EVEN TO S – include self if needed ! • end -- class S

  12. System Execution • Create a certain object • called the root object for the execution • Apply a certain procedure to that object • called the creation procedure This is the BIG BANG!

  13. Deferred / Effective Class A class which is fully implemented is said to be effective. A class which is implemented partially, or not at all, is said to be deferred. Any class is either deferred or effective. Class Definition In Java a deferred class is called an abstract classIn Java an interface is a class with all methods deferred and no objects

  14. Role of Deferred Classes • Design and analysis • Pure description – no implementation details required • Concentrate on architectural properties • Provide for variations in implementation while preserving a particular type • Provide for evolutionary development and its history

  15. Objects Run time direct instances of classes

  16. Fields • The attributes within a class are a template for a collection of fields in an object • class PERSON • feature • name : STRING • sex : GENDER • age : INTEGER • end -- class PERSON a_Person name sex age

  17. Objects • Variables with a class for a type • Must have a name declared and the name must be attached to the object • Using an object requires two steps: declaration and creation • p : PERSON -- declare the name p • create p -- create and attach object to p • w : PERSON -- declare the name w • create w.make ( "Me" ) -- creation via a function

  18. Creation Operator • create is akin to new in C++ and Java • The 4 steps • Create an instance of the type Allocate enough memory for the instance • Initialize each field to default values • Attach the reference to the variable • Execute the procedure (if any) to complete initialization

  19. p void p PERSON Reference types • p is used torefer to an instance of type PERSON • p : PERSON • Create and attach object to p – p is attached • create p • Think of p as a pointer For type safety, unlike C/C++, the pointer cannot bede-referenced

  20. Reference Types – 2 • In general, declaring a type means the variable is a reference to an instance of the type • Primitive types – INTEGER, BOOLEAN, CHARACTER – are not references, they are statically allocated (expanded) • Expansion means the reference is replaced with the fields of the referenced object • Any reference can be expanded • Provided there are no cycles

  21. Copying and Equality • Copying • a := b -- copies reference • a := clone(b) -- shallow copy, one level only • a := deep_clone(b) -- deep copy, all levels • Equality • a = b -- compares references • equal (a,b) – compares corresponding fields (one level) • deep_equal (a,b) – compares corresponding fields (all levels)

  22. a b Almaviva c Almaviva Figaro Susanna Copying and Equality – 2 class PERSON feature name: STRING landlord: PERSON loved_one: PERSON end b := a c := clone(a)

  23. d Almaviva Figaro Susanna Copying and Equality – 3 d := deep_clone(a) All new memory locations

  24. Composite Objects & Expanded Types • Consider the following version of class Person • class PERSON • feature • name : NAME • end -- class PERSON • We say that Person has a NAME • Normally NAME is a reference • Makes it possible for two or more instances of PERSON to share the same NAME p2.name p1.name a_name

  25. Composite Objects & Expanded Types – 2 • Sharing references leads to aliasing which can lead to surprises • p1.name := "John" • print ( p2.name ) --> John • p2.name := "Alice" • print ( p1.name ) --> Alice -- Probably a surprise • Could be careful about names always pointing to different memory locations • condition: p1.name  p2.name • Difficult to enforce

  26. Composite Objects & Expanded Types – 3 • Use expanded types to enforce aggregation • Object has a collection of subparts that are uniquely theirs • feature • name : expanded NAME • end -- class PERSON • Now guarantee • p1.name  p2.name • Still permit: p1.name . equal ( p2.name )

  27. Aliasing • Occurs when two variables point to the same memory location • Can lead to surprises but • Reference assignments needed to benefit from OO • Often need two pointers to point to the same object • Encapsulation makes it possible to avoid dangers of reference manipulations p2.name p1.name a_name

More Related