tng Lm th no area() PHI c nh ngha ti mi lp con ca Shape +area() +draw() -xCoord -yCoord Shape +area() +draw() -radius Circle +area() +draw() -side Square +area() +draw() -height -length Rectangle 76 Phng thc tru tng C th nh ngha phng thc area() l phng thc tru tng nh sau : public abstract double area(); Lu : khng th s dng lp Shape khai bo cc i tng 77 Lp tru tng Mt lp cha mt hoc hn 1 phng thc tru tng l mt lp tru tng Mt lp tru tng Phi c khai bo vi t kha abstract Khng th dng khi to abstract i lp vi final Mt lp tru tng phi c k tha trc khi i tng c th c to ra 78 Lp c th Lp c th l lp c th khai bo kiu i tng thuc lp y Lp c th k tha mt lp tru tng phi hin thc ha mi phng thc tru tng khai bo trong lp cha 79 Giao din (interface) c s dng cho nhng lp c cc phng thc ging nhau (tn) Giao din l c tnh ca ring Java v l mi lin kt gia cc lp c lin quan Mi phng thc trong 1 giao din l tru tng. Mi phng thc ca mt giao din c t ng c kiu truy cp l public Giao din khng c c thuc tnh nhng c th c hng s (final) 80 V d public class Product { private int productID; private String productName; private double price; public Product(int id, String name, double price) { this.productID = id; this.productName = name; this.price = price; } public String toString() { return productID + ": " + productName + " $" + price; } public int getProductID() { return productID; } public String getProductName() { return productName; } public double getPrice() { return price; } } 81 V d public class Student { private int studentNumber; private String studentName; private String degree; public Student(int studentID, String name, String degree) { this.studentNumber = studentID; this.studentName = name; this.degree = degree; } public String toString() { return studentNumber + ": " + studentName + ": " + degree; } public int getStudentNumber() { return studentNumber; } public String getStudentName() { return studentName; } public String getDegree() { return degree; } } 82 V d public class Lipstick { private int shadeNumber; private String shadeColour; public Lipstick(int shade, String colour) { this.shadeNumber = shade; this.shadeColour = colour; } public String toString() { return shadeNumber + ": " + shadeColour; } public int getShade() { return shadeNumber; } public String getColour() { return shadeColour; } } 83 V d public static void sort(Product[] products) { for (int i = products.length - 1; i > 0; --i) { int indexOfLargest = 0; for (int j = 1; j <= i; ++j) { if (products[j].getProductID() > products[indexOfLargest].getProductID()) { indexOfLargest = j; } } Product temp = products[i]; products[i] = products[indexOfLargest]; products[indexOfLargest] = temp; } } 84 V d public static void sort(Student[] students) { for (int i = students.length - 1; i > 0; --i) { int indexOfLargest = 0; for (int j = 1; j <= i; ++j) { if (students[j].getStudentNumber() > students[indexOfLargest].getStudentNumber()) { indexOfLargest = j; } } Student temp = students[i]; students[i] = students[indexOfLargest]; students[indexOfLargest] = temp; } } 85 V d public static void sort(Lipstick[] lipsticks) { for (int i = lipsticks.length - 1; i > 0; --i) { int indexOfLargest = 0; for (int j = 1; j <= i; ++j) { if (lipsticks[j].getShade() > lipsticks[indexOfLargest].getShade()) { indexOfLargest = j; } } Lipstick temp = lipsticks[i]; lipsticks[i] = lipsticks[indexOfLargest]; lipsticks[indexOfLargest] = temp; } } 86 Khai bo giao din Format: [modifier] interface InterfaceName [extends interface1, interface2, ...] { [constants] [methods] } Mi phng thc trong 1 giao din l tru tng. 87 Khai bo interface Khai bo vi t kha interface V d : public interface IntegerKeyed { int getIntegerKey(); } 88 Thc thi mt giao din class A [extends B] implements Interface1, Interface2, { [attribute declarations] [method implementations] } Mt lp c th thc thi hn mt giao din Mt lp thc thi mt giao din phi Cha cc nh ngha y ca mi phng thc m t trong giao din Khai bo nh l i tng thc thi giao din implements InterfaceName 89 Interface vs Class Mt lp ch c th k tha t 1 lp cha Mt lp c th k tha (thc thi) nhiu giao din Mt lp tru tng khng ch cha phng thc tru tng Nhng lp tru tng c th cha cc phng thc bnh thng Mt giao din khng c cha bt c on m thc thi no 90 K tha giao din Interface c th c k tha public interface TripleKeyed extends IntegerKeyed, StringKeyed { double getDoubleKey(); } public class Product implements TripleKeyed {
public int getIntegerKey()
{ return productID; } public String getStringKey() { return productName; } public double getDoubleKey() { return price; } }