intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Bài giảng Lập trình hướng đối tượng: Bài 6 - Nguyễn Khanh Văn

Chia sẻ: Minh Vũ | Ngày: | Loại File: PDF | Số trang:0

41
lượt xem
1
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Bài giảng "Lập trình hướng đối tượng - Bài 6: Một số kỹ thuật trong kế thừa" cung cấp cho người học các kiến thức: Định nghĩa lại, lớp trừu tượng, đơn kế thừa và đa kế thừa, giao diện. Mời các bạn cùng tham khảo nội dung chi tiết.

Chủ đề:
Lưu

Nội dung Text: Bài giảng Lập trình hướng đối tượng: Bài 6 - Nguyễn Khanh Văn

  1. 8/24/2011 Mục tiêu của bài học Bộ môn Công nghệ Phần mềm  Trình bày nguyên lý định nghĩa lại trong kế Viện CNTT & TT thừa 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 LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG trình Java. 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) 1. Định nghĩa lại 2. Lớp trừu tượng (Abstract class) (Redefine/Overriding) 3. Đơn kế thừa và đa kế thừa 2. Lớp trừu tượng (Abstract class) 4. Giao diện (Interface) 3. Đơn kế thừa và đa kế thừa 4. Giao diện (Interface) 3 4 1. Định nghĩa lại hay ghi đè class Shape { protected String name; Shape(String n) { name = n; } public String getName() { return name; }  Lớp con có thể định nghĩa phương thức trùng public float calculateArea() { return 0.0f; } 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
  2. 8/24/2011 class Square extends Shape { Thêm lớp Triangle 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 this và super package abc; public class Person { protected String name;  this: protected int age;  super: public String getDetail() { String s = name + "," + age; return s; } } import abc.Person; public class Employee extends Person { double salary; public String getDetail() { String s = super.getDetail() + "," + salary; return s; } 9 } 10 1. Định nghĩa lại hay ghi đè (3) Ví dụ  Một số quy định class Parent { public void doSomething() {} protected int doSomething2() { return 0; } } class Child extends Parent { protected void doSomething() {} protected void doSomething2() {} } 11 12 2
  3. 8/24/2011 Ví dụ Nội dung class Parent { 1. Định nghĩa lại (Redefine/Overiding) public void doSomething() {} private int doSomething2() { 2. Lớp trừu tượng (Abstract class) return 0; 3. Đơn kế thừa và đa kế thừa } 4. Giao diện (Interface) } class Child extends Parent { public void doSomething() {} private void doSomething2() {} } 13 14 2. Lớp trừu tượng (Abstract Class) 2. Lớp trừu tượng (2)  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 = n; } Ví dụ lớp trừu tượng public String getName() { return name; } public abstract float calculateArea(); import java.awt.Graphics; } abstract class Action { class Circle extends Shape { protected int x, y; private int radius; public void moveTo(Graphics g, Circle(String n, int r){ int x1, int y1) { super(n); erase(g); radius = r; x = x1; y = y1; } draw(g); } public float calculateArea() { float area = (float) (3.14 * radius * radius); abstract public void erase(Graphics g); return area; abstract public void draw(Graphics g); } } } 17 18 3
  4. 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) public Circle(int x, int y, int r) { super(x, y); radius = r; 2. Lớp trừu tượng (Abstract class) } public void draw(Graphics g) { 3. Đơn kế thừa và đa kế thừa System out println("Draw circle at (" + x + "," + y + ")"); 4. Giao diện (Interface) g.drawOval(x-radius, y-radius, 2*radius, 2*radius); } public void erase(Graphics g) { System.out.println("Erase circle at (" + x + "," + y + ")"); } } 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 C SomeClass Animal FlyingThing + color + color  Đơn kế thừa (Single Inheritance) D + getColor () + getColor () Animal FlyingThing + color + color + getColor () + getColor () Bird A E F Bird D 21 Shape Action Nội dung #name: String +getName():String #x: int #y: int +draw(Graphics) +calculateArea():float +moveTo(Graphics,int, int) +erase(Graphics) 1. Định nghĩa lại (Redefine/Overiding) Circle 2. Lớp trừu tượng (Abstract class) -radius: float +calculateArea():float Đơn kế thừa và đa kế thừa +draw(Graphics) 3. +erase(Graphics) 4. Giao diện (Interface) Shape #name: String #x:int #y:int Actable +getName():String +draw(Graphics) +moveTo(Graphics,int, int) +calculateArea():float +erase(Graphics) Circle -radius:float +calculateArea():float +draw(Graphics) 23 +moveTo(Graphics,int,int) 24 +erase(Graphics) 4
  5. 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 Ví dụ 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; Shape Actable } #name: String #x:int #y:int public String getName() { +getName():String +draw(Graphics) +moveTo(Graphics,int, int) return name; +calculateArea():float +erase(Graphics) } public abstract float calculateArea(); Circle } -radius:float interface Actable { +calculateArea():float +draw(Graphics) public void draw(Graphics g); +moveTo(Graphics,int,int) +erase(Graphics) public void moveTo(Graphics g, int x1, int y1); public void erase(Graphics g); } 27 28 class Circle extends Shape implements Actable { Lớp trừu trượng vs. Giao diện 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); Lớp trừu trượng Giao diện 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... } } 29 30 5
  6. 8/24/2011 Nhược điểm của Giao diện để giải quyết vấn đề Đa kế thừa 31 6
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2