Lecture Windows programming chapter 4 introduce about Object-Oriented Programming. This chapter have the contents as: Review concepts in OOP, write classes in C#, interface, inheritance, polymorphism, relationships between objects.
AMBIENT/
Chủ đề:
Nội dung Text: Lecture Windows programming: Chapter 4 - Châu Thị Bảo Hà
- ObjectOriented
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
Basic concepts
class ClassName
object
- Fields
field
method + Methods
Class diagram
Other concepts (see later)
Static members: field, property and method (p.191)
Static constructor (p.191)
Static class (p.192)
Slide 3
- Contents
Review concepts in OOP
Write classes in C#
Interface
Inheritance
Polymorphism
Relationships between objects
Slide 4
- Create a class in C#
Slide 5
- Create a class in C#
Syntax:
[access modifier] class [: base_class]
{
// class body
}
[access modifier]:
private, public, protected, internal, protected internal
If you do not declare base class, the base class
default is the object
Slide 6
- Member access modifiers
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
internal (or none): default, only code in the
current project will have access to them
protected internal: are accessible only from
code-derived classes within the project
Slide 7
- 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 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
- Instantiating an object
To instantiate an object, using the new keyword
ClassName object = new ClassName (…);
ClassName object;
object = new ClassName (…);
Example:
Lop l = new Lop();
Lop c;
c = new Lop(“NCTH2K”, “Cao dang nghe 2K”);
Slide
- 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
- 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
- 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 (global for objects of a given class)?
declare variables using keyword static
Static members can be fields, properties, methods
When using static members, don’t need to instantiate
an object
You might use a static property to keep track of how
many instances of a class have been created
example: see next slide...
Slide
- static class members (cont.)
public class Employee
{
private string firstName;
private string lastName;
private static int count;
public Employee( string fName, string lName )
{
firstName = fName;
lastName = lName; Employee e1=new
count++; Employee("A","AA");
} Employee e2=new
public static int Count Employee("B","BB");
{
get { return count; } e2 = new Employee("C", "CC");
} MessageBox.Show("Số employee: "
} +
Employee.Count); Slide
- static constructors (p.191)
Using static constructors when you want to perform a
more (complex) initialization for static members
Static constructors must have no access modifiers and
cannot have any parameters
in static constructors, cannot access nonstatic member
variables
Static constructors will run before any instance of your
class is created or static members are accessed
Static constructors only be called once
Slide
- static constructors (cont.)
public class Employee
{
private string firstName;
private string lastName;
private static int count;
public Employee( string fName, string lName )
{
firstName = fName;
lastName = lName;
count++;
}
static Employee()
{
count = 0;
}
public static int Count
{ Slide
get { return count; }
- const and readonly members
To declare constant members (members whose
value will never change) using:
the keyword const
const members must be initialized when they are declared
const members are implicitly static
the keyword readonly
readonly members will be initialized in the constructor but not
change after that
Slide
- 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
- 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
- Static classes
Classes that contain only static members and cannot
be used to instantiate objects (such as Console)
A static class can’t have instance constructors, but
have static constructors
Slide