Professional Documents
Culture Documents
C IM C BN V
LP TRNH HNG I TNG
TRONG JAVA
Cc khi nim c bn
i tng (object): trong th gii thc khi nim i
Cc khi nim c bn
Lp (class): l khun mu (template) sinh ra i tng.
lp l 1 th hin c th ca 1 lp .
Khai bo lp
class <ClassName>
{
<danh sch thuc tnh>
<cc khi to>
<danh sch cc phngthc>
}
tng.
class <ClassName>
{
}
}
class Sinhvien {
// Danh sch thuc tnh
String maSv, tenSv, dcLienlac;
int
tuoi;
tng.
Cng tn vi lp.
Khng c gi tr tr v.
T ng thi hnh khi to ra i tng (new)
C th c tham s hoc khng.
Lu : Mi lp s c 1 constructor mc nh (nu ta khng
class Sinhvien
{
// Dng constructor mc nh
Sinhvien sv = new Sinhvien();
V d 2:
class Sinhvien
class Sinhvien
// khng c constructor mc nh
// khai bo constructor mc nh
Sinhvien(<cc i s>) {}
Sinhvien(){}
Sinhvien(<cc i s>) {}
// li bin dch
class Sinhvien {
}
public void xemThongTinSV(String psMaSv) {
class Sinhvien
String
int
tuoi;
}
}
Nested Classes
Khai bo v to i tng
V d Nested Class
V D Nested Class
Scope
Inner
no
inner [non-static]
class
member
yes
local class
local
yes
anonymous class
yes
Tnh ng gi
ng gi: nhm nhng g c lin quan vi nhau vo
Tnh ng gi
ng gi: dng che du mt phn hoc tt c thng
Tnh k tha
i tng hnh hc
Hnh 2d
Trn
E-lp
Vung Ch nht
Hnh 3d
Cu
Lp phng
mi
Thuc tnh: thm mi
Phng thc: thm mi hay hiu chnh
Tr
Tnh k tha
Lp dn xut hay lp con (SubClass)
Lp c s hay lp cha (SuperClass)
Lp con c th k tha tt c hay mt phn cc thnh phn
V d: class nguoi {
}
class sinhvien extends nguoi {
}
Lu : default khng phi l 1 t kha
Tnh k tha
Overriding Method
- c nh ngha trong lp con
- C tn, kiu tr v & cc i s ging vi phng thc ca
lp cha
- C kiu, phm vi truy cp k0 nh hn phng thc trong
lp cha
private
default
protected
public
Tnh k tha
V d: class Hinhhoc {
}
class HinhVuong extends Hinhhoc {
private int canh;
public float tinhdientich() {
return canh*canh;
}
Tnh k tha
class HinhChuNhat extends HinhVuong {
private int cd;
private int cr;
public float tinhdientich() {
return cd*cr;
}
Lp Object
Cy k tha trong Java ch c 1 gc.
Mi lp u k tha trc tip hoc gin tip t lp Object.
Phng thc ca lp Object
clone
equals
finalize
getClass
hashCode
notify, notifyAll, wait
toString
T kha super
Gi constructor ca lp cha
Nu gi tng minh th phi l cu lnh u tin.
Constructor cui cng c gi l ca lp Object
Truy cp n thuc tnh b che lp cha.
33
CommissionE
mployee4.jav
a
Constructor outputs
message to demonstrate
method call order.
(1 of 4)
Lines 23-24
34
CommissionE
mployee4.java
(2 of 4)
35
CommissionE
mployee4.java
(3 of 4)
36
CommissionE
mployee4.java
(4 of 4)
37
BasePlusCom
missionEmplo
yee5.java
Constructor outputs
message to demonstrate
method call order.
(1 of 2)
Lines 15-16
38
BasePlusCommis
sionEmployee5.ja
va
(2 of 2)
39
Instantiate
CommissionEmploy
Construct
ee4 object
orTest
.java
(1 of 2)
Instantiate
two
Lines 8-9
BasePlusCommissionE
Lines 12-19
mployee5 objects to
demonstrate order of subclass
and superclass constructor
method calls.
40
ConstructorTest
.java
(2 of 2)
Subclass
BasePlusCommissionEm
ployee5 constructor body
executes after superclass
CommissionEmployee4s
constructor finishes execution.
Tnh k tha
Lp final: l lp khng cho php cc lp khc dn xut
Tnh a hnh
a hnh: cng mt phng thc c th c nhng cch thi hnh khc
nhau.
Interface: c ci t bi cc lp con trin khai mt phng thc
m lp mun c.
Tnh a hnh
Lp tru tng: l lp dng th hin s tru tng ha mc cao.
// tr v tn ca i tng shape
public String getName() {
return "Point";
}
} // end class Point
V d
51
52
Declare interface
Payable
Outline
Payable.java
Declare getPaymentAmount
method which is implicitly public
and abstract
53
Class Invoice
implements
interface Payable
Outline
Invoice.java
(1 of 3)
54
Outline
Invoice.java
(2 of 3)
55
Outline
Invoice.java
(3 of 3)
Declare getPaymentAmount
to fulfill contract with
interface Payable
56
Outline
Class Employee
implements
interface Payable
Employee.java
(1 of 3)
57
Outline
Employee.java
(2 of 3)
58
Outline
Employee.java
getPaymentAmount
method is not
implemented here
(3 of 3)
59
Outline
(1 of 2)
60
Outline
SalariedEmployee
.java
Declare getPaymentAmount
method instead of earnings
method
(2 of 2)
61
Outline
PayableInterface
Test.java
(1 of 2)
Assigning
references to
Invoice
objects to
Payable
variables
Assigning references to
SalariedEmployee
objects to Payable
variables
62
Outline
PayableInterface
Test.java
(2 of 2)
Interface
Class
Interface
extends
implements
extends