Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 61 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
61
Dung lượng
1,2 MB
Nội dung
CLASS,STRUCT, INTERFACE
ThS. NguyễnHà Giang
1Nguyen HaGiang -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 HaGiang -2009
Class declaration
Nguyen HaGiang -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 HaGiang -2009 4
public class Student
{
// data member
…
// function member
…
}
Access modifier public
No base class
Default derive from
object (System.Object)
Class member
Nguyen HaGiang -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 HaGiang -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 HaGiang -2009 7
Constructor
•
Ex
Nguyen HaGiang -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 HaGiang -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 HaGiang -2009 10
<constructor of class A> : base(list of parameter)
<constructor of class A> : this(list of parameter)
[...]... { set { } } Write-only property NguyenHaGiang- 2009 [modifers] { get { } } Read-only property 24 Property 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; } } } NguyenHaGiang- 2009 Read... HaGiang- 2009 22 Property • Provide a flexible mechanism to read, write or compute the values of private fields Getter/Setter method Property Private member Private member private string name private string name - GetName() - SetName(…) NguyenHaGiang- 2009 Property: NAME 23 Property • Properties as “smart” fields [modifers] { [ set { } ] [ get { . CLASS, STRUCT, INTERFACE ThS. Nguyễn Hà Giang 1Nguyen Ha Giang -2 009 Content 1. Class definition 2. Constructor & destructor 3 class –Sealed class 8. Struct 9. Interface 2Nguyen Ha Giang -2 009 Class declaration Nguyen Ha Giang -2 009 3 [access modifier] class <class_name> [:base class, interfaces…] { // class body } Access. information Nguyen Ha Giang -2 009 7 Constructor • Ex Nguyen Ha Giang -2 009 8 public class Student { // data member string name; int year; float mark; // function member … } public Student() { name = “Ha Giang& quot;; year