Professional Documents
Culture Documents
Chapter 4
Chapter 4
K THA INHERITANCE
LCH THI
Thi thc hnh (20%):
class Employee
{
private string iD;
private string fullName;
private DateTime birthDate;
private int daysOfWork;
class Saler
{
private string iD;
private string fullName;
private DateTime birthDate;
private int daysOfWork;
private int productsOfSale;
M ngun phi vit li nhiu ln. Kh trong vic bo tr, nng cp sau ny
4
class Employee
{
private string iD;
private string fullName;
private DateTime birthDate;
private int daysOfWork;
private int productsOfSale;
private bool isSaler;
Lp c s 1
Lp c s 2
Lp dn xut
Lp dn xut
n tha k
a tha k
7
gm:
Cc thnh phn c khai bo lp dn xut
Cc thnh phn c khai bo lp c s (lp cha)
Lp dn xut khng c quyn xa i nhng thnh
phn c khai bo lp c s
lp dn xut
lp c s
Tn lp dn xut
<tn field>: <kiu d liu>
<tn property> {get;set;}: <tn kiu>
<tn method> (danh sch tham s): <tn kiu tr v>
<tn operator> (danh sch tham s): <tn kiu tr v>
10
V D MINH HA
Trong v d ny, n gin, chng ta
Saler
ProductsOfSale: int
Sale(): void
class Employee
{
public String ID;
public String Name;
public DateTime BirthDate;
public int DaysOfWork;
}
class Saler: Employee
{
public int ProductsOfSale;
}
class Program
{
static void Main(string[] args)
{
Saler a = new Saler();
a.Name = "Nguyen Van A";
a.ID = "12345600";
}
}
Lp Saler k tha
mi thnh phn ca lp
Employee
11
Trong lp con
Ngoi lp
public
+
c php
c php
c php
protected
#
c php
c php
khng c
private
-
c php
khng c
khng c
12
13
CY PHN CP K THA
Cu trc phn cp hnh cy, th hin mi quan h k
tha gia cc lp
Dn xut trc tip
14
CY PHN CP K THA
V d
15
Nu lp c s ch c mt constructor mc nh ( tc khng
nh ngha constructor hay ch c mt constructor khng tham s)
16
17
18
19
Hm main()
20
Mc nh, hm khi to mc
nh ca lp c s s c gi
Do , nu lp c s khng c
hm khi to mc nh, s b bo
li
class A
{
public A()
{Console.WriteLine("Base construction");}
}
class B : A
{
public int Field;
public B(int x)
{ Field = x;
Console.WriteLine("Derived construction");
}
}
class Program
{
static void Main()
{ B b = new B(1); }
}
21
TY CHN GI HM KHI TO CA LP C S
C th quyt nh gi hm khi to no ca lp c s qu
trnh khi to ca lp dn xut
class Employee
{ public String ID;
public String Name;
public DateTime BirthDate;
public int DaysOfWork;
public Employee(String id, String name, String birthdate)
{ ID = id; Name = name;
BirthDate = Convert.ToDateTime(birthdate);
}
}
class Saler: Employee
{
public int ProductsOfSale;
public Saler(String id, String name, String birthdate, int pSale)
: base(id, name, birthdate)
{
ProductsOfSale= pSale;
}
}
22
n dng nh sau
23
24
class Program
{
static void Main()
{
OtherClass oc = new
OtherClass();
oc.Method1(oc.Field1);
}
}
25
T KHA BASE
kha base.
Chng hn nh ta mun truy cp vo thnh phn X
ca lp c s th v vit base.X
S dng nhiu trong trng hp phng thc lp
con override nh vn mun gi li phng thc lp
cha. ( s ni thm trong chng 5 a hnh)
OOP _ CHAPTER 4 - K THA
26
T KHA BASE
T kha base cho php truy xut thnh phn ca lp c s
Lu , khng th dng t kha base truy xut thnh phn private
ca lp c s
class SomeClass // Base class
{
public string Field1 = "Field1 -- In the base class";
}
class OtherClass : SomeClass // Derived class
{ new public string Field1 = "Field1 -- In the derived class";
public void PrintField1()
{ Console.WriteLine(Field1); // Access the derived class.
Console.WriteLine(base.Field1); // Access the base class.
}
}
class Program
{
static void Main()
{ OtherClass oc = new OtherClass();
oc.PrintField1();
}
}
27
28
K THA V CON TR
Con tr (bin) thuc kiu lp c s th ch c th truy cp n
29
30