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 (698.73 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

Behaviors



Properties

 Methods

Package


DONG NAI UNIVERSITY OF TECHNOLOGY

Encapsulation
private

protected

public


DONG NAI UNIVERSITY OF TECHNOLOGY

Inheritance

• 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


DONG NAI UNIVERSITY OF TECHNOLOGY

public abstract class CAbstractProduct
{

private string m_strID;
private string m_strName;

Attributes

private string m_strDescription;
public CAbstractProduct() { }

Constructor


public string ID
{
get { return this.m_strID; }
set { this.m_strID = value; }
}
public string Name
{

Properties

get { return this.m_strName; }
set { this.m_strName = value; }
}
public string Description
{
get { return this.m_strDescription; }
set { this.m_strDescription = value; }
}
public abstract void doSomething();
}

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 doSomething()
{
throw new NotImplementedException();
}
}


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 CCompactDisc:CProduct
{
private string m_strArtist;
private string m_strTitle;
public CCompactDisc()
{ }
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 doSomething()
{
throw new NotImplementedException();
}
}


DONG NAI UNIVERSITY OF TECHNOLOGY

public class CTravelGuide:CBook
{
private string m_strCountry;
public CTravelGuide()
{ }
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 CTravelGuide:CBook
{
private string m_strCountry;
public CTravelGuide()
{ }
public string Country
{
get { return this.m_strCountry; }
set { this.m_strCountry = value; }
}
~CTravelGuide()
{
Console.WriteLine("deconstructor");
}
}


DONG NAI UNIVERSITY OF TECHNOLOGY

END




×