Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 26 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
26
Dung lượng
236,48 KB
Nội dung
Chương XÂY DỰNG ỨNG ĐA TẦNG Nội dung Lập trình hướng đối tượng Mơ hình UML Xây dựng ứng dụng theo mơ hình tầng Lập trình hướng đối tượng Abstraction Encapsulation Inheritance Polymorphism Abstraction Abstraction means working with something we know how to use without knowing how it works internally, The ability to create a new class from an existing class, The existing class is called the base, superclass, or parent, The inherited class is called the derived, subclass, or child, A derived class has an relationship with its base class Abstraction Abstraction interface In the C# language the interface is a definition of a role (a group of abstract actions) Interfaces are also known as contracts or specifications of behavior An object can have multiple roles (or implement multiple interfaces/contracts) and its users can utilize it from different points of view Abstraction Encapsulation Encapsulation is one of the main concepts in OOP, it is also called "information hiding“ An object has to provide its users only with the essential information for manipulation, without the internal details Access specifier: Private Protected Public Internal Proteced Internal Encapsulation Internal types or members are accessible only within files in the same assembly Types or members that have access modifier protected internal can be accessed from the current assembly or from types that are derived from the containing class 10 Inheritance Generalization: a general statement or concept obtained by inference from specific cases The ability to create a new class from an existing class The existing class is called the base, superclass, or parent The inherited class is called the derived, subclass, or child A derived class has an relationship with its base class Inheritance supports reusability 12 Polymorphism Polymorphism is often referred to as the third pillar of OOP, after encapsulation and inheritance Polymorphism is a Greek word that means "manyshaped" and it has two distinct aspects: At run time, objects of a derived class may be treated as objects of a base class in places such as method parameters and collections or array Base classes may define and implement virtual methods, and derived classes can override them 13 Polymorphism 14 Polymorphism 15 Polymorphism 16 Polymorphism 17 Polymorphism 18 Polymorphism Kết thực thi 19 Class in C# Class name Constructors Attributes Properties Methods 20 Class in C# Attributes Properties Constructor Methods 21 Garbage Collection Operator new allocates memory When objects are no longer referenced, the CLR (Common Language Runtime) performs garbage collection Garbage collection helps avoid memory leaks (running out of memory because unused memory has not been reclaimed) Allocation and deallocation of other resources (database connections, file access, etc.) must be explicitly handled by programmers 22 Garbage Collection Use finalizer in conjunction with the garbage collector to release resources and memory Before garbage collector reclaims an object’s memory, it calls the object’s finalizer Each class has only one finalizer (also called destructor) Name of a destructor is the ~ character, followed by the class name Destructors not receive any arguments 23 Garbage Collection class Bird { ~Bird() // destructor { // cleanup statements } } protected override void Finalize() { try { // Cleanup statements } finally { base.Finalize(); } } 24 Garbage Collection 25 ... reclaimed) Allocation and deallocation of other resources (database connections, file access, etc.) must be explicitly handled by programmers 22 Garbage Collection Use finalizer in conjunction with... 19 Class in C# Class name Constructors Attributes Properties Methods 20 Class in C# Attributes Properties Constructor Methods 21 Garbage Collection Operator new allocates memory When objects are...Nội dung Lập trình hướng đối tượng Mơ hình UML Xây dựng ứng dụng theo mơ hình tầng Lập trình hướng đối