Professional Documents
Culture Documents
OOP Bai08 (Vi)
OOP Bai08 (Vi)
Đa hình
trungtt@soict.hust.edu.vn
Nội dung
1. Upcasting và
downcasting
2. Đa hình
3. Liên kết tĩnh và liên kết
động
1
Up-casting và down-casting
Chuyển đổi kiểu dữ liệu đối tượng
Chuyển đổi kiểu dữ liệu nguyên
thủy
4
Chuyển đổi kiểu dữ liệu tham
chiếu
5
Up-casting
6
Ví dụ
7
Person
- name: String
Ví dụ - birthday: Date
+ setName(String)
+ setBirthday(Date)
class Manager extends Employee { + getDetails(): String
Employee assistant;
// ...
public void setAssistant(Employee e) { Employee
assistant = e; - salary: double
} + setSalary(double)
}
public class Test2 {
Manager
public static void main(String arg[]) {
- assistant: Employee
Manager junior, senior;
+ setAssistant(Employee)
// ...
+ getDetails():String
senior.setAssistant(junior);
}
8
}
Person
- name: String
Ví dụ - birthday: Date
+ setName(String)
+ setBirthday(Date)
public class Test3 { + getDetails(): String
String static teamInfo(Person p1, Person p2) {
return "Leader: " + p1.getName() +
", member: " + p2.getName(); Employee
} - salary: double
+ setSalary(double)
10
Person
- name: String
Ví dụ - birthday: Date
+ setName(String)
+ setBirthday(Date)
public class Test2 { + getDetails(): String
public static void main(String arg[]) {
Employee e = new Employee();
Person p = e; // up casting Employee
Employee ee = (Employee) p; - salary: double
// run-time error
Manager
Person p2 = new Manager();
- assistant: Employee
Employee e2 = (Employee) p2;
+ setAssistant(Employee)
}
+ getDetails():String
}
11
2
13
Trong ngôn ngữ HĐT
14
Liên kết tĩnh
15
Liên kết động
16
Person
- name: String
Ví dụ - birthday: Date
+ setName(String)
+ setBirthday(Date)
public class Test { + getDetails(): String
public static void main(String arg[]){
Person p = new Person();
// ... Employee
Employee e = new Employee(); - salary: double
// ... + setSalary(double)
// ...
Person pArr[] = {p, e, m};
Manager
for (int i=0; i< pArr.length; i++){
- assistant: Employee
System.out.println(
+ setAssistant(Employee)
pArr[i].getDetail());
+ getDetails():String
}
}
17
}
3
Đa hình
Polymorphism
Đa hình
19
Person
- name: String
Ví dụ - birthday: Date
+ setName(String)
+ setBirthday(Date)
20
Person
- name: String
Ví dụ - birthday: Date
+ setName(String)
+ setBirthday(Date)
class EmployeeList { + getDetails(): String
Employee list[];
...
public void add(Employee e) {...}
Employee
public void print() {
for (int i=0; i<list.length; i++) { - salary: double
+ setSalary(double)
System.out.println(list[i].getDetail()); + getDetails(): String
}
}
... Manager
EmployeeList list = new EmployeeList();
- assistant: Employee
Employee e1; Manager m1;
... + setAssistant(Employee)
list.add(e1); list.add(m1); + getDetails():String
list.print();
21
Toán tử instanceof
• Kiểm tra xem một đối tượng có phải là thể
hiện của một lớp nào đó không
public class Employee extends Person {}
public class Student extends Person {}
23
Ví dụ
...
public static void handleShapes(Shape[] shapes){
// Vẽ các hình theo cách riêng của mỗi hình
for( int i = 0; i < shapes.length; ++i) {
shapes[i].draw();
}
...
// Gọi đến phương thức xóa, không cần quan tâm
// đó là hình gì
for( int i = 0; i < shapes.length; ++i) {
shapes[i].erase();
}
}
...
Tổng kết
• Upcasting và downcasting
• Nhìn nhận các đối tượng thuộc lớp cơ sở như đối
tượng thuộc lớp dẫn xuất (upcasting) và ngược lại
(down-casting)
• Đa hình
• Nhìn nhận một đối tượng dưới nhiều kiểu khác
nhau
Thank you!
Any questions?