LẬP TRÌNH HƯỚNG đối TƯỢNG bài 06 một số kỹ THUẬT TRONG kế THỪA - Pdf 31

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
Đơ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.

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

k o?sqìmg?g ︰mf?I。h?s ︸mf
Bài 06. Một sốkỹthuật trong kếthừa

2

Nội dung
1.
2.
3.
4.

Nội dung

Đị
nh nghĩa lại (Redefine/Overiding)
Lớp trừu tượng (Abstract class)

}
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) {
super(n);

String s = name + "," + age;
return s;
}
}

this:
super:

9

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

Ví dụ

1. Đị
nh nghĩa lại hay ghi đè (3)

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

private void doSomething2() {}
}

1.
2.
3.
4.

Đị
nh nghĩa lại (Redefine/Overiding)
Lớ p trừ u tư ợ ng ( Abstract class)
Đơn kếthừa và đa kếthừa
Giao diện (Interface)

14

13

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
tượng của lớp) trực tiếp

Cú pháp?

16

15


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;
public Circle(int x, int y, int r) {
super(x, y); radius = r;
}
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 erase(Graphics g) {
System.out.println("Erase circle at ("


C

D

SomeClass
Animal

FlyingThing

+ color
+ getColor ()

+ color
+ getColor ()

Bird
A

E

D

Animal

FlyingThing

+ color
+ getColor ()


4.

Đị
nh nghĩa lại (Redefine/Overiding)
Lớp trừu tượng (Abstract class)
Đơn kếthừa và đa kếthừa
Giao diệ n ( I nterface)

b ‒¦ ¡
L‒\

· Y?¢ ›\

J¦\ ¦· \ ¡`‒¡\GHY¢ ›\
J ‒\•Gf‒\fi⁄ ¦ H
J¡‒\ ¡Gf‒\fi⁄ ¦ H
[[ ‹ ¡‒¢\¦¡]]

r⁄\fi¡

`¦ \ ¡

B‹\«¡Y?r ‒ ‹£ B‚Y ‹ ??B„Y ‹

J ‒\•Gf‒\fi⁄ ¦ H

J£¡ m\«¡GHYr ‒ ‹£
J¦\ ¦· \ ¡`‒¡\GHY¢ ›\

J«› ¡s›Gf‒\fi⁄ ¦ K ‹ K? ‹ H

Cú pháp?

26

25

import java.awt.Graphics;
abstract class Shape {
protected String name;
protected int x, y;
Shape(String n, int x, int y) {
name = n; this.x = x; this.y = y;
}
public String getName() {
return name;
}
public abstract float calculateArea();
}
interface Actable {
public void draw(Graphics g);
public void moveTo(Graphics g, int x1, int y1);
public void erase(Graphics g);
}

Ví dụ

[[ ‹ ¡‒¢\¦¡]]

r⁄\fi¡


class Circle extends Shape implements Actable {
private int radius;
public Circle(String n, int x, int y, int r){
super(n, x, y); radius = r;
}
public float calculateArea() {
float area = (float) (3.14 * radius * radius);
return area;
}
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); x = x1; y = y1; draw(g);
}
public void erase(Graphics g) {
System out println(“Erase circle at ("
+ x + “," + y + ")");
// paint the region with background color...
}
}

Lớp trừu trượng vs. Giao diện
Lớ p trừ u trư ợ ng

29

Giao diệ n


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status