1 / 23

Chapter 4

Chapter 4. Writing Classes. Figures from Lewis, “C# Software Solutions”, Addison Wesley. Topics. Adding a Class to a Project Defining a Class Attributes Methods Using a Class Visibility. Multiple Classes in a Project. You’ve seen projects with many classes

jesse
Télécharger la présentation

Chapter 4

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. Chapter 4 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley

  2. Topics • Adding a Class to a Project • Defining a Class • Attributes • Methods • Using a Class • Visibility

  3. Multiple Classes in a Project • You’ve seen projects with many classes • XNA projects can (and often do) have multiple classes… one for each game element • Can add classes to our project • Keep them all in one file (~bad choice) • One class per file (better choice)

  4. Addingto aProject Right-click project Select Add | Add New Item…

  5. Class = Data + Methods

  6. Using the Class • Before we define the Die class, let’s use it • Remember that OOP allows us to ignore the “guts” and just presume it works • There are 3+ people involved • The writer of the class (knows what the “guts” are) • The user of the class, writing the main program (knows his code, but doesn’t know the class “guts”) • The user of the program – sees the interface, doesn’t see program code at all

  7. The Die • At this point, what do you know about Die from the code? • You can “Roll()” it • It has “FaceValue” • Can call “SetFaceValue()” • Can call “GetFaceValue()” • Now let’s look at the guts…

  8. The Die Class

  9. Die Class (cont)

  10. Die Class (cont)

  11. Die Class (cont)

  12. The “User” of a Class • Recall we don’t want to have to expose the “guts” • But we do need to tell a user of our class what it can do and what they have access to • Define the API of the class

  13. The Die Class API

  14. Unified Modeling Language (UML)

  15. Visibility • OO motivation: protection/security • We need a way of selectively “publishing” parts of a class and “hiding” other parts of the class • Public & private

  16. Visibility Example class BMW_Z4 { private int ModelYear; public string LicensePlate; private bool TopUp; public void Drive() { Console.WriteLine("Roadin’ and Rockin’"); } public void OpenTop() { TopUp = false; } } Note the visibility (attributes will usually be private) Method aretypically public

  17. Object Method & Attribute Visibility BMW_Z4 myCar; myCar = new BMW_Z4(); myCar.LicensePlate = "BMR4ME"; myCar.ModelYear = 2004; myCar.Drive(); myCar.OpenTop(); myCar.Drive(); Illegal b/c private

  18. Interacting with Objects • Keep private/public visibility as needed

  19. Visibility

More Related