1 / 15

Classes and Objects

Classes and Objects. Aims and Objectives. Define: Class schemas to specify object classes (i.e.“types” of objects). » models are possible object instances of a class. Bag-schemas to specify collections of object instances of a given class.

Download Presentation

Classes and Objects

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 and Objects Aims and Objectives • Define: • Class schemas to specify object classes (i.e.“types” of objects). » models are possible object instances of a class. • Bag-schemas to specify collections of object instances of a given class. • » models are collections of object instances  i.e. bags of models of a class schema. • Illustrate these two types of schema using a Library example. Unit 3: Classes and Objects

  2. Classes • Class, in object-oriented approach, specifies: • object’s variables – “state” (i.e. “type”) of objects; • methods – “operations” on objects of a given type. • Overall system is more complicated: • a set of objects • each object has a type  is a model of its class schema • system operations can operate on individual objects • … or can change collection of objects (e.g. create, destroy) • Class schemas for types of objects • Operation schemas for operations on objects. • “Bag-schemas” for bags or multisets of objects Unit 3: Classes and Objects

  3. Bags … half way between sets and lists. • In a list: order and multiplicity all matter. • [3,5], [5,3], [3,3,5] are all different lists • In a set: order and multiplicity make no difference • {3,5}, {5,3}, {3,3,5}are all the same set • In a bag: multiplicity matters, order doesn’t. • {|3,5|}, {|5,3|} are the same bag, • {|3,3,5|} is different • The system has bag of models of class schemas, as collection of different objects of the same class. Unit 3: Classes and Objects

  4. How to define a bag e.g. A bag of integers: Define a set X (“indexing set”) and a family of integers indexed by X. e.g. • a={|3,5|}: X any 2-element set, e.g. {Harry, Tom} aHarry = 3, aTom = 5, where “a” denotes the bag • b={|3,3,5|}: X any 3-element set, e.g.{Tom, John, Harry} bTom= bJohn= 3, bHarry= 5, where “b” denotes the bag • Why the funny X’s? Why not just {1,2} or {1,2,3}? • Just as example to emphasize that X does not have to have any intrinsic ordering – if it did we’d just have lists. Unit 3: Classes and Objects

  5. About the indexing set • Indexing set X provides “individual identities” for elements of bag. • cf. addresses of objects: X = {addresses used} • Two different objects may have same type, but they’ll still be distinguished by their different addresses. • Finite bag – when X is finite. Unit 3: Classes and Objects

  6. FinBag(Y) [X] X0: FX f: X  Y x:X. xX0 Bag(Y) [X] f: X Y Schemas whose models are bags Assume a set of elements Y and an indexing set X, then: Model = bag of elements Y. Model = finite bag of elements Y. • What’s X0 for? • It’s just a trick to make sure X is finite: • X0 is declared as a finite subset of X; • the axiom says X is a subset of X0. • Hence X0 = X and both are finite. Unit 3: Classes and Objects

  7. Bags of models of schemas • What we saw before was bags of elements of sets. • Given one schema S, we can define a new “bag-schema” whose models are bags of models of S. How to define them • Simple, when S does not include any base sort. • Add a new base sort X denoting the indexing set. • Constants become functions from X. • Functions and predicates gain an extra argument of sort X. Unit 3: Classes and Objects

  8. Example: a library Making many simplifying assumptions: • Books are described by ISBNs (International Standard Book Numbers) • Library may have more than one copy of the same book. • Each copy has a location comprising a library (e.g. central or departmental) and a shelf-mark. • Some copies may be identical twins of each other (same book, same location) but they are still distinct. • Borrowers have id codes and upper limit on no. books they can borrow. Unit 3: Classes and Objects

  9. Copy isbn: seqCHAR location: LOCATION status: COPYSTATUS Copies as objects in a class schema • LOCATION = LIBRARYSHELFMARK • (e.g. Library = {Maths, Physics, …} • SHELFMARK = … whatever. • We don’t need to know the details.) • COPYSTATUS = {in, out} Unit 3: Classes and Objects

  10. NewCopy Copy isbn?: seqCHAR location?: LOCATION isbn = isbn? location = location? status = in MoveCopy DCopy to?: LOCATION isbn' = isbn status' = status location' = to? Initialization, operations on copies Unit 3: Classes and Objects

  11. Collection [stock] stock0: F(stock) isbn: stock  seqCHAR location: stock  LOCATION status: stock  COPYSTATUS "x:stock. xstock0(forces stock to be finite) Bag of copies • Elements of stock identify individual copies held. • For each xstock, values isbn(x), location(x) and status(x) are the values needed to define a model of Copy. • Collection is FinBag(Copy): a model of Collection is a finite bag of models of Copy. Unit 3: Classes and Objects

  12. MoveOneCopy DCollection moving?: stock to?: LOCATION stock' = stock isbn' = isbn  status' = status "x:stock. (location'(x)=location(x)  x = moving?) location'(moving?) = to? Promotion Operations on individual copies are “promoted” to give operations on the entire collection. e.g.:MoveCopy - moving one copy but in a collection Unit 3: Classes and Objects

  13. OneNewCopy DCollection isbn?: seqCHAR location?: LOCATION new!: stock' new!  stock stock' = stock{new!} "x:stock. (isbn'(x) = isbn(x)  location'(x) = location(x)  status'(x) = status(x)) isbn'(new!) = isbn? location'(new!) = location? status'(new!) = in Creation NewCopy: creating one new copy in a collection Unit 3: Classes and Objects

  14. Other classes Borrower id: seqCHAR max: N (max = max. no. books to be borrowed) • Suppose it also has • a class schema Borrower: • a corresponding bag-schema • Borrowers = FinBag(Borrower), with indexing set “borrower”. System is more than just a collection of objects from a single class schema. • Library • Collection • Borrowers • borrowed: F(borrowerstock) • "x: stock. (status(x) = out $y:borrower. (y,x)  borrowed) • "y:borrower. |{x:stock | (y,x)  borrowed}|  max(y) Unit 3: Classes and Objects

  15. Borrow DLibrary x?:stock y?:borrower status(x?) = in |{x:stock. (y?,x)  borrowed}| < max(y?) stock' = stock  borrower' = borrower isbn' = isbn  location' = location id' = id  max' = max "x:stock. (status'(x) = status(x)  x = x?) status'(x?) = out borrowed' = borrowed{(y?,x?)} Example Operation Unit 3: Classes and Objects

More Related