Professional Documents
Culture Documents
LP TRNH C#
-------------------------@&?-------------------------
MC LC
Chng 1: Microsoft .NET .............................................................................................................................2
Chng 2: Gii thiu ngn ng C# ................................................................................................................3
Chng 3: Nn tng ngn ng C#..................................................................................................................4
Kiu d liu ...................................................................................................................................................4
Bin v hng ..................................................................................................................................................5
Cu lnh .........................................................................................................................................................5
Namespace .....................................................................................................................................................6
Chng 4: Lp trnh hng i tng .......................................................................................................... 7
Mc tiu ca vic thit k 1 phn mm ......................................................................................................... 7
Qu trnh thit k phn mm..........................................................................................................................7
Cc cch tip cn trong thit k .....................................................................................................................7
Tru tng ha ..............................................................................................................................................7
i tng trong LTHT ...............................................................................................................................8
To v s dng i tng ..............................................................................................................................8
Tnh ng gi Encapsulation ....................................................................................................................10
Tnh k tha - Inheritance, Tnh a hnh - Polymorphism ........................................................................ 11
Tnh tru tng - abstract classes ................................................................................................................13
Giao din Interfaces .................................................................................................................................. 16
Delegate .......................................................................................................................................................17
Event: ...........................................................................................................................................................18
Trang 1
Phuc158@yahoo.com
Trang 2
Phuc158@yahoo.com
C# l ngn ng hng module: M ngun C# c th c vit trong nhng phn c gi l nhng lp,
nhng lp ny cha cc phng thc thnh vin ca n. Nhng lp v nhng phng thc c th c s
dng li trong ng dng hay cc chng trnh khc.
Trang 3
Phuc158@yahoo.com
Trang 4
Phuc158@yahoo.com
Bin v hng
Gn gi tr xc nh cho bin: <kiu> <tn bin> = <gi tr>;
Hng: const <kiu> <tn hng> = <gi tr>;
Kiu lit k:
class ViDuKieuLietKe
{
// Khai bo kiu lit k
enum NhietDoNuoc
{
DoDong = 0,
DoNguoi = 20,
DoAm = 40,
DoNong = 60,
DoSoi = 100,
}
static void Main()
{
System.Console.WriteLine( Nhiet do dong: {0}, NhietDoNuoc.DoDong);
System.Console.WriteLine( Nhiet do nguoi: {0}, NhietDoNuoc.DoNguoi);
System.Console.WriteLine( Nhiet do am: {0}, NhietDoNuoc.DoAm);
System.Console.WriteLine( Nhiet do nong: {0}, NhietDoNuoc.DoNong);
System.Console.WriteLine( Nhiet do soi: {0}, NhietDoNuoc.DoSoi);
}
}
Cu lnh
Phn nhnh khng c iu kin: gi mt trong cc t kha sau: goto, break, continue, return,
statementthrow.
Phn nhnh c iu kin:
if (biu thc iu kin)
{
<lnh 1>
<lnh 2>
....
}
else
{
<lnh 1>
<lnh 2>
...
}
Trang 5
Phuc158@yahoo.com
Namespace
Gi s c mt ngi ni Phc l mt k s, t k s phi i km vi mt lnh vc nht nh no , v nu
khng th chng ta s khng bit c l anh ta l k s cu ng, c kh hay phn mm. Khi mt lp
trnh vin C# s bo rng Phc l CauDuong.KySu phn bit vi CoKhi.KySu hay PhanMem.KySu.
Namespace trong trng hp ny l CauDuong, CoKhi, PhanMem s hn ch phm vi ca nhng t theo
sau. N to ra mt vng khng gian tn sau c ngha. Tng t nh vy ta c to cc namespace
phn thnh cc vng cho cc lp trng tn khng tranh chp vi nhau.
C# a ra t kha using khai bo s dng namespace trong chng trnh:
using < Tn namespace >
to mt namespace dng c php sau:
namespace <Tn namespace>
{
< nh ngha lp A>
< nh ngha lp B >
.....
}
Trang 6
Phuc158@yahoo.com
Tnh ti s dng (reusability): thit k cc thnh phn c th c s dng trong nhiu phn mm
khc nhau
Tnh m rng (extensibility): h tr cc plug-ins.
Tnh mm do (flexibility):
o C th d dng thay i khi thm mi d liu hay tnh nng.
o Cc thay i khng lm nh hng nhiu n ton b h thng
Qu trnh thit k: chia phn mm v thit k theo tng phn, tng component
Tru tng ha: rt cn thit trong thit k. C th: b qua nhng chi tit ca component, quan tm
cc thnh phn mc tru tng.
Xc nh cc component: top-down
Tch hp: bottom-up. Gn kt cc components nh li vi nhau.
Tru tng ha
Trang 7
Phuc158@yahoo.com
i tng l 1 thc th phn mm bao bc cc thuc tnh m t trng thi v cc phng thc lin
quan.
Kiu d liu lp i tng: class
Class c cc tnh cht sau:
o ng gi: cha ng d liu v cc hm/th tc lin quan
o Che giu d liu: cc thc th phn mm khc khng can thip trc tip vo d liu bn trong
c m phi thng qua cc phng thc cho php
To v s dng i tng:
V d: Lp Phn S
namespace Phan1_PhanSo
{
class PhanSo
{
private int _TuSo;
private int _MauSo;
private static int Dem = 0;//Bien dem so luong phan so da duoc tao
//Properties-----------------------------------------------------public int TuSo
{
get { return _TuSo; }
set { _TuSo = value; }
}
public int MauSo
{
get
{
return _MauSo;
}
set
{
if (value == 0)
_MauSo = 1;
else
_MauSo = value;
}
}
//Constructors----------------------------------------------------public PhanSo()
{
_TuSo = 1;
_MauSo = 1;
Dem++;
}
public PhanSo(int So)
{
_TuSo = So;
_MauSo = 1;
Dem++;
}
Trang 8
Phuc158@yahoo.com
Trang 9
Phuc158@yahoo.com
Tnh ng gi 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)
}
}
}
Kt lun:
o T (1)(4)(7) => Private ch trong ni b class A.
o T (2)(5)(8) => Protected ch trong ni b class A v cc class k tha n.
o T (3)(6)(9) => Public truy xut bt k ni u u c.
o Internal ch c truy xut trong phm vi file DLL
o Protected Internal c truy xut trong phm vi file DLL v lp k tha.
Trang 10
Phuc158@yahoo.com
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;
}
Trang 11
Phuc158@yahoo.com
Trang 12
Phuc158@yahoo.com
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());
}
}
}
}
Trang 13
Phuc158@yahoo.com
}
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{}
}
Trang 14
Phuc158@yahoo.com
Trang 15
Phuc158@yahoo.com
Trang 16
Phuc158@yahoo.com
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());
//Kt lun:
//Khi nim delegate tng t nh con tr hm.
//u im ca Delegate l tnh kh bin ca n.
//C th gi bt c phng thc no c 2 i s int v tr v tr int.
}
}
}
Trang 17
Phuc158@yahoo.com
Event:
As compares to delegates events works with source and listener methodology . So listeners who are
interested in receiving some events they subscribe to the source.Once this subscription is done the source
raises events to all of its 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);
}
}
}
Trang 18
Phuc158@yahoo.com