Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 1301 Lecture 5 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.

Similar presentations


Presentation on theme: "CSE 1301 Lecture 5 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick."— Presentation transcript:

1 CSE 1301 Lecture 5 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

2 CSE 1301 Topics Adding a Class to a Project Defining a Class – Attributes – Methods Using a Class Visibility Instance Variables Properties

3 CSE 1301 Multiple Classes in a Project You’ve seen projects with many classes – Unity 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 CSE 1301 Why User-Defined Classes? Primitive data types (int, double, char,.. ) are great … … but in the real world, we deal with more complex objects: products, Web sites, flight records, employees, students,.. Object-oriented programming enables us to manipulate real-world objects.

5 CSE 1301 User-Defined Classes Combine data and the methods that operate on the data Advantages: – Class is responsible for the validity of the data. – Implementation details can be hidden. – Class can be reused. Client of a class – A program that instantiates objects and calls methods of the class

6 CSE 1301

7 Classes A class can contain data declarations and method declarations int size, weight; char category; Data declarations Method declarations

8 CSE 1301 Important Terminology Fields – instance variables: data for each object – class data: static data that all objects share Members – fields and methods Access Modifier – determines access rights for the class and its members – defines where the class and its members can be used

9 CSE 1301 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

10 CSE 1301 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

11 CSE 1301

12

13 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…

14 CSE 1301 The Die Class API

15 CSE 1301 Die Class

16 CSE 1301 Die Class (cont)

17 CSE 1301 Die Class (cont)

18 CSE 1301 Unified Modeling Language (UML)

19 CSE 1301 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

20 CSE 1301 public vs. private Instance variables are usually declared to be private Methods that will be called by the client of the class are usually declared to be public Methods that will be called only by other methods of the class are usually declared to be private APIs of methods are published (made known) so that clients will know how to instantiate objects and call the methods of the class

21 CSE 1301 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; } Method are typically public Note the visibility (attributes will usually be private)

22 CSE 1301 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

23 CSE 1301 Interacting with Objects Keep private/public visibility as needed

24 CSE 1301 Visibility

25 CSE 1301 Defining Instance Variables Syntax: accessModifier dataType identifierList; dataType can be primitive date type or a class type identifierList can contain: – one or more variable names of the same data type – multiple variable names separated by commas – initial values Optionally, instance variables can be declared as const

26 CSE 1301 Examples of Instance Variable Definitions private string name = ""; private const int PERFECT_SCORE = 100, PASSING_SCORE = 60; private int startX, startY, width, height;

27 CSE 1301 The Auto Class class Auto { private string model; private int milesDriven; private double gallonsOfGas; }

28 CSE 1301 Properties Combines field/attribute with method Standard: – Make attributes private – Lower-case first letter of attribute – Make properties public – Upper-case first letter of properties – Define “get” and “set” for each property (selectively)

29 CSE 1301 class BMW_Z4 { private int modelYear; private string licensePlate; private bool topUp; public int ModelYear { get { return modelYear; } set { if (value >= 2003) modelYear = value; } } public string LicensePlate { get { return licensePlate; } set { if (value.Length() == 6) licensePlate = value; } }... } Properties Example

30 CSE 1301 Auto Properties Now how would you write the properties for the Auto class? class Auto { private string model; private int milesDriven; private double gallonsOfGas; }


Download ppt "CSE 1301 Lecture 5 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick."

Similar presentations


Ads by Google