Object-Oriented Programming
Chapter 4
Ebook: Beginning Visual C# 2010, part 1, chapter 8,9,10,13
Reference: DEITEL - CSharp How to Program
Contents
Review concepts in OOP
Write classes in C#
Interface
Inheritance
Polymorphism
Relationships between objects
Slide 2
Review concepts in OOP
Review concepts in OOP
class
object
field
property
method
event
Other problems
Static members: field, property and method (p.191)
Static constructor (p.191)
Static class (p.192)
Slide 3
ClassName
- Fields
+ Methods
Class diagram
Contents
Review concepts in OOP
Write classes in C#
Interface
Inheritance
Polymorphism
Relationships between objects
Slide 4
Create a class in C#
Slide 7
Member access modifiers
Without a
access modifier
: can be accessed by any class in
the same namespace
private: can only be accessed from inside the class
public: can be accessed from anywhere
protected: can be accessed from inside the child class
or any class in the same namespace
Slide 8
Writing properties
Properties
(accessors):
Contain two blocks:
get
and
set
the value of the fields
One of these blocks can be omitted to create read-only or
write-only properties
You can use
Encapsulate Field
function to properties
Slide 9
Writing constructors
A constructor is a special method in a class that is invoked
when the object gets instantiated
A constructor is used to initialize the properties of the
object
Note to write a constructor:
The name of the constructor and the name of the class are
the same
A constructor does not return value, not even void
A class can have multiple constructors (overloaded
constructors)
public ClassName (parameterList)
{
}
Slide 10
Instantiating an object
To instantiate an object, using the new keyword
Example:
Lop l = new Lop();
Lop c;
c = new Lop(“NCTH2K”, “Cao dang nghe 2K”);
ClassName object = new ClassName (…);
ClassName object;
object = new ClassName (…);
Slide 11
Using properties, methods of a class
Call methods or properties of a class
Using
dot
operator
Accessors:
get: variable = object.propertyName
set: object.propertyName = variable
Example:
Lop c = new Lop();
c.MaLop = "NCTH2K"; // call set{}
c.TenLop = "Cao dang nghe 2K"; // call set{}
Slide 12
OOP tools in VS
The Class View Window (p.222)
Menu View\Class View
The Object Browser (p.224)
Menu View\Object Browser
Class Diagrams (p.227)
The class diagram editor in VS enables you to generate UML-
like diagrams of your code and use them to modify projects
Slide 13
static class members (p.191)
Every object of a class has its own copy of all instance
variables
How can all objects of a class share the same copy of a
variable?
Declare variables using keyword static
Static members can be fields, properties, methods
When using static members, don’t need to instantiate an
object
Slide 14
static class members (cont.)
public class Employee
{
private string firstName;
private string lastName;
private static int count; // Employee objects in memory
public Employee( string fName, string lName )
{
firstName = fName;
lastName = lName;
count++;
}
public static int Count
{
get { return count; }
}
}
Slide 15
Employee e1=new Employee("A","AA");
Employee e2=new Employee("B","BB");
e2 = new Employee("C", "CC");
MessageBox.Show("S employee: " + ố
Employee.Count);
static constructors (p.191)
Static constructors
must have no access modifiers and
cannot have any parameters
In
static constructors
, cannot access nonstatic member
variables
Static constructors
only be called once
Static constructors
will run before any instance of your
class is created or static members are accessed
Example:
public class ABC{
private static string name;
static ABC() {
name = “static member”;
}
}
Slide 16
const and readonly members
To declare constant members (members whose value will
never change) using:
the keyword const
const members are implicitly static
const members must be initialized when they are declared
the keyword readonly
readonly members will be initialized in the constructor but not
change after that
Slide 17
const and readonly members (cont.)
public class Constants
{
// PI là m t h ng s constộ ằ ố
public const double PI = 3.14159;
// radius là m t h ng s ch a đ c kh i t oộ ằ ố ư ượ ở ạ
public readonly int radius;
public Constants( int radiusValue )
{
radius = radiusValue;
}
}
Slide 18
const and readonly members (cont.)
public class UsingConstAndReadOnly
{
static void Main( string[] args )
{
Random random = new Random();
Constants constantValues = new Constants( random.Next( 1, 20 ) );
MessageBox.Show( "Radius = " + constantValues.radius +
"\nCircumference = " + 2 * Constants.PI * constantValues.radius,
"Circumference" );
}
}
Slide 19
Contents
Review concepts in OOP
Write classes in C#
Interface
Inheritance
Polymorphism
Relationships between objects
Slide 22
Inheritance (p.194)
Inheritance enables to create a new class that extends an
existing class
The existing class is called the
parent class,
or
super class
, or
base class
The new class is called the
child class
or
subclass, derived class
The child class inherits the properties and methods of the
parent class
A programmer can tailor a child class as needed by adding
new variables
or
methods
, or by modifying the inherited ones
C# provides a common base class for all objects called
Object class
Slide 23
Inheritance (cont.)
Declare inheritance class
Classes in C# may derive only from a single base class
directly
The protected modifier allows a child class to reference
a variable or method directly in the parent class
A protected variable is visible to any class in the same
namespace
Slide 24
ParentClass
ChildClass
public class ChildClass : ParentClass
{
//
}
The base reference
Constructors are not inherited
To invoke the parent's
constructor
The base reference can also be used to reference other
variables
and
methods
defined in the parent’s class
Slide 25
: base (parameters);
base.VariableName
base.MethodName( parameters );
The base reference (cont.)
public class NegativeNumberException : ApplicationException
{
public NegativeNumberException()
: base( "Phai nhap vao so khong am" )
{
}
public NegativeNumberException( string message ) : base( message )
{
}
public NegativeNumberException( string message, Exception inner )
: base( message, inner )
{
}
}
Slide 26
Overriding methods
A child class can redefine a base-class method; this method
is called
overriding
method
To be overridden, a base-class method must be declared
virtual
To write an overriding method, using override keyword in
the method header
To view the method header for a method, using Object
Browser
Example: view Object’s methods
Slide 27
Object class (p.215)
All classes inherit from Object
All classes have access to the protected and public members
of this class (table p.215)
You can override some methods of Object class (if a method
is being overridden)
Example: public override string ToString()
Some Object’s methods:
ToString(): example
Equals(object): example
GetHashCode(): example
Slide 28
Example: class Point
public class Point
{
private int x, y;
public Point()
{
}
public Point( int xValue, int yValue )
{
X = xValue; // use property X
Y = yValue; // use property Y
}
public int X
{
get { return x; }
set { x = value; }
}
Slide 29
public int Y
{
get { return y; }
set { y = value; }
}
public override string ToString()
{
return "[" + x + ", " + y + "]";
}
}