Sie sind auf Seite 1von 13

Lecture 12: Shared Class Resources

Outline
 In the last lecture, we continued our discussion of Overriding…
 With Polymorphism, a simple but powerful concept
 Form 1 : Overriding Base Class Methods and Properties.
 Form 2 : The Ability of Methods to Accept ‘Related’ Classes.
 And then illustrated with a generalized Multiple-JTrain Example…
 That uses both ‘forms’ of Polymorphism.
 We also learned the basics of creating our own custom Collections:
 Similar to Collections.ArrayList, but defined by us…
 By inheriting from the .NET System Class, CollectionBase.
 And defined our own Collection called JTrainCollection.

 In this lecture, we first continue by updating Module1:


 Add two utility Methods:
 AddTrain()
 FindTrain()
 Update our Main() Method to use these with JTrainCollection
 Then, we discuss Shared Members and Methods
 Which allow us to share resources amongst all Object Instances.
 Example (JFreightTrain) : A Shared WagonWeight Member
Last time, we had just added the
CommandTrain() Method
Next, add the AddTrain() Method
The FindTrain() Method
Shared Resources
 It is often useful to define a ‘Shared’ Class resource:
 A Member, Property, or Method which is associated with a Class.
 But not to any particular instance.
 Such shared resources can be useful for:
 Book-keeping
 SPACE efficiency.
 To make a resource shared, we use the Shared keyword.
 Some JTrain Examples:
 A Shared WagonWeight Member for Class JFreightTrain
 That sets the weight of all wagons for all instances (once only).
 This let’s us change the LoadWeight at once for all Trains!
 Note: We are really setting their ideal full-load (not actual) weights…

 A Shared Counter for Class JTrain:


 That keeps track of how many instances exist, total.
 We simply add JTrain.Counter
 Then increment on creation, using the Base Constructors
Ex.: Shared WagonWeight Member
Example (cont.)
Shared Members (Assignment 1)
 (*) As an exercise, do the same for a Shared JTrain Counter…
 1. Add a Shared Member called Counter.
 Note: (*) You must also update the Base’s New() methods, as well!
 To increment Counter each time a new JTrain or JFreightTrain is created.

 2. Update the Main() Method to let the user check it!

 I will check this, when you turn in your Final Project.


Shared Methods
 Shared Methods work in the same way...
 Such Methods are shared by ALL Class instances.

 However, Shared Methods have a natural limitation :


 They may only access Members of the class that are also Shared.

Otherwise, it is unclear which instance of the Class is being acted on.
Overview / Forward
 In this lecture, we have continued our discussion of Objects…
 By finishing our our own custom Collection: JTrainCollection
 Similar to Collections.ArrayList, but defined by us…
 By inheriting from the .NET System Class, CollectionBase.
 As illustrated its use, with a generalized Multiple-JTrain Example.

 We then learned about Shared Members and Methods…


 Which allow us to share resources amongst all Object Instances.
 Ex. 1 (JFreightTrain) : A Shared WagonWeight Member
 Ex. 2 (JTrain): A Shared Counter Member

 Next Lecture, we will continue with a discussion of Objects:


 The NET Framework
 The .NET Framework Classes
 Namespaces
 The Object Browser, etc

 Finally, I will assign your FINAL PROJECT for the course.

Das könnte Ihnen auch gefallen