Lecture 03 - Class, Object
Lecture 03 - Class, Object
1 2
3 4 5
namespace GradeBookTest
{
class Program
{
static void Main(string[] args)
{
//creating new object
GradeBook gradeBook = new GradeBook();
gradeBook.DisplayMessage();//method call
}
}
}
UML Class Diagram for GradeBook
• In UML a class is modelled as a rectangle with
three compartments
– The first one contains the name (bold)
– The middle one contains attributes (instance
variables and properties)
– The bottom one contains class’s operations
(methods)
Declaring Method with Parameters
• The Analogy:
– The car’s gas pedal sends a message to the car to perform
a task – to go faster
– But how fast should it be, we need to inform it
• Method can require one or more parameters
– They represent additional info to perform the task
– Parameters are separated by commas
– E.g., public void DisplayMessage(string p1, string p2)
• Method call can supply the values
– These are called arguments – for each of the parameters
– E.g., obj.DisplayMessage(“Mr. A”, “Ms. B”);
Method with Parameter
namespace GradeBookTest
{
class GradeBook
{
//courseName is a parameter of type string
public void DisplayMessage(string courseName) {
System.Console.WriteLine("Welcome to GradeBook for {0}", courseName);
}
}
}
namespace GradeBookTest
{
class Program
{
static void Main(string[] args)
{
//creating new object
GradeBook gradeBook = new GradeBook();
//courseName is a variable to store the input course
System.Console.WriteLine("Please input course name: ");
string courseName = System.Console.ReadLine();
//courseName is used as an argument (value for the param)
gradeBook.DisplayMessage(courseName);//method call
System.Console.ReadLine();
}
}
}
Class diagram with method
parameters
Note:
You could also model (optional) the private instance variable, and so, you can add
this to the middle section
- courseName: string
The ‘-’ sign indicates that it is the private member
The courseName is the name of the variable
string is the data type
Software Engineering with get/set
• It seems like get/set is the same as making its
corresponding instance variable public
• This is not the case because
– They allows the class to control the manner in which
the data is set/get
– So we can validate the data
• We should always manipulate data within the
class via the class properties
– Even the class itself can have direct access to the
instance variable
Auto-implemented properties
• When set simply assign the value and get simply
return the value
– No logics appear in the accessors
– C# provides auto-implemented properties
• This is handy when you first create the class
– You can modify this when needing to add logics
• So we can implement the CourseName property
with this
– public string CourseName {get; set}
– The code snippet is “prop” then a “Tab” key twice
VALUE-TYPES VS. REFERENCE-TYPES
Value Types
• C#’s simple types (like int and double) are
value types
– Variable of value type contains value of that type
Reference Types
• Reference-type variable (sometimes called
reference)
– Contains the address of a location in memory
where the data is stored
Reference-type instance variable
initialized to null
• Reference-type instance variables (such as
myGradeBook) are initialized by default to null
• string is a reference type
• an Empty string is different from a null
– Empty string refers to a string with no character
– null refers to nothing
Send messages to object
• A client of an object must use variable that refers
to the object to
– invoke (i.e., call) the object’s method
– access the object’s properties
• In our example
– Main method uses myGradeBook to send messages to
the GradeBook object
– The messages are calls to methods or references to
properties
– E.g., myGradeBook.CourseName =
Console.ReadLine();
CONSTRUCTORS
Initializing objects with constructors
• The GradeBook object is created
– Its instance variable courseName is initialized to null
by default
• How if we would like to provide a name for the
courseName variable?
– That is when we need to use constructor
• Constructor
– Is called automatically when creating a new object
– Has the same name as the class name
– Default there is a constructor with no input parameter
– We can specify the parameters for the constructor
GradeBook Class with Constructors
namespace GradeBookTest
{
class GradeBook
{
public GradeBook(string courseName)
{
CourseName = courseName;
}
public string CourseName { get; set; }
//courseName is a parameter of type string
public void DisplayMessage() {
System.Console.WriteLine("Welcome to GradeBook for {0}!", CourseName);
}
}
}
Program
namespace GradeBookTest
{
class Program
{
static void Main(string[] args)
{