1. Trang chủ
  2. » Công Nghệ Thông Tin

Lập trình cơ sở dữ liệu C Sharp-Phần 3 pdf

118 847 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Chương 3 Xây dựng lớp và giao diện

  • Nội dung

  • Nội dung

  • Overview

  • Object-oriented Programming (OOP)

  • What is an Object?

  • Cấu trúc của object

  • Life Cycle of an Object

  • Tạo lớp trong C#

  • Khóa truy xuất cho class (Access modifier)

  • Interface là gì?

  • Biểu diễn interface bằng UML

  • Khai báo interface

  • So sánh Interface và class

  • Các thành phần của class

  • Constructor

  • Phân loại Constructor

  • Slide 18

  • Slide 19

  • Slide 20

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

  • Ví dụ

  • Ví dụ 1

  • Ví dụ 2

  • Constructor initializer

  • Quiz

  • static Constructors

  • Slide 28

  • Slide 29

  • Destructor

  • Slide 31

  • Member definition

  • Access modifier or Accessibility Levels

  • Field and constant

  • Field và Property

  • Property

  • Slide 37

  • Slide 38

  • Slide 39

  • Defining methods

  • Defining Method

  • Static members

  • Static member

  • Static and nonstatic Method

  • Slide 45

  • Slide 46

  • Slide 47

  • Slide 48

  • Từ khóa “This”

  • Từ khóa “This”: sử dụng ngầm định

  • Từ khóa “This”: sử dụng tường minh

  • Ví dụ sử dụng this tường minh

  • Indexer

  • Slide 54

  • Định nghĩa indexer

  • Ví dụ về indexer

  • Inheritance

  • Slide 58

  • Ví dụ về inheritance

  • Slide 60

  • Mối quan hệ giữa lớp cơ sở và lớp dẫn xuất

  • Slide 62

  • Slide 63

  • Khai báo constructor của lớp suy dẫn

  • Demo for inheritance

  • Inheritance Hierarchy Using protected Instance Variables

  • Bất lợi khi sử dụng protected

  • Thiết kế tốt theo quan điểm SW engineering

  • Overriding method

  • Example of a method override

  • Slide 71

  • Polymorphism

  • Ví dụ về polymorphism

  • Slide 74

  • Ví dụ về tính đa hình

  • Slide 76

  • Ví dụ về tính đa hình (tt)

  • Dynamic binding (late binding)

  • Ví dụ của dynamic binding

  • Lớp trừu tượng (Abstract class)

  • Khai báo Lớp Abstract

  • Abstract class

  • Abstract method

  • Abstract property

  • Abstract class - example

  • Slide 86

  • Slide 87

  • Case study: Payroll system

  • Slide 89

  • Slide 90

  • Case study :Polymorphic processing

  • Toán tử is

  • Downcast

  • Note

  • Từ khóa new

  • VD Polymorphism

  • Slide 97

  • Method overloading

  • Method - overload

  • Slide 100

  • Sealed Class

  • Sealed method

  • Slide 103

  • Static binding (early binding)

  • Static binding và inlining the code

  • Nested class

  • Slide 107

  • Slide 108

  • Interface

  • Khai báo Interface

  • Slide 111

  • Thực thi Interface

  • Interface - example

  • Slide 114

  • So sánh interface và abstract class

  • Sử dụng interface hay abstract class

  • Creating class libraries

  • Slide 118

Nội dung

1 Chương 3 Chương 3 Xây dưng lơp va giao diêṇ ́ ̀ ̣ Xây dưng lơp va giao diêṇ ́ ̀ ̣ N i dungộ N i dungộ  Lâp trinh h ng đôi t ng la gi?̣ ̀ ướ ́ ượ ̀ ̀  Khai báo l pớ  Khai bao interfacé  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)  Ky thuât lâp trinh truyên thông ( procedural ̃ ̣ ̣ ̀ ̀ ́ programming):  all functionality is contained in a few modules of code (often just one)  Kho thay thê cai tiêń ́ ̉ ́  Ky thuât OOP :̃ ̣  S dung nhiêu modules of code, môi module cung ử ̣ ̀ ̃ câp 1 ch c năng riêng va co thê đ c lâp ́ ứ ̀ ́ ̉ ượ ̣ (isolated) hay thâm chi đôc lâp hoan toan v i ̣ ́ ̣ ̣ ̀ ̀ ớ nhau.  Dê dang s dung lai cac modulẽ ̀ ử ̣ ̣ ́ 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 truc cua object́ ́ ̉ Câu truc cua object́ ́ ̉  Môt object gôm co: ̣ ̀ ́  Field  Method 7 Life Cycle of an Object Life Cycle of an Object  Gôm 2 giai đoan (two important stages):̀ ̣ ➤ Construction: đôi t ng cân đ c kh i tao, va đ c th c ́ ượ ̀ ượ ở ̣ ̀ ượ ự thi b i ham constructor ở ̀ ➤ Destruction: khi đôi t ng bi huy bo, 1 nhiêm vu clean-́ ượ ̣ ̉ ̉ ́ ̣ ̣ up cân đ c th c thi đê giai phong bô nh ̀ ượ ự ̉ ̉ ́ ̣ ớ  ham ̀ 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 2 khóa truy xu tộ ỉ ấ  Internal: ch cho phép s d ng bên trong project ỉ ử ụ hiên hanh (default)̣ ̀ internal class MyClass { // Class members. }  Public: cho phép cac project bên ngoài truy ́ xu tấ public class MyClass { // Class members. } 10 [...]... 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   Có thể... Đươ c goi tự đông trươ c khi 1 điên hinh cua lớp ̣ ̣ ̉ ̀ ̉ đươ c tao hay bât kỳ static member nao đươ c ̣ ́ ̀ tham chiêu ́ Môi class chỉ có thể có 1 constructor static ̃ Constructor static không chứa access modifier và bât kỳ tham số nao ́ ̀ Constructor static chỉ thư c thi đung 1 lân, bât kể ́ ̀ ́ có bao nhiêu đôi tượng đươ c tao ra ́ ̣ 27 static Constructors    A static constructor... constructor can never be called directly; instead, it is executed when one of the following occurs: ➤ An instance of the class containing the static constructor is created ➤ A static member of the class containing the static constructor is accessed In both cases, the static constructor is called first, before the class is instantiated or static members accessed No matter how many instances of a class are created,... Destructor  Th c hiện nhiệm vụ “clean” khi đối tượng bị hủy    Trùng tên lớp và c dấu “~” phía trư c Không c tham số và access modifier Mỗi lớp chỉ c 1 destructor class HocSinh { // ~HocSinh() { siSo ; } } 30 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,... public HocSinh(string ht) { hoTen = ht; } } Constructor m c định Constructor sao chép Constructor kh c (tạo h c sinh khi biết họ tên) 18 Constructor  Khai báo private cho constructor sẽ ko cho phép khởi tạo đối tượng Ko thể tạo thể hiện/obj 19 class Car { // The 'state' of the Car public string petName; public int currSpeed; // A custom default constructor public Car() { petName = "Chuck"; A custom... sử dung constructor initializer để xac đinh loai ̣ ́ ̣ ̣ constructor nao c n thư c hiên ̀ ̀ ̣ Ví du: xac đinh constructor cua lớp c ban nao c n dung ̣ ́ ̣ ̉ ̉ ̀ ̀ ̀ trong đinh nghia cua constructor cua lớp suy dân (derived ̣ ̃ ̉ ̉ ̃ class) public class MyDerivedClass : MyBaseClass { public MyDerivedClass(int i, int j) : base(i) { } } 25 Quiz public class MyBaseClass { public MyBaseClass() :... = pn; Car mary = new Car("Mary"); currSpeed = cs; } mary.PrintState(); // Make a Car called Daisy } going 75 MPH Car daisy = new Car("Daisy", 20 Thứ tự thư c thi cac constructor ́   In C# , constructors are called using the new keyword Khi lớp dân xuât đươ c khởi tao, lớp c sở cua nó ̃ ́ ̣ ̉ cung phai đươ c khởi tao Và khi lớp c sở nay đươ c ̃ ̉ ̣ ̀ khởi tao thì lớp c sở cua chinh... its static constructor will only be called once All nonstatic constructors are also known as instance constructors 28 public class Bus Ví dụ 1 { // Static constructor: static Bus() { System.Console.WriteLine("The static constructor invoked."); } public static void Drive() { System.Console.WriteLine("The Drive method invoked."); } } class TestBus { Output The static constructor invoked static void Main()... interface như khởi tao ̣ ̣ ̣ 1 class  14 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    Có cung tên với class, có thể không có tham số nao ̀ ̀ (parameter) và không trả về bât kỳ giá trị nao (kể cả... hiên có trong tai khoan Môi đôi ̉ ̃ ̀ ̣ ̀ ̉ ̃ ́ tượng bank account c n phai biêt số cuả chinh nó ̀ ̉ ́ ́ nhưng không c n phai biêt số cua cac đôi tương ̀ ̉ ́ ̉ ́ ́ bank account khac ́ These attributes are not necessarily accessible directly Có thể dung properties để truy xuât đên cac thu c tinh ̀ ́ ́ ́ ̣ ́ cua đôi tương ̉ ́ Properties chứa get accessors để đoc giá trị từ cac thuôc . } 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. which means creating a real, named instance of a class. 6 C u truc cua object́ ́ ̉ C u truc cua object́ ́ ̉  Môt object gôm co: ̣ ̀ ́  Field  Method 7 Life Cycle of an Object Life Cycle. i tao ̉ ở ̣ ̣ ư ở ̣ 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

Ngày đăng: 21/06/2014, 09:20

TỪ KHÓA LIÊN QUAN