You are on page 1of 30

K tha

Ni dung
Vn s dng li
S dng li bng k tha
K tha trong Java

nh

ngha lp k tha
thm phng thc, thuc tnh
kim sot truy cp
constructor

Lp Object

Nguyn Vit H

K tha

Ti liu tham kho


Thinking in Java, chapter 6
Java how to program, chapter 9

Nguyn Vit H

K tha

S dng li

Tn ti nhiu loi i tng c cc thuc


tnh v hnh vi tng t hoc lin quan
n nhau
Person,

Student, Manager,

Xut hin nhu cu s dng li cc m


ngun vit
S

dng li thng qua copy


S dng li thng qua quan h has_a
S dng li thng qua c ch k tha
Nguyn Vit H

K tha

S dng li

Copy m ngun
Tn

cng, d nhm
Kh sa li do tn ti nhiu phin bn

Quan h has_a
S

dng lp c nh l thnh phn ca lp


mi
S dng li ci t vi giao din mi
Phi vit li giao din
Cha mm do

Nguyn Vit H

K tha

V d: has_a
class Person {
private String name;
private Date bithday;
public String getName() { return name; }
...
}
class Employee {
private Person me;
private double salary;
public String getName() { return me.getName(); }
...
}
Nguyn Vit H

K tha

class Manager {
private Employee me;
private Employee assistant;
public setAssistant(Employee e) {...}
...
}
...
Manager junior = new Manager();
Manager senior = new Manager();
senior.setAssistant(junior); // error

Nguyn Vit H

K tha

K tha
Da trn quan h is_a
Tha hng li cc thuc tnh v phng
thc c
Chi tit ha cho ph hp vi mc ch s
dng mi

Thm

cc thuc tnh mi
Thm hoc hiu chnh cc phng thc

Nguyn Vit H

K tha

Thut ng
K tha
Lp c s, lp cha
Lp dn xut, lp con

Nguyn Vit H

K tha

K tha trong Java


[public] class DerivedClass extends BaseClass {
/* new features goes here */
}
V d:
class Employee extends Person {
private double salary;
public boolean setSalary(double sal) {
...
salary = sal;
return true;
}
}

Nguyn Vit H

K tha

10

Person
-name
-birthday
+setName()
+setBirthday()

Employee e = new Employee();


e.setName("John");
e.setSalary(3.0);

Employee
-salary
+setSalary()
+getDetail()

Nguyn Vit H

K tha

11

private members
class Employee extends Person {
...
public String getDetail() {
String s;
// s = name + "," + birthday;
s = getName() + "," + getBirthday();
s += "," + salary;
return s;
}
}

Nguyn Vit H

K tha

12

Mc truy cp protected

m bo che du thng tin, thng


thng cc thuc tnh c khai bo l
private
i

tng thuc lp dn xut phi truy cp ti


chng thng qua cc phng thc get v set.

Mc truy cp protected gii quyt vn


ny
i

tng ca lp dn xut truy cp c


cc protected members ca lp c s
Cc i tng khc khng truy cp c
Nguyn Vit H

K tha

13

public class Person {


protected Date birthday;
protected String name;
...
}
public class Employee extends Person {
...
public String getDetail() {
String s;
s = name + "," + birthday;
s += "," + salary;
return s;
}
}

Nguyn Vit H

K tha

14

Cc mc kim sot truy cp


Modifier

Same
class

Same
package

Subclass

private

Yes

package
(default)

Yes

Yes

protected

Yes

Yes

Yes

public

Yes

Yes

Yes

Nguyn Vit H

K tha

Universe

Yes

15

Trong cng gi

public class Person {


Date birthday;
String name;
...
}

public class Employee extends Person {


...
public String getDetail() {
String s;
s = name + "," + birthday;
s += "," + salary;
return s;
}
}
Nguyn Vit H

K tha

16

Khc gi

package abc;
public class Person {
protected Date birthday;
protected String name;
...
}

import abc.Person;
public class Employee extends Person {
...
public String getDetail() {
String s;
s = name + "," + birthday;
s += "," + salary;
return s;
}
}
Nguyn Vit H

K tha

17

nh ngha li cc phng thc


Chng ta c th nh ngha li cc phng
thc ca lp c s
i tng ca lp dn xut s hot ng
vi phng thc mi ph hp vi n
C th ti s dng phng thc cng tn
ca lp c s bng t kha super

Nguyn Vit H

K tha

18

V d

package abc;
public class Person {
protected Date birthday;
protected String name;
public String getDetail() {...}
...
}

import abc;
public class Employee extends Person {
...
public String getDetail() {
String s;
s = super.getDetail() + "," + salary;
return s;
}
}

Nguyn Vit H

K tha

19

nh ngha li phng thc


Phi c quyn truy cp khng cht hn
phng thc c nh ngha li
Phi c kiu gi tr tr li nh nhau

Nguyn Vit H

K tha

20

class Parent {
public void doSomething() {}
public int doSomething2() {
return 0;
}
}
class Child extends Parent {
protected void doSomething() {}
public void doSomething2() {}
}

Nguyn Vit H

K tha

21

Tha k nhiu tng


Person
-name
-birthday
+setName
+setBirthday

Mi i tng u
tha k t lp gc Object

Student
Employee
-salary
+setSalary
+getDetail

Manager
-rank
...

Nguyn Vit H

K tha

-id
...

Programmer
-project
...

22

Constructor
Lp dn xut k tha mi thuc tnh v
phng thc ca lp c s
Khng k tha phng thc khi to
C hai gii php gi constructor ca lp
c s

dng constructor mc nh
gi constructor ca lp c s mt cch tng
minh
Nguyn Vit H

K tha

23

class Point {
protected int x, y;
public Point() {}
public Point(int xx, int yy) {
x = xx;
y = yy;
}
}
class Circle extends Point {
protected int radius;
public Circle() {}
}
Point p = new Point(10, 10);
Circle c1 = new Circle();
Circle c2 = new Circle(10, 10); // erorr
Nguyn Vit H

K tha

24

Gi constructor ca lp c s
Vic khi to thuc tnh ca lp c s nn
giao ph cho constructor ca lp c s
S dng t kha super gi
constructor ca lp c s

Constructor

ca lp c s bt buc phi
c thc hin u tin
Nu lp c s khng c constructor mc nh
th bt buc phi gi constructor tng minh
Nguyn Vit H

K tha

25

class Point {
protected int x, y;
public Point() {}
public Point(int xx, int yy) {
x = xx;
y = yy;
}
}
class Circle extends Point {
protected int radius;
public Circle() {}
public Circle(int xx, int yy, int r) {
super(xx, yy);
radius = r;
}
}
Nguyn Vit H

K tha

26

class Point {
protected int x, y;
public Point(int xx, int yy) {
x = xx;
y = yy;
}
}
class Circle extends Point {
protected int radius;
public Circle() { super(0, 0); }
public Circle(int xx, int yy, int r) {
super(xx, yy);
radius = r;
}
}

Nguyn Vit H

K tha

27

class Point {
protected int x, y;
public Point() {}
public Point(int xx, int yy) {
x = xx;
y = yy;
}
}
class Circle extends Point {
protected int radius;
public Circle() { }
public Circle(int xx, int yy, int r) {
// super(xx, yy);
radius = r;
}
}
Nguyn Vit H

K tha

28

Th t khi to
class Point {
protected int x, y;
public Point() {
System.out.println("Point constructor");
}
}
class Circle extends Point {
protected int radius;
public Circle() {
System.out.println("Circle constructor");
}
}
...
Circle c = new Circle();

Nguyn Vit H

K tha

29

T kha final

Thuc tnh final


hng

s, ch c gn gi tr khi to mt ln, khng


thay i c gi tr

Phng thc final


khng

Tham s final
khng

cho php nh ngha li lp dn xut


thay i c gi tr ca tham chiu

Lp final
khng

Nguyn Vit H

nh ngha c lp dn xut

K tha

30

You might also like