Download Adding a Class to a Project, Visibility, Instance Variables | CSE 1301 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!
CSE 1301
Lecture 4A
Writing Classes
Figures from Lewis, āC# Software Solutionsā, Addison Wesley
Briana B. Morrison
CSE 1301 4A-
Topics
⢠Adding a Class to a Project
⢠Defining a Class
ā Attributes
ā Methods
⢠Using a Class
⢠Visibility
⢠Instance Variables
⢠Properties
CSE 1301 4A-
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)
CSE 1301 4A-
Adding
to a
Project
Right-click project
Select Add | Add New
Itemā¦
CSE 1301 4A-
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.
CSE 1301 4A-
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
CSE 1301 4A-
Classes
⢠A class can contain data declarations
and method declarations
int size, weight;
char category; Data declarations
Method declarations
CSE 1301 4A-
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
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
CSE 1301 4A-
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
CSE 1301 4A-
The Die Class API
CSE 1301 4A-
Unified Modeling Language (UML)
CSE 1301 4A-
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
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
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;
Visibility Example
Method are
typically
public
Note the visibility
(attributes will usually be private)
CSE 1301 4A-
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
CSE 1301 4A-
Interacting with Objects
⢠Keep private/public visibility as needed
CSE 1301 4A-
Visibility
CSE 1301 4A-
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