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.
Trang 3Abstraction Encapsulation
Inheritance
Polymorphism
Trang 6private protected
public
Trang 7from an existing class
• The existing class is called the
• The inherited class is called the
derived , subclass , or child
relationship with its base class
Trang 8Inheritance
Trang 9• Inheritance supports reusability
• Reusability – creation of object
functionality that may be used in multiple projects
Trang 10• 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
Trang 11How to create Class
Trang 13get { 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();
Constructor
Trang 14get { return this m _dPrice; }
set { this m _dPrice = value ; }
Trang 15private string m _strISBN ;
private string m _strAuthor;
private string m _strTitle;
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; }
}
}
Trang 16{
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; }
Trang 17public class CTravelG uide : CBook
{
private string m _strCountry;
public CTravelG uide()
{ }
public string Country
{
get { return this m _strCountry; }
set { this m _strCountry = value ; }
}
}
Trang 18Garbage Collection
Trang 19• 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
Trang 20• 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
Trang 21public class CTravelG uide: CBook
{
private string m _strCountry;
public CTravelG uide()
{ }
public string Country
{
get { return this m _strCountry; }
set { this m _strCountry = value ; }
Trang 22END