Sie sind auf Seite 1von 18

NGN NG 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

Chng 1: Microsoft .NET


Microsoft .NET gm 2 phn chnh : Framework v Integrated Development Environment (IDE) o Framework cung cp nhng g cn thit v cn bn, ch Framework c ngha l khung hay khung cnh trong ta dng nhng h tng c s theo mt qui c nht nh cng vic c tri chy. o IDE th cung cp mt mi trng gip chng ta trin khai d dng, v nhanh chng cc ng dng da trn nn tng .NET. Nu khng c IDE chng ta cng c th dng mt trnh son tho v nh Notepad hay bt c trnh son tho vn bn no v s dng command line bin dch v thc thi, tuy nhin vic ny mt nhiu thi gian.

Trang 2

Phuc158@yahoo.com

Chng 2: Gii thiu ngn ng C#


C# l ngn ng n gin: C# loi b mt vi s phc tp v ri rm ca nhng ngn ng nh Java v c++, bao gm vic loi b nhng macro, nhng template, a k tha, v lp c s o (virtual base class). Chng l nhng nguyn nhn gy ra s nhm ln hay dn n nhng vn cho cc ngi pht trin. C# l ngn ng hin i: iu g lm cho mt ngn ng hin i? Nhng c tnh nh l x l ngoi l, thu gom b nh t ng, nhng kiu d liu m rng, v bo mt m ngun l nhng c tnh c mong i trong mt ngn ng hin i. C# cha tt c nhng c tnh trn. C# l ngn ng hng i tng: Nhng c im chnh ca ngn ng hng i tng (Object-oriented language) l s ng gi (encapsulation), s k tha (inheritance), v a hnh (polymorphism). C# h tr tt c cc c tnh trn. C# l ngn ng mnh m v cng mm do: C# c s dng cho nhiu cc d n khc nhau nh l to ra ng dng x l vn bn, ng dng ha, bn tnh C# l ngn ng t t kha:

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

Chng 3: Nn tng ngn ng C#


Kiu d liu
C# l ngn ng lp trnh mnh v kiu d liu, mt ngn ng mnh v kiu d liu l phi khai bo kiu ca mi i tng khi to (kiu s nguyn, s thc, kiu chui, kiu iu khin...) v trnh bin dch s gip cho ngi lp trnh khng b li khi ch cho php mt loi kiu d liu c th c gn cho cc kiu d liu khc. Kiu d liu ca mt i tng l mt tn hiu trnh bin dch nhn bit kch thc v kh nng ca mt i tng. C# phn tp hp kiu d liu ny thnh hai loi: Kiu d liu gi tr (value) v kiu d liu tham chiu (reference). i vi mt kiu d liu gi tr th s c lu gi kch thc tht trong b nh cp pht l stack. Trong khi th a ch ca kiu d liu tham chiu th c lu trong stack nhng i tng tht s th lu trong b nh heap. Ghi ch: Tt c cc kiu d liu xy dng sn l kiu d liu gi tr ngoi tr cc i tng v chui. V tt c cc kiu do ngi dng nh ngha ngoi tr kiu cu trc u l kiu d liu tham chiu.

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

Chng 4: Lp trnh hng i tng


Mc tiu ca vic thit k 1 phn mm:
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 phn mm:


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.

Cc cch tip cn trong thit k


Thit k theo hm/th tc: o Tm ra cc hm/th tc hon tt cc yu cu o Kt qu l h thng cu trc v mi quan h gia cc hm/th tc Thit k theo module: o Phn tch v tm ra cc module bao gm thnh phn d liu v cc hm/th tc lin quan o Cch thc hin da vo vic gom nhm cc thnh phn tng t nhau v ngha, phm vi Thit k theo hng i tng o Tru tng ha d liu v cc hm/th tc lin quan o Chia h thng ra thnh cc lp/i tng o Mi lp/i tng c cc tnh nng v hnh ng chuyn bit o Cc lp c th c s dng to ra nhiu i tng c th

Tru tng ha

Trang 7

Phuc158@yahoo.com

i tng trong LTHT


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

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

catch (Exception ex) { Console.WriteLine("Loi! " + ex.Message); } } } }//End

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

Tnh k tha - Inheritance, Tnh a hnh - 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; }

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()); } } } }

Tnh tru tng - 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() + ")"; } }

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

Giao din 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(); } } } }

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

Das könnte Ihnen auch gefallen