You are on page 1of 50

Xy dng lp v giao din

Ni dung
Khai bo lp
Constructor & destructor
Hm thnh vin
Thuc tnh
a hnh trong C#
Down cast up cast
Abstract class
Sealed class, nested class
Interface
2
To lp trong C#
Khai bo lp




Access modifier:
public, protected, internal, protected internal, private
Nu ko khai bo lp c s th C# mc nh
xem lp c s l object
Lp lun l kiu d liu tham chiu trong C#
3
[access modifier] class <class name> [: base class]
{
// class body
}
Kha truy xut cho class
Mt class cha trong namespace ch c 2 kha truy xut
Public: cho php bn ngoi assembly truy xut
Internal: ch cho php s dng bn trong assembly
Assembly l tp m c bin dch sang .NET
Mt assembly cha ni dung thc thi chng trnh hay th
vin ng
Assembly c th cha trong nhiu file
4
Cc thnh phn ca class
Lp c th cha cc phn sau
Constructor v destructor
Field v constant
Method
Property
Indexer
Event
Cha cc kiu khc (nested): class, struct, enumeration,
interface v delegate
5
To i tng
Khai bo
Trong thn lp
Ging nh thuc tnh
Trong thn phng thc
Tng t nh bin
Khi to
Bng lnh new
6
HocSinh hs ;
Tn lp
Tn i tng
hs = new HocSinh();
<HocSinh> hs
hs
To i tng
Constructor
c gi t ng khi to i tng
Cng tn vi lp
Constructor ko tham s s c to mc nh khi khng
c bt c constructor no
Cho php overload constructor to ra nhiu cch khi
to i tng
Static constructor: ko tham s, ko access modifier,


7
Constructor
Constructor mc nh
Khng c tham s
Khi to th hin (i tng) khi cha bit thng tin g v n
Constructor sao chp
Tham s vo l i tng cng lp
To ra obj nh bn sao ca obj u vo
Constructor khc
C mt hay nhiu tham s vo
To obj khi bit mt s thng tin no v n
8
Constructor
9
class HocSinh
{
//...
public HocSinh()
{
hoTen = unknown";
namSinh = 1990;
diemVan = diemToan = 0;
}
public HocSinh(HocSinh hs)
{
hoTen = hs.hoTen;
namSinh = hs.namSinh;
diemVan = hs.diemVan;
diemToan = hs.diemToan;
}
public HocSinh(string ht)
{
hoTen = ht;
}
}
Constructor mc nh
Constructor sao chp
Constructor khc
(to hc sinh khi bit h tn)
Constructor
Khai bo private cho constructor s ko cho php to i
tng
10
Ko th to
th hin/obj
Destructor
Thc hin nhim v clean khi i tng b hy
Trng tn lp v c du ~ pha trc
Khng c tham s v access modifier
Mi lp ch c 1 destructor
11
class HocSinh
{
//...
~HocSinh()
{
siSo--;
}
}
Method
Hm, th tc khai bo trong class
Hnh vi giao tip vi bn ngoi
Static v non static
12
public class CSharp
{
public CSharp ( ) { . . .}
public static void StaticMethod( ) { . . .}
public void NonStaticMethod( ) { . . .}
}
public class Tester() {
CSharp cs = new CSharp( );
cs.NonStaticMethod( );
CSharp.StaticMethod( );
}
Truy cp qua th hin: cs
Truy cp qua tn lp: CSharp
Method
13
namespace QuanLyHocSinh
{
class HocSinh
{
//...






}
}
static public bool KiemTraDiem( double diem )
{


}
bool kq = (0<= diem && diem <= diemToiDa);
return kq;
Phn khai bo
Phn nh ngha
Cc cu lnh
Kiu tr v Tn
i s
Cu lnh tr kt qu ra ngoi
Method
14
namespace QuanLyHocSinh
{
class HocSinh
{
//...








}
}
public void Xuat( )
{




}
Console.WriteLine("Ho ten : "+hoTen);
Console.WriteLine("Nam sinh : "+namSinh);
Console.WriteLine("Diem van : "+diemVan);
Console.WriteLine("Diem toan: "+diemToan);
Cc cu lnh
Kiu tr v
Khng c
i s
Method - overload

15
static void ThongBao( double d )
{
Console.WriteLine("Day la ThongBao(double)");
}
static void ThongBao( int i )
{
Console.WriteLine("Day la ThongBao(int)");
}
static void ThongBao( int i1, int i2 )
{
Console.WriteLine("Day la ThongBao(int, int)");
}
static void ThongBao( HocSinh hs )
{
Console.WriteLine("Day la ThongBao(HocSinh)");
}
Cc phng thc cng c tn l ThongBao
Cc phng c tham s u vo khc nhau
Method - overload

16
ThongBao(40);
ThongBao(6.8);
ThongBao(new HocSinh());
ThongBao(9,5);
Day la ThongBao(int)
Day la ThongBao(double)
Day la ThongBao(HocSinh)
Day la ThongBao(int, int)
Method virtual method

17
class LopCon:LopCha
{

}
Tn lp con Tn lp cha
class HocSinhVan: HocSinh
{
//...
}
To ra lp HocSinhVan (hc sinh chuyn
vn) k tha t lp HocSinh
LopCha
LopCon
HocSinh
HocSinhVan
Method virtual method
Phng thc o:
Khai bo t kho virtual
Cho php lp con c th thay th (override)
y chnh l thc thi tnh a hnh
Mt phng thc ca lp c s (lp cha) c th c thc
thi khc nhau lp dn xut (lp con)
18
Method virtual method
Phng thc tnh im trung bnh ca lp HocSinh
19
class HocSinh
{
//...
public virtual float TinhDiemTrungBinh()
{
float kq = (diemVan + diemToan) / 2;
return kq;
}
}
Method virtual method
Lp HocSinhVan ph quyt li cch tnh im trung bnh
ca lp HocSinh
20
class HocSinhVan:HocSinh
{
//...
public override double TinhDiemTrungBinh()
{
double kq = (diemVan * 2 + diemToan) / 3;
return kq;
}
}
Method virtual method

21
HocSinh hs1 = new HocSinh(6,7);
float d1 = hs1.TinhDiemTrungBinh();
HocSinhVan hs2 = new HocSinhVan(8,9);
float d2 = hs2.TinhDiemTrungBinh();
hs1 = hs2;
float d3 = hs1.TinhDiemTrungBinh();
VD Polymorphism

22
Hm mi cng tn Draw vi
hm Draw ca lp c s
Ph quyt hm
Draw ca Shape
Polymorphism

23
Shape s1 = new Shape();
s1.Draw();
Shape s2 = new Line();
s2.Draw();
Shape s3 = new Circle();
s3.Draw();
Circle c = (Circle)s3;
c.Draw();
Shape draw!
Line draw!
Shape draw!
Circle draw!
Property
Getter/Setter l cc phng thc:
Getter: Cho php i tng cung cp
gi tr ca thuc tnh ra bn ngoi
Setter: Cho php bn ngoi thay i
gi tr ca thuc tnh ca i tng
mt cch c kim sot
Property:
c b sung vo C# thay th
cch dng getter/setter truyn thng
24
Property
t vn
Lp HocSinh c thuc tnh diemVan
(im vn)
Gi tr ca diemVan phi t 0 ti 10
Bn ngoi c th thy v i gi tr
ca diemVan
Ch cho php a gi tr mi
(diemMoi) vo diemVan nu gi tr
mi l hp l (t 0 ti 10)
25
Property
Dng getter/setter
26
class HocSinh
{
protected double diemVan=0;
//
public double GetDiemVan()
{
return diemVan;
}
public void SetDiemVan( double diemMoi)
{
if (0<=diemMoi&&diemMoi<=10)
diemVan=diemMoi;
}
}
Property
Ci t bng property
27
class HocSinh
{
protected double diemVan=0;
//
public double DiemVan {
get {
return diemVan;
}
set {
if (0 <= value && value <= 10)
diemVan = value;
}
}
}
Property
S dng property
28
class Tester
{
HocSinh hs1 = new HocSinh(Nguyen Ha My Tien);
hs1.DiemVan = 5;
HocSinh hs2 = new HocSinh(Nguyen Ha Thanh Tung);
hs2.DiemVan = hs1.DiemVan;
}
set get
Indexer
Cho php truy cp Mng/Tp
hp ca i tng thng qua
tn i tng v ch mc.
to Indexer:
To Property vi tn this
Khai bo kiu d liu ca ch
mc
S dng Indexer:
Dng k hiu mng c/ghi
gi tr

29
V d
30

V d
31

Multiple Indexer
32

Up-cast v down-cast
Up-cast
p kiu t handle con ln handle cha
Lun thnh cng
Thc hin t ng (implicit)
Down-cast
p kiu t handle cha xung handle con
Ty trng hp
Phi ch nh r (explicit)
33
V d up-cast
34
HocSinh hs = new HocSinhVan();
HocSinhVan hsv = new HocSinhVan();
Object o = hsv;
HocSinhVan l lp con (gin tip) ca Object
HocSinhVan l lp con ca HocSinh
HinhHoc hh = new HinhVuong();
HinhVuong l lp con ca HinhChuNhat
HinhChuNhat l lp con ca HinhHoc
HocSinh
HocSinhVan
HinhHoc
HinhChuNhat
HinhVuong
V d down-cast
35
((HinhVuong)hh).Width = 8;
((HinhChuNhat)o).Width = 8;
c v handle hh ang gi mt i tng HinhVuong
Khng c v handle o ang gi mt i tng HocSinhVan,
m lp HocSinhVan v lp HinhChuNhat khng c quan h cha-con
cn c user-defined cast
((HinhChuNhat)hh).Width = 8;
c v handle hh ang gi mt i tng HinhVuong,
m lp HinhChuNhat l cha ca lp HinhVuong
Kim tra trc khi down-cast
m bo down-cast thnh cng, cn kim tra xem
handle c phi ang gi i tng ph hp hay khng
T kha: is
36
if (o is HinhChuNhat)
((HinhChuNhat)o).Width = 8;
else
Console.WriteLine("o khong la HinhChuNhat");
Abstract class
Lp tru tng ko cho php to th hin ca lp
S dng polymorphism i hi
khai bo cc phng thc l virtual hay abstract trong lp
c s tru tng
Override chng trong lp dn xut (lp con)
Bt c lp no c mt phng thc tru tng th phi
khai bo lp l lp tru tng
37
Abstract class
Abstract Method Virtual Method
t kho: abstract t kho: virtual
Ch c phn khai bo
method v kt thc l
du ;, Khng cn c
phn thc thi cho
phng thc abstract
lp abstract
C phn thc thi cho
phng thc virtual lp
c s
Bt buc lp dn xut
phi override li
Khng bt buc lp dn
xut phi override
t kho override trc
phng thc lp con
t kho override trc
phng thc lp con
38
Abstract class - example
39
Abstract class:

public abstract class AbstractClass
{
public AbstractClass()
{
}
public abstract int AbstractMethod() ;
public virtual int VirtualMethod()
{
return 0;
}
}

Abstract class - example
40
Derived class:

public class DerivedClass : AbstractClass
{
public DerivedClass()
{ }
public override int AbstractMethod()
{
return 0;
}
public override int VirtualMethod()
{
return base.VirtualMethod ();
}
}
Bt buc phi c
Sealed Class
Cn gi l lp nim phong, khng cho php
lp khc k tha n, ngc vi lp abstract.
41
using System;
sealed class MyClass
{
public int x;
public int y;
}
class MainClass {
public static void Main()
{
MyClass mC = new MyClass();
mC.x = 110; mC.y = 150;
Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y);
}
}
Sealed method
42
using System;
class MyClass1 {
public int x; public int y;
public virtual void Method() {
Console.WriteLine("virtual method");
}
}
class MyClass : MyClass1 {
public override sealed void Method() {
Console.WriteLine("sealed method");
}
}
class MainClass {
public static void Main() {
MyClass1 mC = new MyClass();
mC.x = 110; mC.y = 150;
Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y);
mC.Method();
}
}
S dng sealed trc
phng thc ngn ko
cho lp dn xut override
Nested class
Lp c khai bo bn trong thn ca lp khc gi l:
inner class hay nested class, lp kia gi l outer class
Lp ni c th truy cp tt c thnh vin ca lp ngoi,
k c private
Lp ni nm trong lp ngoi nn n c th l private vi
lp ngoi
Khi lp ni khai bo l public th c th truy xut thng
qua tn ca lp ngoi: outer_class.inner_class
43
Nested class
44
public class Fraction
{
private int numerator;
private int denominator;
public Fraction(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}
public override string ToString() {
string str = numerator.ToString() + "/" + denominator.ToString();
return s;
}
public class FractionArtist
{
public void Draw(Fraction f) {
Console.WriteLine("Drawing the numerator {0}", f.numerator);
Console.WriteLine("Drawing the denominator {0}", f.denominator);
}
}
}
Lp nested class
Truy xut c thnh phn
Private ca lp outer
Nested class
45
class Tester
{
static void Main()
{
Fraction f1 = new Fraction(3, 4);
Console.WriteLine("f1: {0}", f1.ToString());
Fraction.FractionArtist fa = new Fraction.FractionArtist();
fa.Draw(f1);
}
}
Truy xut lp inner qua lp
outer, lp outer tng t
nh manespace
Interface
Interface quy nh cc chc nng nhng khng m t c
th chng
Phng thc trong interface
Ch khai bo, khng nh ngha
Khng c t kha phm vi, lun l public
Interface khng th cha thuc tnh
T kha: interface
46
C th xem interface nh mt bn hp ng, nu lp no
s dng (k tha) n th phi thc thi y cc m t
(phng thc) trong hp ng (interface)
Interface
Mt interface c th k tha t nhiu interface
khc
Mt lp c th k tha t nhiu interface
Phi nh ngha tt c phng thc m cc interface "cha"
quy nh
S k tha interface phi t sau s k tha
lp
Cc interface thng c t tn vi tin t
l I
IFile, IComparable, IDisposable, IStorable, ICloneable
47
[access modifier] interface <interface name> [: base interface list]
Interface - example
48
public interface IStudent
{
int StudentID
{
get;
set;
}
void AddSubject(string subjectName);
}

Khai bo property
StudentID gm hm get ,set
Khai bo phng thc
AddSubject
Phi nh ngha {get,set}
ca StudentID v
AddSubject lp thc thi
interface
Interface - example
49
public class Student : IStudent
{
private int studentID = 0;
private ArrayList subjects = null;
public Student() {}
public int StudentID
{
get { return studentID; }
set { studentID = value; }
}
public void AddSubject(string subjectName)
{
subjects.Add(subjectName);
}
}
Bt buc lp student
phi nh ngha property
Student v hm
AddSubject
50

You might also like