Tải bản đầy đủ (.ppt) (118 trang)

Lập trình Windows - Lập Trình C #- Lập Trình C Shap - Chương 3 potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1013.03 KB, 118 trang )

1
Chương 3
Chương 3
Xây dựng lớp và giao diện
Xây dựng lớp và giao diện
Nội dung
Nội dung

Lập trình hướng đối tượng là gì?

Khai báo lớp

Khai báo interface

Constructor & destructor

Thuộc tính (field)

Method

Inheritance

Protected fields

Overriding method
2
Nội dung
Nội dung

Polymorphism (Đa hình)


Down cast – up cast

Abstract class

Sealed class, nested class

Interface
3
Overview
Overview
“Everything is an object! At least, that is the view from
inside the CLR and the C# programming language. This is
no surprise, because C# is, after all, an object-oriented
language. The objects that you create through class
definitions in C# have all the same capabilities as the
other predefined objects in the system…”
Apress-Accelerated C# 2008
4
Object-oriented Programming (OOP)
Object-oriented Programming (OOP)

Kỹ thuật lập trình truyền thống ( procedural
programming):

all functionality is contained in a few modules of
code (often just one)
 Khó thay thế cải tiến

Kỹ thuật OOP :


Sử dụng nhiều modules of code, mỗi module cung
cấp 1 chức năng riêng và có thể được cô lập
(isolated) hay thậm chí độc lập hoàn toàn với
nhau.
 Dễ dàng sử dụng lại các module
5
What is an Object?
What is an Object?

An object is a building block of an OOP
application

Objects in C# are created from types, just like
the variables.

The type of an object is known by a special
name in OOP, its class.

You can use class definitions to instantiate
objects, which means creating a real, named
instance of a class.
6
Cấu trúc của object
Cấu trúc của object

Một object gồm có:

Field

Method

7
Life Cycle of an Object
Life Cycle of an Object

Gồm 2 giai đoạn (two important stages):
➤ Construction: đối tượng cần được khởi tạo, và được
thực thi bởi hàm constructor
➤ Destruction: khi đối tượng bị hủy bỏ, 1 số nhiệm vụ
clean-up cần được thực thi để giải phóng bộ nhớ  hàm
destructor.
8
Tạo lớp trong C#
Tạo lớp trong C#

Khai báo lớp

Nếu ko khai báo lớp cơ sở thì C# mặc định xem
lớp cơ sở là object

Lớp luôn là kiểu dữ liệu tham chiếu (reference
type) trong C#
9
[access modifier] class <class name> [: base class]
{
// class body
}
Khóa truy xuất cho class
Khóa truy xuất cho class
(Access modifier)
(Access modifier)


Một class chỉ có 2 khóa truy xuất

Internal: chỉ cho phép sử dụng bên trong project
hiện hành (default)
internal class MyClass
{
// Class members.
}

Public: cho phép các project bên ngoài truy
xuất
public class MyClass
{
// Class members.
}
10
Interface là gì?
Interface là gì?

Interface là 1 tập hợp các method và property điển hình
chung nào đó được nhóm lại cùng nhau để đóng gói
(encapsulate) 1 chức năng nào đó.

Interface không chứa mã để thực thi, nó chỉ định nghĩa
các thành phần của chính nó.

Sau khi định nghĩa interface, có thể thực thi nó trong 1
class  class sẽ hỗ trợ (support) mọi property và thành
phần được xác định trong interface.


Interfaces cannot exist on their own  không thể khởi
tạo 1 interface (‘‘instantiate an interface’’)
11
Biểu diễn interface bằng UML
Biểu diễn interface bằng UML
12
Khai báo interface
Khai báo interface

Khai báo interface cũng tương tự như khai báo lớp:
interface IMyInterface
{
// Interface members.
}

Các khóa truy xuất: internal (default), public, không
được phép dùng astract hay sealed

Tính kế thừa của interface cũng tương tự như tính
kế thừa của class
public interface IMyInterface : IMyBaseInterface, IMyBaseInterface2
{
// Interface members.
}
13
So sánh Interface và class
So sánh Interface và class

Tất cả class đều kế thừa từ lớp cơ sở System.Object theo

dạng cây phân cấp (inheritance hierarchy)

Interfaces không phải là class, vì vậy không kế thừa từ
System.Object.

Không thể khởi tạo (instantiate) một interface như khởi
tạo 1 class.
14
Các thành phần của class
Các thành phần của class

Lớp có thể chứa các phần sau

Constructor và destructor

Field và constant

Method

Property

Indexer

Event

Chứa các kiểu khác (nested): class, struct, enumeration,
interface và delegate
15
Constructor
Constructor


Có cùng tên với class, có thể không có tham số nào
(parameter) và không trả về bất kỳ giá trị nào (kể cả
void).

Mỗi class phải có ít nhất 1 constructor. Nếu bạn không
tạo constructor cho 1 class thỉ compiler sẽ tự động phát
ra 1 default constructor

Cho phép overload constructor để tạo ra nhiều cách khởi
tạo đối tượng
16
Phân loại Constructor
Phân loại Constructor

Constructor mặc định

Không có tham số

Khởi tạo đối tượng từ class khi chưa biết thông tin gì về nó

Constructor sao chép

Tham số vào là đối tượng cùng lớp

Tạo ra đối tượng như bản sao của đối tượng đầu vào

Constructor không mặc định khác

Có một hay nhiều tham số vào


Tạo đối tương khi biết một số thông tin nào về nó
17
Constructor
Constructor
18
class HocSinh
{
//
public HocSinh()
{
hoTen = “unknown";
namSinh = 1990;
diemVan = diemToan = 0;

}
public HocSinh(HocSinh hs)
{
hoTen = hs.hoTen;
namSinh = hs.namSinh;
diemVan = hs.diemVan;
diemToan = hs.diemToan;

}
public HocSinh(string ht)
{
hoTen = ht;
}
}
Constructor mặc định

Constructor mặc định
Constructor sao chép
Constructor sao chép
Constructor khác
Constructor khác
(tạo học sinh khi biết họ tên)
(tạo học sinh khi biết họ tên)
Constructor
Constructor

Khai báo private cho constructor sẽ ko cho phép khởi tạo
đối tượng
19
Ko thể tạo
thể hiện/obj
class Car
{
// The 'state' of the Car.
public string petName;
public int currSpeed;
// A custom default constructor.
public Car()
{
petName = "Chuck";
currSpeed = 10;
}
// Here, currSpeed will receive the
// default value of an int (zero).
public Car(string pn)
{

petName = pn;
}
// Let caller set the full 'state' of the Car.
public Car(string pn, int cs)
{
petName = pn;
currSpeed = cs;
}

}
20
A custom default
constructor
Instance
Constructors
static void Main(string[] args)
{
// Make a Car called Chuck
going 10 MPH.
Car chuck = new Car();
chuck.PrintState();
// Make a Car called Mary
going 0 MPH.
Car mary = new Car("Mary");
mary.PrintState();
// Make a Car called Daisy
going 75 MPH.
Car daisy = new Car("Daisy",
75);
daisy.PrintState();

}
Thứ tự thực thi các constructor
Thứ tự thực thi các constructor

In C#, constructors are called using the new
keyword.

Khi lớp dẫn xuất được khởi tạo, lớp cơ sở của nó
cũng phải được khởi tạo. Và khi lớp cơ sở này được
khởi tạo thì lớp cơ sở của chính nó cũng phải được
khởi tạo và cứ thế cho đến khi tiến đến lớp
System.Object
21
Ví dụ
Ví dụ
public class MyBaseClass
{
public MyBaseClass()
{
}
public MyBaseClass(int i)
{
}
}
public class MyDerivedClass :
MyBaseClass
{
public MyDerivedClass()
{
}

public MyDerivedClass(int i)
{
}
public MyDerivedClass(int i, int j)
{
}
}
22
Ví dụ 1
Ví dụ 1

Có thể khởi tạo MyDerivedClass như sau:
MyDerivedClass myObj = new MyDerivedClass();

Chuỗi sự kiện sau sẽ lần lượt thực hiện:
➤ The System.Object.Object constructor will execute.
➤ The MyBaseClass.MyBaseClass() constructor will
execute.
➤ The MyDerivedClass.MyDerivedClass() constructor will
execute
23
Ví dụ 2
Ví dụ 2
MyDerivedClass myObj = new MyDerivedClass(4, 8);

Chuỗi các constructor sẽ lần lượt thực hiện như sau:
➤ The System.Object.Object constructor will execute.
➤ The MyBaseClass.MyBaseClass() constructor will
execute.
➤ The MyDerivedClass.MyDerivedClass(int i, int j)

constructor will execute.
Hay
➤ The System.Object.Object constructor will execute.
➤ The MyBaseClass.MyBaseClass(int i)constructor will
execute.
➤ The MyDerivedClass.MyDerivedClass(int i, int j)
constructor will execute.
24
Constructor initializer
Constructor initializer

Có thể sử dụng constructor initializer để xác định loại
constructor nào cần thực hiện

Ví dụ: xác định constructor của lớp cơ bản nào cần dùng
trong định nghĩa của constructor của lớp suy dẫn
(derived class)
public class MyDerivedClass : MyBaseClass
{

public MyDerivedClass(int i, int j) : base(i)
{
}
}
25

×