K th a ế ừ
N iộ dung
i ạ i b ng k th a ế ừ
ề ử ụ ạ ằ
ớ ng th c, thu c tính
ộ
V n đ s d ng l ấ S d ng l ử ụ K th a trong Java ế ừ đ nh nghĩa l p k th a ế ừ ị thêm ph ứ ươ ki m soát truy c p ậ ể constructor
2 K th a ế ừ
Tài li uệ tham kh oả
Thinking in Java, chapter 6 Java how to program, chapter 9
3 K th a ế ừ
S d ng l ử ụ
i ạ
T n t
ng có các thu c
ồ ạ
ề
ộ ho c liên quan
i nhi u lo i đ i t ạ ố ượ ng t ươ
ặ
ự
i các mã
ử ụ
ệ
ầ
ạ
tính và hành vi t đ n nhau ế Person, Student, Manager,… Xu t hi n nhu c u s d ng l ấ t ngu n đã vi ế ồ S d ng l i thông qua copy ạ ử ụ S d ng l i thông qua quan h ạ ử ụ S d ng l i thông qua c ch “k th a” ơ ạ ử ụ
ệ has_a ế ế ừ
4 K th a ế ừ
S d ng l ử ụ
i ạ
Copy mã ngu nồ
ố
i nhi u phiên b n
T n công, d nh m ễ Khó s a l i do t n t ử ỗ
ầ ồ ạ
ề
ả
Quan h ệ has_a
ầ
ủ ớ i cài đ t v i giao di n m i ớ
ệ
S d ng l p cũ nh là thành ph n c a l p m i ớ ớ ử ụ S d ng l ạ ử ụ t l Ph i vi ả ế ạ Ch a đ m m d o ủ ề ư
ư ặ ớ i giao di n ệ ẻ
5 K th a ế ừ
Ví d : ụ has_a
class Person { private String name; private Date birthday; public String getName() { return name; } ... } class Employee { private Person me; private double salary; public String getName() { return me.getName(); } ... }
6 K th a ế ừ
class Manager { private Employee me; private Employee assistant; public setAssistant(Employee e) {...} ... } ... Manager junior = new Manager(); Manager senior = new Manager(); senior.setAssistant(junior); // error
7 K th a ế ừ
K th a ế ừ
ệ is_a
i các thu c tính và ph
ng
ng l
ươ
ộ
ạ
t hóa cho phù h p v i m c đích s ử
ụ
ớ
ợ
D a trên quan h ự Th a h ưở ừ th c đã có ứ Chi ti ế d ng m i ớ ụ Thêm các thu c tính m i ộ ớ Thêm ho c hi u ch nh các ph ệ
ặ
ỉ
ươ
ng th c ứ
8 K th a ế ừ
Thu t ngậ
ữ
K th a ế ừ L p c s , l p cha ơ ở ớ ớ L p d n xu t, l p con ẫ ớ
ấ ớ
9 K th a ế ừ
K th a 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; } }
10 K th a ế ừ
Employee e = new Employee();
Person
e.setName("John"); e.setSalary(3.0);
-name -birthday +setName() +setBirthday()
Employee
-salary +setSalary() +getDetail()
11 K th a ế ừ
private members
class Employee extends Person { ... public String getDetail() { String s; // s = name + "," + birthday; s = getName() + "," + getBirthday(); s += "," + salary; return s; } }
12 K th a ế ừ
M c truy c p protected ậ
ứ
Đ đ m b o che d u thông tin, thông
ả
ấ
ng các thu c tính đ
c khai báo là
ộ
ượ
ể ả th ườ private Đ i t
ộ ớ
ố ượ
ả ứ
ng thu c l p d n xu t ph i truy c p t i ậ ớ ấ ẫ ng th c get và set. chúng thông qua các ph ươ i quy t v n đ M c truy c p protected gi ề ế ấ ả ậ ứ này Đ i t
c các
ố ượ
ượ
ủ ớ
ậ
protected members c a l p c s
Các đ i t
ơ ở ậ
ố ượ
c ượ
ng c a l p d n xu t truy c p đ ấ ẫ ủ ớ ng khác không truy c p đ K th a 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; } }
14 K th a ế ừ
Các m c ki m soát truy c p
ứ
ể
ậ
Modifier
Subclass Universe
Same class
Same package
private
Yes
Yes
Yes
package (default)
protected
Yes
Yes
Yes
public
Yes
Yes
Yes
Yes
15 K th a ế ừ
Trong cùng gói
public class Person { Date birthday; String name; ... }
public class Employee extends Person { ... public String getDetail() { String s; s = name + "," + birthday; s += "," + salary; return s; } }
16 K th a ế ừ
Khác gói
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; } }
17 K th a ế ừ
Đ nh nghĩa l
i các ph
ị
ạ
ươ
ng th c ứ
i các
ạ
ứ
ể ị ng th c c a l p c s ủ ớ
Chúng ta có th đ nh nghĩa l ơ ở ng c a l p d n xu t s ho t đ ng
ạ ộ
ấ ẽ ợ
ẫ ớ
ớ
ng th c cùng tên
ứ
ph ươ Đ i t ố ượ ng th c m i phù h p v i nó v i ph ươ ớ Có th tái s d ng ph ươ ể c a l p c s b ng t khóa ừ ủ ớ
ủ ớ ứ ử ụ ơ ở ằ
super
18 K th a ế ừ
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; } }
19 K th a ế ừ
Đ nh nghĩa l
i ph
ị
ạ
ươ
ng th c ứ
ề
ph
ượ
ch tặ h n ơ i ạ
Ph i có quy n truy c p không ậ c đ nh nghĩa l ị ị ả ạ
ng th c đ ứ Ph i có ki u giá tr tr l ể
ả ươ ả
i nh nhau ư
20 K th a ế ừ
class Parent { public void doSomething() {} private int doSomething2() { return 0; } }
class Child extends Parent { protected void doSomething() {} private void doSomething2() {} }
21 K th a ế ừ
Th a k nhi u t ng
ề ầ
ừ
ế
Person
-name -birthday +setName +setBirthday
ng đ u
Student
Employee
-id ...
-salary +setSalary +getDetail
Programmer
Manager
-project ...
-rank ...
M i đ i t th a k t l p g c Object ọ ố ượ ừ ế ừ ớ ề ố
22 K th a ế ừ
Constructor
L p d n xu t k th a m i thu c tính và
ộ
ớ ph
ươ
ng th c kh i t o
ở ạ
ọ ấ ế ừ ẫ ng th c c a l p c s ơ ở ủ ớ ứ ứ ươ ế ừ i pháp g i constructor c a l p ọ ả
ủ ớ
ặ
ị
ng
Không k th a ph Có hai gi c sơ ở s d ng constructor m c đ nh g i constructor c a l p c s m t cách t ủ ớ
ơ ở ộ
ườ
ử ụ ọ minh
23 K th a ế ừ
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
24 K th a ế ừ
G i constructor c a l p c s
ủ ớ
ơ ở
ọ
Vi c kh i t o thu c tính c a l p c s ơ ở ộ
ủ ớ
ở ạ
ệ
ủ ớ
nên giao phó cho constructor c a l p c ơ sở
S d ng t ử ụ
ừ
super đ g i ể ọ
constructor c a l p c s Constructor c a l p c s b t bu c ph i đ
c
ơ ở ơ ở ắ
ộ
ả ượ
khóa ủ ớ ủ ớ th c hi n đ u tiên
ệ
N u l p c s không có constructor m c đ nh
ặ
ị
ng minh
ự ế ớ ắ
ầ ơ ở ộ
thì b t bu c ph i g i constructor t ả ọ
ườ
25 K th a ế ừ
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;
} }
26 K th a ế ừ
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;
} }
27 K th a ế ừ
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;
} }
28 K th a ế ừ
Th t
ứ ự
kh i t o ở ạ
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();
29 K th a ế ừ
T khóa
ừ
final
c gán giá tr kh i t o m t l n, không
Thu c tính final h ng s , ch đ
ở ạ
ộ ầ
ị
ộ ằ
ỉ ượ c giá tr
ị
Ph
ố thay đ i đ ổ ượ ng th c final ứ ươ
không cho phép đ nh nghĩa l
i
ị
ạ ở ớ
l p d n xu t ấ ẫ
Tham s final
c giá tr c a tham chi u
ổ ượ
ị ủ
ế
L p final
ố không thay đ i đ ớ không đ nh nghĩa đ ị
ượ ớ
c l p d n xu t ẫ
ấ
30 K th a ế ừ