Ngôn ngữ lập trình C#
MỤC LỤC
Chương 1: Microsoft .NET.2
Chương 2: Giới thiệu ngôn ngữ C#.3
Chương 3: Nền tảng ngôn ngữ C#.4
Kiểu dữ liệu.4
Biến và hằng.5
Câu lệnh.5
Namespace.6
Chương 4: Lập trình hướng đối tượng.7
Mục tiêu của việc thiết kế 1 phần mềm.7
Quá trình thiết kế phần mềm.7
Các cách tiếp cận trong thiết kế.7
Trừu tượng hóa.7
Đối tượng trong LTHĐT.8
Tạo và sử dụng đối tượng.8
Tính đóng gói –Encapsulation.10
Tính kế thừa -Inheritance, Tính đahình -Polymorphism.11
Tính trừu tượng -abstract classes.13
Giao diện –Interfaces.16
Delegate.17
Event:.18
hanSo();
Console.WriteLine("So luong phan so da tao: {0}", SoLuongPhanSo);
//Tinh tong 2 phan so, su dung operator
PhanSo Tong = PhanSo1 + PhanSo2;
Console.WriteLine(PhanSo1.Xuat() + " + "
+ PhanSo2.Xuat() + "=" + Tong.Xuat());
}
Phuc158@yahoo.com Trang 10
catch (Exception ex)
{
Console.WriteLine("Loi! " + ex.Message);
}
}
}
}//End
Tính đóng gói – Encapsulation:
The process of hiding all the internal details of an object from the outside world.
namespace Phan2_TinhDongGoi
{
class A
{
private int MyPrivate = 0;
protected int MyProtected = 0;
public int MyPublic = 0;
public void MyMethodA()
{
MyPrivate = 10;//OK! (1)
MyProtected = 10;//OK!(2)
MyPublic = 10;//OK!(3)
}
}
class B : A
{
public void MyMethodB()
{
//MyPrivate = 100;//Error! (4)
MyProtected = 100;//OK! (5)
MyPublic = 100;//OK! (6)
}
}
class Program
{
static void Main(string[] args)
{
A a = new A();
//a.MyPrivate = 10;//Error (7)
//a.MyProtected = 10;//Error (8)
a.MyPublic = 10;//OK! (9)
}
}
}
Kết luận:
o Từ (1)(4)(7) => Private chỉ trong nội bộ class A.
o Từ (2)(5)(8) => Protected chỉ trong nội bộ class A và các class kế thừa nó.
o Từ (3)(6)(9) => Public truy xuất bất kì nơi đâu đều được.
o Internal chỉ được truy xuất trong phạm vi file DLL
o Protected Internal được truy xuất trong phạm vi file DLL và lớp kế thừa.
Phuc158@yahoo.com Trang 11
Tính kế thừa - Inheritance, Tính đa hình - Polymorphism
namespace Phan3_KeThua2
{
class Shape
{
protected string _Name;
//Properties
public string Name
{
get { return _Name; }
set { _Name = value; }
}
//Methods
public virtual float Area()
{
return 0;
}
}
class Rectangle : Shape
{
protected float _Width;
protected float _Height;
//Properties
public float Width
{
get { return _Width; }
set { _Width = value; }
}
public float Height
{
get { return _Height; }
set { _Height = value; }
}
//Constructors
public Rectangle()
{
_Width = _Height = 0;
}
Phuc158@yahoo.com Trang 12
public Rectangle(string name, int width, int height)
{
_Name = name;
_Width = width;
_Height = height;
}
//Methods
public override float Area()
{
return Width * Height;
}
}
class Square : Rectangle
{
//Properties
public float Side
{
get { return _Width; }
set { _Width = _Height = value; }
}
//Constructors
public Square(string name, float side)
{
_Name = name;
Side = side;
}
}
class Circle : Shape
{
protected float _R;
//Properties
public float R
{
get { return _R; }
set { _R = value; }
}
//Constructors
public Circle(string name, float r)
{
_Name = name;
_R = r;
}
//Methods
public override float Area()
{
return (float)Math.PI * R * R;
}
}
Phuc158@yahoo.com Trang 13
class Program
{
static void Main(string[] args)
{
Shape[] MyShapes = new Shape[] {new Rectangle("Rectangle",50,20),
new Square("Square",50),
new Circle("Circle",20)};
foreach (Shape s in MyShapes)
{
Console.WriteLine("Name: " + s.Name);
Console.WriteLine("Area: " + s.Area().ToString());
}
}
}
}
Tính trừu tượng - abstract classes
Following are features of a abstract class :
o You can not create a object of abstract class
o Abstract class is designed to act as a base class (to be inherited by other classes).
o Abstract class is a design concept in program development and provides a base upon which other
classes are built.
o Abstract classes are similar to interfaces: After declaring an abstract class, it can not be instantiated
on it's own, it must be inherited.
o Abstract classes are different to interfaces: Abstract classes can have implementation or pure abstract
methods which should be implemented in the child class.
namespace Phan4_TinhTruuTuong
{
class Point
{
//Fields
private int _X;
private int _Y;
//Properties
public int X
{
get { return _X; }
set { _X = value; }
}
public int Y
{
get { return _Y; }
set { _Y = value; }
}
//Constructors
public Point(int x, int y)
{
_X = x;
_Y = y;
}
//Methods
public string Output()
{
return "(" + _X.ToString() + "," + _Y.ToString() + ")";
}
}
Phuc158@yahoo.com Trang 14
abstract class AShape
{
//Properties
public abstract Point Center
{
get;
set;
}
//MeThods
//This method must be implemented at child class
public abstract float Area();
//Abstract class: This method can be used by child class object
//But Interface class can not
//because all method at Interface must be implemented
public void WriteAString()
{
Console.WriteLine("Hello! this is a method at abstract class");
}
}
class Rectangle : AShape
{
private Point _TopLeft;
private int _Width;
private int _Height;
//Constructors
public Rectangle(Point topLeft, int width, int height)
{
_TopLeft = topLeft;
_Width = width;
_Height = height;
}
//Properties
public Point TopLeft
{
get { return _TopLeft; }
set { _TopLeft = value; }
}
public int Width
{
get { return _Width; }
set { _Width = value; }
}
public int Height
{
get { return _Height; }
set { _Height = value; }
}
//This property is override the Center property at abstract class AShape
public override Point Center
{
get { return new Point(TopLeft.X + Width / 2, TopLeft.Y + Height / 2); }
set{}
}
Phuc158@yahoo.com Trang 15
//Methods-This method is override the Area method at abstract class AShape
public override float Area()
{
return Width * Height;
}
}
class Circle : AShape
{
private Point _Center;
private float _R;
//Properties
public float R
{
get { return _R; }
set {_R = value;}
}
public override Point Center
{
get { return _Center; }
set { _Center = value; }
}
//Constructors
public Circle(Point center, float r)
{
_Center = center;
_R = r;
}
//Mehthods
public override float Area()
{
return (float) (Math.PI * R * R);
}
}
class Program
{
static void Main(string[] args)
{
Point MyPoint = new Point(0,0);
Rectangle R = new Rectangle(MyPoint,50,30);
Circle C = new Circle(MyPoint, 40);
Console.WriteLine("Center of the rectangle: " + R.Center.Output());
Console.WriteLine("Center of the circle: " + C.Center.Output());
Console.WriteLine("Area of the rectangle: " + R.Area());
Console.WriteLine("Area of the circle: " + C.Area());
R.WriteAString();
C.WriteAString();
}
}
}
Phuc158@yahoo.com Trang 16
Giao diện – Interfaces
Following are features of a abstract class :
o Single Class can implement multiple interfaces.
o If a class implements a interface then it has to provide implementation to all its methods.
namespace Phan5_GiaoDien
{
interface IMyInterface
{
void WriteName();
}
class Person : IMyInterface
{
private string Name;
public Person(string name)
{
Name = name;
}
public void WriteName()
{
Console.WriteLine(Name);
}
}
class Building : IMyInterface
{
private string Name;
public Building(string name)
{
Name = name;
}
public void WriteName()
{
Console.WriteLine(Name);
}
}
class Program
{
static void Main(string[] args)
{
IMyInterface[] School = new IMyInterface[]
{ new Person("Phuc"), new Building("I Buiding") };
foreach (IMyInterface I in School)
{
Console.Write(I.GetType().Name.ToString() + ": ");
I.WriteName();
}
}
}
}
Phuc158@yahoo.com Trang 17
Delegate:
Delegate is a class that can hold a reference to a method or a function.Delegate class has a signature and it
can only reference those methods whose signature is compliant with the class.Delegates are type-safe
functions pointers or callbacks.
namespace Phan6_Delegate
{
//Khai bao delegate
delegate int pFunction(int a, int b);
class TestDelegate
{
public int Add(int a, int b)
{
return a + b;
}
public int Mul(int a, int b)
{
return a * b;
}
}
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
pFunction p;// Tao bien delegate
TestDelegate t = new TestDelegate();
//Su dung delegate
p = new pFunction(t.Add);
Console.WriteLine(p(a,b).ToString());
p = new pFunction(t.Mul);
Console.WriteLine(p(a,b).ToString());
//Kết luận:
//Khái niệm delegate tương tự như con trỏ hàm.
//Ưu điểm của Delegate đó là tính khả biến của nó.
//Có thể gọi bất cứ phương thức nào có 2 đối số int và trả về trị int.
}
}
}
Phuc158@yahoo.com Trang 18
Event:
As compares to delegates events works with source and listener methodology . So listener’s who are
interested in receiving some events they subscribe to the source.Once this subscription is done the source
raises events to all of it’s listener when needed.One source can have multiple listeners.
namespace Tuan8_Event
{
class MyClass
{
//Khai bao delegate
public delegate void MyDelegate(string Message);
//Khai bao su kien
public event MyDelegate MyEvent;
public void RaiseEvent(string Message)
{
if(MyEvent!=null)
MyEvent(Message);
}
}
class Program
{
static void Main(string[] args)
{
//Tao the hien cua class
MyClass ShowMessage = new MyClass();
//Goi su kien cho doi tuong ShowMessage
ShowMessage.MyEvent += new MyClass.MyDelegate(ShowMessage_MyEvent);
string Message = Console.ReadLine();
ShowMessage.RaiseEvent(Message);
}
//Ham duoc delegate
static void ShowMessage_MyEvent(string Message)
{
Console.WriteLine("Your message is: {0}",Message);
}
}
}
File đính kèm:
CSharp_OOP.pdf

