Beruflich Dokumente
Kultur Dokumente
-------------------------@&?-------------------------
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
switch (biu thc iu kin) { case <gi tr>: <Cc cu lnh thc hin> <lnh nhy> [default: <Cc cu lnh thc hin mc nh>] } Cu lnh lp: while (Biu thc) <Cu lnh thc hin> do <Cu lnh thc hin> while ( iu kin ) for ([ phn khi to] ; [biu thc iu kin]; [bc lp]) <Cu lnh thc hin> foreach ( <kiu tp hp> <tn truy cp thnh phn > in < tn tp hp>) <Cc cu lnh thc hin>
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
Tru tng ha
Trang 7
Phuc158@yahoo.com
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
public PhanSo(int Tu, int Mau) { _TuSo = Tu; //Neu mau so = 0 thi nem loi ra if (Mau == 0) throw new Exception("Khong khoi tao duoc phan so, mau so phai khac 0."); else _MauSo = Mau; Dem++; } //Methods---------------------------------------------------------//Xuat phan so public string Xuat() { return _TuSo.ToString() + "/" + _MauSo.ToString(); } //Su dung static public static int XuatSoLuongPhanSo() { return Dem; } //Su dung operator public static PhanSo operator +(PhanSo a, PhanSo b) { PhanSo KetQua = new PhanSo(); KetQua.TuSo = a.TuSo * b.MauSo + b.TuSo * a.MauSo; KetQua.MauSo = a.MauSo * b.MauSo; return KetQua; } }//End Class PhanSo class Program { static void Main(string[] args) { try { //Tao cac doi tuong cua lop phan so PhanSo PhanSo1 = new PhanSo(1, 2); PhanSo PhanSo2 = new PhanSo(3, 2); PhanSo PhanSo3 = new PhanSo(5); //Xuat cac phan so Console.WriteLine("Phan so thu nhat la: " + PhanSo1.Xuat()); Console.WriteLine("Phan so thu hai la: " + PhanSo2.Xuat()); Console.WriteLine("Phan so thu ba la: " + PhanSo3.Xuat()); //Xuat so luong cac phan so da tao //Luu y: Khong can khoi tao doi tuong khi truy cap phuong thuc static int SoLuongPhanSo = PhanSo.XuatSoLuongPhanSo(); 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()); }
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
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; } }
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
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{} }
Trang 14
Phuc158@yahoo.com
//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(); } } }
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