Bài giảng Lập trình hướng đối tượng - Bài 06. Một số kỹ thuật trong kế thừa
8/24/2011
Mục tiêu của bài học
Trình bày nguyên lý định nghĩa lại trong kế
thừa
Bộ môn Công nghệ Phần mềm
Viện CNTT & TT
Trường Đại học Bách Khoa Hà Nội
Đơn kế thừa và đa kế thừa
Giao diện và lớp trừu tượng
Sử dụng các vấn đề trên với ngôn ngữ lập
trình Java.
LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG
Bài 06. Một số kỹ thuật trong kế thừa
2
Nội dung
Nội dung
1. Định nghĩa lại (Redefine/Overiding)
2. Lớp trừu tượng (Abstract class)
3. Đơn kế thừa và đa kế thừa
4. Giao diện (Interface)
1. Định nghĩa lại
(Redefine/Overriding)
2. Lớp trừu tượng (Abstract class)
3. Đơn kế thừa và đa kế thừa
4. Giao diện (Interface)
3
4
class Shape
protected String name;
Shape(String n) name
public String getName()
public float calculateArea()
{
1. Định nghĩa lại hay ghi đè
{
=
{
n;
}
return name;
}
{
return 0.0f; }
Lớp con có thể định nghĩa phương thức trùng
tên với phương thức trong lớp cha:
}
class Circle extends Shape
{
private int radius;
Circle(String n, int r){
super(n);
radius
= r;
}
public float calculateArea()
float area (float) (3.14 * radius
radius);
return area;
{
=
*
}
}
5
6
1
8/24/2011
Thêm lớp Triangle
class Square extends Shape {
private int side;
Square(String n, int s) {
class Triangle extends Shape {
super(n);
private int base, height;
side = s;
Triangle(String n, int b, int h) {
}
super(n);
public float calculateArea() {
base = b; height = h;
float area = (float) side * side;
}
return area;
public float calculateArea() {
}
}
float area = 0.5f * base * height;
return area;
}
}
7
8
thisvà super
package abc;
public class Person
{
protected String name;
protected int age;
public String getDetail()
this:
super:
{
String
s = name + "," + age;
return s;
}
}
import abc.Person;
public class Employee extends Person
double salary;
{
public String getDetail()
{
String s = super.getDetail()
return s;
+
"," + salary;
}
}
9
10
Ví dụ
1. Định nghĩa lại hay ghi đè (3)
class Parent {
public void doSomething() {}
Một số quy định
protected int doSomething2() {
return 0;
}
}
class Child extends Parent {
protected void doSomething() {}
protected void doSomething2() {}
}
11
12
2
8/24/2011
Ví dụ
Nội dung
class Parent {
public void doSomething() {}
private int doSomething2() {
1. Định nghĩa lại (Redefine/Overiding)
2. Lớp trừu tượng (Abstract class)
3. Đơn kế thừa và đa kế thừa
4. Giao diện (Interface)
return 0;
}
}
class Child extends Parent {
public void doSomething() {}
private void doSomething2() {}
}
13
14
2. Lớp trừu tượng (2)
2. Lớp trừu tượng (Abstract Class)
Không thể thể hiện hóa (instantiate – tạo đối
Cú pháp?
tượng của lớp) trực tiếp
15
16
abstract class Shape
protected String name;
Shape(String n) name
public String getName()
public abstract float calculateArea();
{
Ví dụ lớp trừu tượng
{
=
{
n;
}
return name;
}
import java.awt.Graphics;
}
abstract class Action
protected int x, y;
{
class Circle extends Shape
{
private int radius;
Circle(String n, int r){
super(n);
public void moveTo(Graphics g,
int x1, int y1)
erase(g);
{
x = x1;
draw(g);
y = y1;
radius = r;
}
}
public float calculateArea()
{
float area = (float) (3.14
return area;
}
*
radius
*
radius);
abstract public void erase(Graphics g);
abstract public void draw(Graphics g);
}
}
17
18
3
8/24/2011
Ví dụ lớp trừu tượng (2)
Nội dung
class Circle extends Action
int radius;
{
1. Định nghĩa lại (Redefine/Overiding)
2. Lớp trừu tượng (Abstract class)
3. Đơn kế thừa và đa kế thừa
4. Giao diện (Interface)
public Circle(int x, int y, int r)
super(x, y); radius r;
{
=
}
public void draw(Graphics g)
System out println("Draw circle at ("
","
g.drawOval(x-radius, y-radius,
2*radius, 2*radius);
{
+
x
+
+
y
+
")");
")");
}
public void erase(Graphics g)
System.out.println("Erase circle at ("
"," + y +
{
+
x
+
}
}
19
20
Đa kế thừa và đơn kế thừa
Vấn đề gặp phải trong Đa kế thừa
Đa kế thừa (Multiple Inheritance)
khác
A
B
D
C
Đơn kế thừa (Single Inheritance)
A
E
D
F
21
Action
Shape
#name: String
#x: int
#y: int
Nội dung
+getName():String
+calculateArea():float
+draw(Graphics)
+moveTo(Graphics,int, int)
+erase(Graphics)
1. Định nghĩa lại (Redefine/Overiding)
2. Lớp trừu tượng (Abstract class)
3. Đơn kế thừa và đa kế thừa
4. Giao diện (Interface)
Circle
-radius: float
+calculateArea():float
+draw(Graphics)
+erase(Graphics)
<<interface>>
Actable
Shape
#name: String #x:int #y:int
+draw(Graphics)
+moveTo(Graphics,int, int)
+erase(Graphics)
+getName():String
+calculateArea():float
Circle
-radius:float
+calculateArea():float
+draw(Graphics)
+moveTo(Graphics,int,int)
+erase(Graphics)
23
24
4
8/24/2011
4. Giao diện
4. Giao diện (2)
• Không thể thể hiện hóa (instantiate) trực tiếp
Cú pháp?
25
26
import java.awt.Graphics;
abstract class Shape
Ví dụ
{
protected String name;
protected int x, y;
Shape(String n, int x, int y)
{
name
=
n; this.x
=
x; this.y
= y;
<<interface>>
Actable
}
Shape
#name: String #x:int #y:int
public String getName()
{
+draw(Graphics)
+moveTo(Graphics,int, int)
+erase(Graphics)
+getName():String
+calculateArea():float
return name;
}
public abstract float calculateArea();
Circle
}
-radius:float
interface Actable
{
+calculateArea():float
+draw(Graphics)
+moveTo(Graphics,int,int)
+erase(Graphics)
public void draw(Graphics g);
public void moveTo(Graphics g, int x1, int y1);
public void erase(Graphics g);
}
27
28
class Circle extends Shape implements Actable
private int radius;
{
public Circle(String n, int x, int y, int r){
Lớp trừu trượng vs. Giao diện
super(n, x, y); radius
=
r;
}
public float calculateArea()
float area
return area;
{
=
(float) (3.14
*
radius
*
radius);
Lớp trừu trượng
Giao diện
}
public void draw(Graphics g)
System out println("Draw circle at ("
“,"
{
+
x
+
+ y
+
")");
g.drawOval(x-radius,y-radius,2*radius,2*radius);
}
public void moveTo(Graphics g, int x1, int y1){
erase(g); x1; y1; draw(g);
x
=
y =
}
public void erase(Graphics g)
System out println(“Erase circle at ("
“," ")");
// paint the region with background color...
{
+
x
+
+ y +
}
}
29
30
5
8/24/2011
Nhược điểm của Giao diện để giải quyết
vấn đề Đa kế thừa
31
6
Bạn đang xem tài liệu "Bài giảng Lập trình hướng đối tượng - Bài 06. Một số kỹ thuật trong kế thừa", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
- bai_giang_lap_trinh_huong_doi_tuong_bai_06_mot_so_ky_thuat_t.pdf