Bài giảng Lập trình C# 2010: Chương 5 - ĐH Công nghệ Đồng Nai

22 9 0
Bài giảng Lập trình C# 2010: Chương 5 - ĐH Công nghệ Đồng Nai

Đ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

Bài giảng Lập trình C# 2010: Chương 5 - String Class có nội dung trình bày về: Constructor, String Field, String Operators, String Methods, DEMO String & Dictionary.

DONG NAI UNIVERSITY OF TECHNOLOGY DONG NAI UNIVERSITY OF TECHNOLOGY DONG NAI UNIVERSITY OF TECHNOLOGY Abstraction Encapsulation Inheritance Polymorphism DONG NAI UNIVERSITY OF TECHNOLOGY Abstraction Characteristics Properties Object Behaviors  Methods DONG NAI UNIVERSITY OF TECHNOLOGY Encapsulation Characteristics Properties Behaviors  Methods Package DONG NAI UNIVERSITY OF TECHNOLOGY Encapsulation private protected public Inheritance DONG NAI UNIVERSITY OF TECHNOLOGY • 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 “is a” relationship with its base class DONG NAI UNIVERSITY OF TECHNOLOGY Inheritance DONG NAI UNIVERSITY OF TECHNOLOGY Inheritance • Inheritance supports reusability • Reusability – creation of object functionality that may be used in multiple projects DONG NAI UNIVERSITY OF TECHNOLOGY Polymorphism • Methods with identical names have different implementations • The Select method is different for radio buttons, check boxes, and list boxes • Allows a single class to have more than one method with different argument lists DONG NAI UNIVERSITY OF TECHNOLOGY How to create Class DONG NAI UNIVERSITY OF TECHNOLOGY Class Name Constructors Attributes Properties Methods public abstract class CAbstractProduct { private string m _strID ; private string m _strN am e; private string m _strD escription; public CAbstractProduct() { } public string ID { get { return this.m _strID ; } set { this.m _strID = value; } } public string N am e { get { return this.m _strN am e; } set { this.m _strN am e = value; } } public string D escription { get { return this.m _strD escription; } set { this.m _strD escription = value; } } public abstract void doSom ething(); } DONG NAI UNIVERSITY OF TECHNOLOGY Attributes Constructor Properties Methods DONG NAI UNIVERSITY OF TECHNOLOGY public class CProduct : CAbstractProduct { private double m _dPrice; public CProduct() {} public double Price { get { return this.m _dPrice; } set { this.m _dPrice = value; } } public override void doSom ething() { throw new N otIm plem entedException(); } } public class CBook:CProduct { private string m _strISBN ; private string m _strAuthor; private string m _strTitle; public CBook() { } public string ISBN { get { return this.m _strISBN ; } set { this.m _strISBN = value; } } public string Author { get { return this.m _strAuthor; } set { this.m _strAuthor = value; } } public string Title { get { return this.m _strTitle; } set { this.m _strTitle = value; } } } DONG NAI UNIVERSITY OF TECHNOLOGY DONG NAI UNIVERSITY OF TECHNOLOGY public class CCom pactD isc:CProduct { private string m _strArtist; private string m _strTitle; public CCom pactD isc() {} public string Artist { get { return this.m _strArtist; } set { this.m _strArtist = value; } } public string Title { get { return this.m _strTitle; } set { this.m _strTitle = value; } } public override void doSom ething() { throw new N otIm plem entedException(); } } DONG NAI UNIVERSITY OF TECHNOLOGY public class CTravelG uide:CBook { private string m _strCountry; public CTravelG uide() {} public string Country { get { return this.m _strCountry; } set { this.m _strCountry = value; } } } DONG NAI UNIVERSITY OF TECHNOLOGY Garbage Collection DONG NAI UNIVERSITY OF TECHNOLOGY • Operator new allocates memory • When objects are no longer referenced, the CLR 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 DONG NAI UNIVERSITY OF TECHNOLOGY • Use finalizers 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 DONG NAI UNIVERSITY OF TECHNOLOGY public class CTravelG uide:CBook { private string m _strCountry; public CTravelG uide() {} public string Country { get { return this.m _strCountry; } set { this.m _strCountry = value; } } ~ CTravelG uide() { Console.W riteLine("deconstructor"); } } DONG NAI UNIVERSITY OF TECHNOLOGY END ...DONG NAI UNIVERSITY OF TECHNOLOGY DONG NAI UNIVERSITY OF TECHNOLOGY Abstraction Encapsulation Inheritance Polymorphism DONG NAI UNIVERSITY OF TECHNOLOGY Abstraction... DONG NAI UNIVERSITY OF TECHNOLOGY Encapsulation Characteristics Properties Behaviors  Methods Package DONG NAI UNIVERSITY OF TECHNOLOGY Encapsulation private protected public Inheritance DONG NAI. .. child • A derived class has an “is a” relationship with its base class DONG NAI UNIVERSITY OF TECHNOLOGY Inheritance DONG NAI UNIVERSITY OF TECHNOLOGY Inheritance • Inheritance supports reusability

Ngày đăng: 08/05/2021, 12:17

Mục lục

  • Slide 1

  • Slide 2

  • Slide 3

  • Slide 4

  • Slide 5

  • Slide 6

  • Slide 7

  • Slide 8

  • Slide 9

  • Slide 10

  • Slide 11

  • Slide 12

  • Slide 13

  • Slide 14

  • Slide 15

  • Slide 16

  • Slide 17

  • Slide 18

  • Slide 19

  • Slide 20

Tài liệu cùng người dùng

Tài liệu liên quan