Tải bản đầy đủ (.pdf) (61 trang)

CLASS, STRUCT, INTERFACE - ThS. Nguyễn Hà Giang pdf

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 (1.2 MB, 61 trang )

CLASS, STRUCT, INTERFACE
ThS. Nguyễn Hà Giang
1Nguyen Ha Giang -2009
Content
1.
Class definition
2. Constructor & destructor
3. Property
4. Indexer
5. Polymorphism
6. Downcast –Upcast
7. Abstract class –Sealed class
8. Struct
9. Interface
2Nguyen Ha Giang -2009
Class declaration
Nguyen Ha Giang -2009 3
[access modifier] class <class_name> [:base class, interfaces…]
{
// class body
}
Access modifier
public
protected
internal
protected internal
private (default)
Base class: only one class or not
Interface: implement some interfaces
Class declaration
Nguyen Ha Giang -2009 4


public class Student
{
// data member

// function member

}
Access modifier public
No base class
Default derive from
object (System.Object)
Class member
Nguyen Ha Giang -2009 5
Class
Constructor
Properties
Indexer
Delegate,
event
Method
Field,
const,
readonly
Nested
type
Constructor

Called when create object
• Same class name
• Class has default constructor which no

parameter
• Class has multiple constructor (overloading
constructor)
Nguyen Ha Giang -2009 6
Constructor

Default constructor
•No parameter
•Create object when has no any information
• Copy constructor
•Input parameter is another object
•Create new object like input object
• Other constructors
•One or more parameter
•Create new object with some information
Nguyen Ha Giang -2009 7
Constructor

Ex
Nguyen Ha Giang -2009 8
public class Student
{
// data member
string name;
int year;
float mark;
// function member

}
public Student()

{
name = “Ha Giang";
year = 1978;
mark = 5;
}
public Student(Student st)
{
name = st.name;
year = st.year;
mark = st.mark;
}
public Student(string sname)
{
name = sname;
}
Constructor

The private constructor prevents the class from
being created
Nguyen Ha Giang -2009 9
class Student
{
//
private Student()
{
}
}
class Program
{
static void Main(string[] args)

{
Student s = new Student();
}
}
Constructor

Constructor chaining
•Call one constructor from another constructor
•Usage
•base(parameter): call constructor of base class
•this(parameter): call a constructor in the current class
Nguyen Ha Giang -2009 10
<constructor of class A> : base(list of parameter)
<constructor of class A> : this(list of parameter)
Constructor
Nguyen Ha Giang -2009 11
class Person {
protected string name;
protected int year;
public Person(string name, intyear) {
this.name = name;
this.year= year;
}
}
class Student : Person {
float mark;
public Student(string name, intyear, float mark)
: base(name, year)
{
this.mark= mark;

}
}
Call base constructor
Constructor
Nguyen Ha Giang -2009 12
class Student
{
string name;
int year;
float mark;
public Student(string name, intyear)
{
this.name = name;
this.year= year;
}
public Student(string name, intyear, float mark)
: this(name, year)
{
this.mark= mark;
}
}
Call constructor
Destructor

Features of destructor
•A class can have one destructor only
•Destructor cannot be inherited or overloaded
•Destructor are invoked automatically
•Destructor can not have modifiers or parameters
Nguyen Ha Giang -2009 13

class Student
{
//…
~ Student()// destructor
{
//Code for cleanup resources
}
}
Instantiating an object

Declare
•Data member of class
•Variable of method
• Using “new” keyword
Nguyen Ha Giang -2009 14
Student st;
st
st= new Student(“Ha Giang”, 1978);
<Student>
st
Method

Methods are declared within a class by
•Access modifier
•Return value
•Name of method
•List of parameter
• Including
•Static method
•Non static method

Nguyen Ha Giang -2009 15
Method
Nguyen Ha Giang -2009 16
class Student {
static intnumber; // save number of objects
string name;
//constructor
public Student(string n) {
name = n;
number++; //increment 1
}
public void Show() {
Console.WriteLine("{0} ",name);
}
public static intNumberOfInstance() {
return number; // return number of objects
}
}
Method
Nguyen Ha Giang -2009 17
class Program
{
static void Main(string[] args)
{
Student s1 = new Student("Ha Giang");
Student s2 = new Student("Ngoc Thao");
Student s3 = new Student("Ha Nam");
s1.Show();
intnum = Student.NumberOfInstance();
Console.WriteLine("Number of objects: {0}", num);

Console.ReadLine();
}
}
Invoked by instance of class
Called directly from the class
Overload

Methods
•same name
• Different sets of parameters determined by
•The number
•Types
•Order of the parameter
Nguyen Ha Giang -2009 18
Overload
Nguyen Ha Giang -2009 19
class HagLib{
public void Swap(ref inta, ref intb) {
inttemp = a;
a = b;
b = temp;
}
public void Swap(ref long a, ref long b) {
long temp = a;
a = b;
b = temp;
}
public void Swap(ref float a, ref float b) {
float temp = a;
a = b;

b = temp;
}
}// end class
Different by type
of parameters
Virtual Method
Nguyen Ha Giang -2009 20
Employee
SaleEmployee
Virtual Method
Nguyen Ha Giang -2009 21
class Employee {
public void CalculatePay()
{
Console.WriteLine("Employee.CalculatePay");
}
}
class SaleEmployee: Employee {
public void CalculatePay()
{
Console.WriteLine("SaleEmployee.CalculatePay");
}
}
Employee e = new Employee();
SaleEmployee s = new SaleEmployee();
e.CalculatePay();
s.CalculatePay();
e = new SaleEmployee();
e.CalculatePay();
Why not polymorphism?

Virtual Method
Nguyen Ha Giang -2009 22
class Employee {
public virtual void CalculatePay()
{
Console.WriteLine("Employee.CalculatePay");
}
}
class SaleEmployee: Employee {
public override void CalculatePay()
{
Console.WriteLine("SaleEmployee.CalculatePay");
}
}
Employee e = new Employee();
SaleEmployee s = new SaleEmployee();
e.CalculatePay();
s.CalculatePay();
e = new SaleEmployee();
e.CalculatePay();
Polymorphism!
Property

Provide a flexible mechanism to read, write or
compute the values of private fields
Nguyen Ha Giang -2009 23
Private member
private string name
-GetName()
-SetName(…)

Private member
private string name
Property: NAME
Getter/Setter method Property
Property

Properties as “smart” fields
Nguyen Ha Giang -2009 24
[modifers]<type><property-name>
{
[set{<accessor-body>}]
[get{<accessor-body>}]
}
[modifers]<type><property-name>
{
set {<accessor-body>}
}
[modifers]<type><property-name>
{
get {<accessor-body>}
}
Write-only property Read-only property
Property
Nguyen Ha Giang -2009 25
class Employee {
// fields
private string name;
private float salary;
// properties
public string NAME {

get {
return name;
}
set {
name = value;
}
}
public float SALARY {
get {
return salary;
}
}
}
Read write property
Read only property

×