Bài giảng Bổ trợ Một số điểm khác của C# so với C ++ - pdf 17

Download miễn phí Bài giảng Bổ trợ Một số điểm khác của C# so với C ++



System.IO
 Cung cấp nhiều lớp cho vào/ra
 Các lớp File và Directory sử dụng để quản lý thư mục và tệp
 Các thao tác bao gồm sao chép (copying),
di chuyển (moving), đổi tên (renaming) và
xóa (deleting) các tệp và thư mục



Để tải bản Đầy Đủ của tài liệu, xin Trả lời bài viết này, Mods sẽ gửi Link download cho bạn sớm nhất qua hòm tin nhắn.
Ai cần download tài liệu gì mà không tìm thấy ở đây, thì đăng yêu cầu down tại đây nhé:
Nhận download tài liệu miễn phí

Tóm tắt nội dung tài liệu:

n 1 / 18 of 45
Tất cả các kiểu dữ liệu trong C#
ñều kế thừa từ kiểu cơ sở object
using System;
class ObjectProff
{
public static void Main()
{
string objectVal;
objectVal = 7.ToString();
Console.WriteLine (“The value now is
”+objectVal);
}
}
10
C# Simplified / Session 1 / 19 of 45
Cấu trúc có cả cách
struct SINHVIEN
{
public string hoten;
public byte tuoi;
public void datTen(string ht)
{
hoten = ht;
}
}
C# Simplified / Session 1 / 20 of 45
Kiểu liệt kê (Enumerators)
public enum WeekDays
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday
}
Theo mặc ñịnh, các giá trị liệt kê lần lượt là 0, 1, 2, …
Ví dụ, với kiểu liệt kê trên, Monday = 0, Friday = 4
11
C# Simplified / Session 1 / 21 of 45
Kiểu liệt kê
 Có thể ñịnh lại giá trị cho các thành phần
liệt kê
public enum WeekDays
{
Monday=1,
Tuesday=2,
Wednesday=3,
Thursday=4,
Friday=5
}
C# Simplified / Session 1 / 22 of 45
Chồng cách
Hai cách chồng cách –
 Số lượng tham số khác nhau
 Kiểu của tham số khác nhau
12
C# Simplified / Session 1 / 23 of 45
Chồng cách – Số lượng
tham số khác nhau
using System;
public class Area
{
private int areaVal;
public void AreaCal(int radius)
{
areaVal = (22/7)* radius*radius;
}
public void AreaCal(int length, int breadth)
{
areaVal = length*breadth;
}
public void AreaCal(int length, int breadth, int
height)
{
areaVal = length*breadth*height;
}

}
C# Simplified / Session 1 / 24 of 45
Chồng cách – Tham số
khác kiểu
...
public void Add(int number1, int number2)
{
sum = number1 + number2;
}
public void Add(string value1, string value2)
{
int sum;
sum = Int32.Parse(value1) + Int32.Parse(value2);
Console.WriteLine ("Sum is {0}",sum);
Console.WriteLine ("Strings are converted to integers to add”);
}
...
13
C# Simplified / Session 1 / 25 of 45
Chồng toán tử
 Các toán tử sau ñược phép ñịnh nghĩa chồng
C# Simplified / Session 1 / 26 of 45
Chồng toán tử
using System;
public class Distance
{
int longitude, latitude;
public Distance()
{
longitude = 0;
latitude = 0;
}
public Distance(int longitude, int latitude)
{
this.longitude = longitude;
this.latitude = latitude;
}
public static Distance operator - (Distance first,
Distance second)
{
return new
Distance(first.longitude - second.longitude,
first.latitude - second.latitude);
}
//main
}
public static void Main()
{
Distance start = new Distance();
Distance objDistance = new Distance();
Distance finish = new Distance();
start.longitude = 12;
start.latitude = 10;
finish.longitude = 2;
finish.latitude = 1;
objDistance = start - finish;
Console.WriteLine ("The Finish is {0} Degrees East
and {1} Degrees North of the Start.",
objDistance.longitude,objDistance.latitude);
}
This statement does not
return an error as the ‘-’
operator is overloaded
14
C# Simplified / Session 1 / 27 of 45
Lớp bị bao bọc (Sealed Class)
Không cho lớp nào kế thừa từ nó
Sử dụng từ khóa ‘sealed’.

sealed class classOne
{
//Class Implementation
}

C# Simplified / Session 1 / 28 of 45
Lớp trừu tượng
 Lớp trừu tượng là lớp ñược sử dụng ñể kế thừa
và không thể bao bọc nó
 C# sử dụng từ khóa abstract trước ñịnh nghĩa
lớp ñể chỉ một lớp là lớp trừu tượng
 Một lớp trừu tượng có thể có cách
không trừu tượng
 Khi muốn viết lại một cách trừu tượng ở
lớp kế thừa, ta dùng từ khóa override
15
C# Simplified / Session 1 / 29 of 45
Lớp trừu tượng – Ví dụ
using System;
abstract class BaseClass
{
public abstract void MethodA();
public void MethodB()
{
Console.WriteLine ("This is the non abstract method”); }
}
class DerivedClass : BaseClass
{
public override void MethodA()
{
Console.WriteLine ("This is the abstract method
overriden in derived class");
}
}
C# Simplified / Session 1 / 30 of 45
class AbstractDemo
{
public static void Main()
{
DerivedClass objDerived = new
DerivedClass();
BaseClass objBase = objDerived;
objBase.MethodA();
objDerived.MethodB();
}
}
16
C# Simplified / Session 1 / 31 of 45
Giao diện (Interfaces)
 Giao diện là lớp trừu tượng chỉ chứa các
cách trừu tượng mà mỗi cách
chỉ có chữ ký, không có thân
 Lớp kế thừa một giao diện phải cài ătj tất cả
các cách của giao diện
public interface IFile
{
int delFile();
void disFile();
}
C# Simplified / Session 1 / 32 of 45
Giao diện – Ví dụ
public interface IFile
{
int delFile();
void disFile();
}
public class MyFile : IFile
{
public int delFile()
{
System.Console.WriteLine ("DelFile Implementation!");
return(0);
}
public void disFile()
{
System.Console.WriteLine ("DisFile Implementation!");
}
}
17
C# Simplified / Session 1 / 33 of 45
Giao diện - Output
class InterfaceDemo
{
public static void Main()
{
MyFile objMyFile = new MyFile();
objMyFile.disFile();
int retValue = objMyFile.delFile();
}
}
public class BaseforInterface
{
public void open()
{
System.Console.WriteLine ("This is the open method of BaseforInterface");
}
}
C# Simplified / Session 1 / 34 of 45
Giao diện – Kế thừa
public interface IFile
{
int delFile();
void disFile();
}
public class BaseforInterface
{
public void open()
{
System.Console.WriteLine ("This is the open method of
BaseforInterface");
}
}
18
C# Simplified / Session 1 / 35 of 45
Giao diện – Kế thừa
public class MyFile : BaseforInterface, IFile
{
public int delFile()
{
System.Console.WriteLine ("DelFile Implementation!");
return(0);
}
public void disFile()
{
System.Console.WriteLine ("DisFile Implementation!");
}
}
C# Simplified / Session 1 / 36 of 45
Giao diện – Output về kế thừa
class Test
{
static void Main()
{
MyFile objMyFile = new MyFile();
objMyFile.disFile();
int retValue = objMyFile.delFile();
objMyFile.open();
}
}
19
C# Simplified / Session 1 / 37 of 45
Kế thừa từ nhiều giao diện
public interface IFileTwo
{
void applySecondInterface();
}
C# Simplified / Session 1 / 38 of 45
Kế thừa nhiều giao diện
public class MyFile : BaseforInterface, IFile, IFileTwo
{
public int delFile()
{
System.Console.WriteLine ("DelFile Implementation!");
return(0);
}
public void disFile()
{
System.Console.WriteLine ("DisFile Implementation!");
}
public void applySecondInterface()
{
System.Console.WriteLine ("ApplySecondInterface
Implementation!");
}
}
20
C# Simplified / Session 1 / 39 of 45
Kế thừa nhiều giao diện - Output
class MultipleInterfaces
{
public static void Main()
{
MyFile objMyFile = new MyFile();
objMyFile.disFile();
int retValue = objMyFile.delFile();
objMyFile.open();
objMyFile.applySecondInterface();
}
}
C# Simplified / Session 1 / 40 of 45
Namespaces
 Một namespace (không gian tên) là một ñơn
vị logic của phần mềm bao gồm một hoặc
nhiều lớp
 Namespace ñược sử dụng ñể
 tổ chức mã nguồn
 tránh xung ñột tên
 giảm ñộ phức tạp khi sử dụng lại
21
C# Simplified / Session 1 / 41 of 45
Tổ chức mã
nguồn với
namespaces
NameSP1
NameSP2
NameSP11
NameSP12
Class1
Class2
Class1
Class3
Class2
Class1 Class2
Class1
Class2
Class3
C# Simplified / Session 1 / 42 of 45
Ví dụ khai báo Namespace
class SamsungTelevision
{
...
}
class SamsungWalkMan
{
...
}
class SonyTelevision
{
...
}
class SonyWalkMan
{
...
}
namespace Samsung
{
class Television
{...}
class WalkMan
{...}
}
namespace Sony
{
class Television
{...}
class Walkman
{...}
}
22
C# Simplified / Session 1 / 43 of 45
Namespaces lồng nhau
Khai báo namespace trong namespace khác
namespace Sony
{
namespace Television
{
class T14inches
{
...
}
class T21inches
{
...
}
}
}
...
namespace Sony.Television
{
class T14inches
{
...
}
class T21inches
{
...
}
}
...
C# Simplified / Session 1 / 44 of 45
Namespaces & Bổ ngữ truy
cập
 Namespaces mặc ñịnh là public
 Namespaces không thể là protected,
private hay internal ...
public namespace Sony //error
{
...
}
private namespace Samsung
//error
{
...
}
...
23
C# Simplified / Session 1 / 45 of 45
Namespace.classname
Tên ñầy ñủ
 ðể sử dụng một lớp trong cùng
namespace, chúng ta chỉ cần sử dụng
tên lớp (tên không ñầy ñủ)
 ðể sử dụng lớp ngoài namespa...
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status