Tải bản đầy đủ (.pptx) (22 trang)

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

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 (516.43 KB, 22 trang )

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 do 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




×