Ngôn ngữ lập trình c# full pdf

18 846 1
Ngôn ngữ lập trình c# full pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Ngôn ngữ lập trình c# full pdf

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# Chương 3: Nền tảng ngôn ngữ C# Kiểu liệu Biến Câu lệnh Namespace .6 Chương 4: Lập trình hướng đối tượng Mục tiêu việc thiết kế phần mềm Quá trình thiết kế phần mềm Các cách tiếp cận thiết kế .7 Trừu tượng hóa Đối tượng LTHĐT .8 Tạo sử dụng đối tượng Tính đóng gói – Encapsulation 10 Tính kế thừa - Inheritance, Tính đa hình - Polymorphism 11 Tính trừu tượng - abstract classes 13 Giao diện – Interfaces 16 Delegate .17 Event: 18 Trang Phuc158@yahoo.com Chương 1: Microsoft NET Microsoft NET gồm phần : Framework Integrated Development Environment (IDE) o Framework cung cấp cần thiết bản, chữ Framework có nghĩa khung hay khung cảnh ta dùng hạ tầng sở theo qui ước định để công việc trôi chảy o IDE cung cấp mơi trường giúp triển khai dễ dàng, nhanh chóng ứng dụng dựa tảng NET Nếu khơng có IDE dùng trình soạn thảo ví Notepad hay trình soạn thảo văn sử dụng command line để biên dịch thực thi, nhiên việc nhiều thời gian Trang Phuc158@yahoo.com Chương 2: Giới thiệu ngôn ngữ C# C# ngôn ngữ đơn giản: C# loại bỏ vài phức tạp rối rắm ngôn ngữ Java c++, bao gồm việc loại bỏ macro, template, đa kế thừa, lớp sở ảo (virtual base class) Chúng nguyên nhân gây nhầm lẫn hay dẫn đến vấn đề cho người phát triển C# ngôn ngữ đại: Điều làm cho ngơn ngữ đại? Những đặc tính xử lý ngoại lệ, thu gom nhớ tự động, kiểu liệu mở rộng, bảo mật mã nguồn đặc tính mong đợi ngôn ngữ đại C# chứa tất đặc tính C# ngơn ngữ hướng đối tượng: Những đặc điểm ngơn ngữ hướng đối tượng (Object-oriented language) đóng gói (encapsulation), kế thừa (inheritance), đa hình (polymorphism) C# hỗ trợ tất đặc tính C# ngôn ngữ mạnh mẽ mềm dẻo: C# sử dụng cho nhiều dự án khác tạo ứng dụng xử lý văn bản, ứng dụng đồ họa, tính … C# ngơn ngữ từ khóa: C# ngơn ngữ hướng module: Mã nguồn C# viết phần gọi lớp, lớp chứa phương thức thành viên Những lớp phương thức sử dụng lại ứng dụng hay chương trình khác Trang Phuc158@yahoo.com Chương 3: Nền tảng ngôn ngữ C# Kiểu liệu C# ngơn ngữ lập trình mạnh kiểu liệu, ngôn ngữ mạnh kiểu liệu phải khai báo kiểu đối tượng tạo (kiểu số nguyên, số thực, kiểu chuỗi, kiểu điều khiển ) trình biên dịch giúp cho người lập trình khơng bị lỗi cho phép loại kiểu liệu gán cho kiểu liệu khác Kiểu liệu đối tượng tín hiệu để trình biên dịch nhận biết kích thước khả đối tượng C# phân tập hợp kiểu liệu thành hai loại: Kiểu liệu giá trị (value) kiểu liệu tham chiếu (reference) Đối với kiểu liệu giá trị lưu giữ kích thước thật nhớ cấp phát stack Trong địa kiểu liệu tham chiếu lưu stack đối tượng thật lưu nhớ heap Ghi chú: Tất kiểu liệu xây dựng sẵn kiểu liệu giá trị ngoại trừ đối tượng chuỗi Và tất kiểu người dùng định nghĩa ngoại trừ kiểu cấu trúc kiểu liệu tham chiếu Trang Phuc158@yahoo.com Biến Gán giá trị xác định cho biến: = ; Hằng: const = ; Kiểu liệt kê: class ViDuKieuLietKe { // Khai báo kiểu liệt kê enum NhietDoNuoc { DoDong = 0, DoNguoi = 20, DoAm = 40, DoNong = 60, DoSoi = 100, } static void Main() { System.Console.WriteLine( “Nhiet dong: {0}”, NhietDoNuoc.DoDong); System.Console.WriteLine( “Nhiet nguoi: {0}”, NhietDoNuoc.DoNguoi); System.Console.WriteLine( “Nhiet am: {0}”, NhietDoNuoc.DoAm); System.Console.WriteLine( “Nhiet nong: {0}”, NhietDoNuoc.DoNong); System.Console.WriteLine( “Nhiet soi: {0}”, NhietDoNuoc.DoSoi); } } Câu lệnh Phân nhánh khơng có điều kiện: gọi từ khóa sau: goto, break, continue, return, statementthrow Phân nhánh có điều kiện: if (biểu thức điều kiện) { } else { } Trang Phuc158@yahoo.com switch (biểu thức điều kiện) { case : [default: ] } Câu lệnh lặp: while (Biểu thức) while ( điều kiện ) for ([ phần khởi tạo] ; [biểu thức điều kiện]; [bước lặp]) foreach ( in < tên tập hợp>) Namespace Giả sử có người nói Phúc kỹ sư, từ kỹ sư phải kèm với lĩnh vực định đó, khơng khơng biết là kỹ sư cầu đường, khí hay phần mềm Khi lập trình viên C# bảo Phúc CauDuong.KySu phân biệt với CoKhi.KySu hay PhanMem.KySu Namespace trường hợp CauDuong, CoKhi, PhanMem hạn chế phạm vi từ theo sau Nó tạo vùng khơng gian để tên sau có nghĩa Tương tự ta tạo namespace để phân thành vùng cho lớp trùng tên không tranh chấp với C# đưa từ khóa using đề khai báo sử dụng namespace chương trình: using < Tên namespace > Để tạo namespace dùng cú pháp sau: namespace { < Định nghĩa lớp A> < Định nghĩa lớp B > } Trang Phuc158@yahoo.com Chương 4: Lập trình hướng đối tượng Mục tiêu việc thiết kế phần mềm: · · · Tính tái sử dụng (reusability): thiết kế thành phần sử dụng nhiều phần mềm khác Tính mở rộng (extensibility): hỗ trợ plug-ins Tính mềm dẻo (flexibility): o Có thể dễ dàng thay đổi thêm liệu hay tính o Các thay đổi không làm ảnh hưởng nhiều đến tồn hệ thống Q trình thiết kế phần mềm: · · · · Quá trình thiết kế: chia phần mềm thiết kế theo phần, component Trừu tượng hóa: cần thiết thiết kế Cụ thể: bỏ qua chi tiết component, quan tâm thành phần mức trừu tượng Xác định component: top-down Tích hợp: bottom-up Gắn kết components nhỏ lại với Các cách tiếp cận thiết kế · · · Thiết kế theo hàm/thủ tục: o Tìm hàm/thủ tục để hoàn tất yêu cầu o Kết hệ thống cấu trúc mối quan hệ hàm/thủ tục Thiết kế theo module: o Phân tích tìm module bao gồm thành phần liệu hàm/thủ tục liên quan o Cách thực dựa vào việc gom nhóm thành phần tương tự ý nghĩa, phạm vi… Thiết kế theo hướng đối tượng o Trừu tượng hóa liệu hàm/thủ tục liên quan o Chia hệ thống thành lớp/đối tượng o Mỗi lớp/đối tượng có tính hành động chun biệt o Các lớp sử dụng để tạo nhiều đối tượng cụ thể Trừu tượng hóa Trang Phuc158@yahoo.com Đối tượng LTHĐT · · · Đối tượng thực thể phần mềm bao bọc thuộc tính mơ tả trạng thái phương thức liên quan Kiểu liệu lớp đối tượng: class Class có tính chất sau: o Đóng gói: chứa đựng liệu hàm/thủ tục liên quan o Che giấu liệu: thực thể phần mềm khác không can thiệp trực tiếp vào liệu bên mà phải thông qua phương thức cho phép Tạo sử dụng đối tượng: Ví dụ: Lớp Phân 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 Phuc158@yahoo.com public PhanSo(int Tu, int Mau) { _TuSo = Tu; //Neu mau so = thi nem loi 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 truy cap phuong thuc static int SoLuongPhanSo = PhanSo.XuatSoLuongPhanSo(); Console.WriteLine("So luong phan so da tao: {0}", SoLuongPhanSo); //Tinh tong phan so, su dung operator PhanSo Tong = PhanSo1 + PhanSo2; Console.WriteLine(PhanSo1.Xuat() + " + " + PhanSo2.Xuat() + "=" + Tong.Xuat()); } Trang Phuc158@yahoo.com 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 nội class A o Từ (2)(5)(8) => Protected nội class A class kế thừa o Từ (3)(6)(9) => Public truy xuất nơi đâu o Internal truy xuất phạm vi file DLL o Protected Internal truy xuất phạm vi file DLL lớp kế thừa Trang 10 Phuc158@yahoo.com 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; } 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()); } } } } 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() + ")"; } } 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 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(); } } } } 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()); //Kết luận: //Khái niệm delegate tương tự trỏ hàm //Ưu điểm Delegate tính khả biến //Có thể gọi phương thức có đối số int trả trị int } } } Trang 17 Phuc158@yahoo.com 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); } } } Trang 18 Phuc158@yahoo.com ... thức sử dụng lại ứng dụng hay chương trình khác Trang Phuc158@yahoo.com Chương 3: Nền tảng ngôn ngữ C# Kiểu liệu C# ngơn ngữ lập trình mạnh kiểu liệu, ngôn ngữ mạnh kiểu liệu phải khai báo kiểu... triển C# ngôn ngữ đại: Điều làm cho ngơn ngữ đại? Những đặc tính xử lý ngoại lệ, thu gom nhớ tự động, kiểu liệu mở rộng, bảo mật mã nguồn đặc tính mong đợi ngôn ngữ đại C# chứa tất đặc tính C# ngơn... dùng trình soạn thảo ví Notepad hay trình soạn thảo văn sử dụng command line để biên dịch thực thi, nhiên việc nhiều thời gian Trang Phuc158@yahoo.com Chương 2: Giới thiệu ngôn ngữ C# C# ngôn ngữ

Ngày đăng: 05/10/2013, 17:30

Hình ảnh liên quan

Tính kế thừa - Inheritance, Tính đa hình - Polymorphism - Ngôn ngữ lập trình c# full pdf

nh.

kế thừa - Inheritance, Tính đa hình - Polymorphism Xem tại trang 11 của tài liệu.

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan