You are on page 1of 20

Giao din

Bi 4. Giao din

GV. Trn Trng Hiu

Ni dung
nh ngha mt giao din Thc thi mt giao din

Dng giao din nh mt kiu


Vit li giao din V d

Bi 4. Giao din

GV. Trn Trng Hiu

nh ngha mt giao din


Cc giao din c dng m ha nhng im ging nhau

m cc lp ca cc kiu khc nhau cng c tuy nhng khng nht thit phi cha mt quan h lp. V d:
Mt ngi v mt con vt cng c th hut so. iu khng c

ngha nn biu din lp Ngi v lp Vt l cc lp con ca lp i tng Hut_so. nhin khng nn xp hai i tng ny vo cng mt lp i tng no c

Mt ngi v mt hnh ch nht u c th o mt s kch thc, tuy

Bi 4. Giao din

GV. Trn Trng Hiu

nh ngha mt giao din


Trong ngn ng OPP, mt giao din c c t bng mt

kiu tru tng.


Mt giao din ch bao gm:
Cc ch k phng thc (nguyn mu) Cc khai bo hng

Mt kiu tru tng thun ty


public interface Relatable { public int isLargerThan(Relatable other);

}
4
Bi 4. Giao din GV. Trn Trng Hiu

nh ngha mt giao din


Mt giao din c th c m rng t mt giao din khc:
public interface GroupedInterface extends Interface1, Interface2, Interface3 { double E = 2.718282; Mt hng s

void doSomething(int i, double x); int doSomthingElse(String s); } Hai ch k phng thc

Vy GroupedInterface c bao nhiu ch k phng thc?

Bi 4. Giao din

GV. Trn Trng Hiu

Thc thi mt giao din


Giao din khng th c khi to.
Relatable relatable = new Relatable(); SAI

Ti sao?
Bi v mt giao din l mt kiu tru tng v ta khng th to ra

mt i tng tru tng.

Bi 4. Giao din

GV. Trn Trng Hiu

Thc thi mt giao din


Mt lp m thc thi mt giao din th n phi thc thi tt

c cc phng thc c m t trong giao din .


Mt lp tru tng m thc thi mt giao din th n c th

ch cn thc thi mt s phng thc c nh ngha bi giao din.


Cc phng thc m cha c thc thi phi c thc thi cc

lp con ca lp tru tng .

Bi 4. Giao din

GV. Trn Trng Hiu

Thc thi mt giao din


public class Person implements Relatable { private double weight; public Person(double w) { Thc thi phng thc c weight = w; } trong giao din Relatable. public double getWeight() { return weight; } public int isLargerThan(Relatable other) { Ch ton t Person person = (Person) other; if (this.getWeight() < person.getWeight()) return -1; else if (this.getWeight() > person.getWeight()) return 1; else return 0; } } 8
Bi 4. Giao din GV. Trn Trng Hiu

m t

p kiu

Thc thi mt giao din


public class Rectangle implements Relatable { private int width = 0; private int height = 0; private Point origin; public Rectangle(Point p, int w, int h) { origin = p; width = w; Thc thi phng thc bt height = h; snh hai hnh ch nht. } public int getArea() { return width * height; } public int isLargerThan(Relatable other) { Rectangle otherRect = (Rectangle) other; if (this.getArea() < otherRect.getArea()) return -1; else if (this.getArea() > otherRect.getArea()) return 1; else return 0; } }

buc so

Bi 4. Giao din

GV. Trn Trng Hiu

S dng mt giao din


Mt giao din c dng nh mt kiu. Nu ta nh ngha mt bin m kiu ca n l mt giao

din th i tng m ta gn cho bin ny phi l mt hin thc ca mt lp m thc thi giao din .
Relatable person = new Person(50.5); Relatable rectangle = new Rectangle(new Point(5, 5), 20, 40); Relatable a; a = new A(); Khai bo ny li nu lp A khng thc thi giao din Relatable.

10

Bi 4. Giao din

GV. Trn Trng Hiu

S dng mt giao din


Mt phng thc tm i tng ln hn trong hai i tng

kiu Relatable:
public Object findLargest(Object object1,

Object object2) {
Relatable obj1 = (Relatable) object1; Relatable obj2 = (Relatable) object2; if ((obj1).isLargerThan(obj2) > 0) return object1; else return object2; Phng thc ny cho php so snh hai i
tng kiu Person hay hai i tng kiu Rectangle.
GV. Trn Trng Hiu

}
11

Bi 4. Giao din

Vit li giao din


Xt mt giao din nh sau:
public interface DoIt { void doSomething(int i, double x); int doSomethingElse(String s); }

Cc ngi dng khc to ra cc thc thi khc nhau ca giao

din ny
H to ra ba lp A, B, C cng thc thi DoIT.

12

Bi 4. Giao din

GV. Trn Trng Hiu

Vit li giao din


Gi s sau mt thi gian ta mun thm mt phng thc mi cho

DoIt:
public interface DoIt { void doSomething(int i, double x); int doSomethingElse(String s); boolean didItWork(int i, double x, String s); }

iu ny lm tt c cc lp thc thi giao din DoIt b hng. Cc lp

A, B, C khng cn lm vic c na.


Ti sao? Bi v cc lp ny khng thc thi giao din na. Nhng lp trnh vin tin tng vo giao din ny s phn i n hon ton.

13

Bi 4. Giao din

GV. Trn Trng Hiu

Vit li giao din


Ta nn to giao din DoItPlus l m rng ca DoIt. By gi nhng ngi khc c th la chn tip tc dng

DoIt hay nng cp ln DoItPlus.


public interface DoItPlus extends DoIt { boolean didItWork(int i, double x, String s); }

14

Bi 4. Giao din

GV. Trn Trng Hiu

Mt s v d khc v giao din


/** * Describes an interface for any class whose * objects can be measured. */ public interface Measurable { /** Ti liu v giao din * Gets the measure of the object. * @return a double */ double getMeasure(); } Ti liu v phng thc

Ta nn lm th no nu mun m hnh th g m c th o c?
15
Bi 4. Giao din GV. Trn Trng Hiu

Mt s v d khc v giao din


/** * Describes any class whose objects * can measure other objects. */ public interface Measurer { /** * Computes the measure of an object. * * @param anObject * the object to be measured * * @return the measure of the object. */ double measure(Object anObject); }

16

Bi 4. Giao din

GV. Trn Trng Hiu

Mt s v d khc v giao din


/** * This interface imposes a total ordering on the objects of * each class that implements it. */ public interface Comparable<T> { /** * Compares this object with the specified object for order. * * @param o the object to be compared. * @return a negative integer, zero, or a positive integer * as this object is less than, equal to, or greater * than the specified object. */ int compareTo(T o); }

17

Bi 4. Giao din

GV. Trn Trng Hiu

Mt s v d khc v giao din


/** * A predator can chase and eat preys. */ public interface Predator { /** * Chases a prey p. * @param p a prey * @return <code>true</code> if success, <code>false</code> if failure. */ boolean chasePrey(Prey p); /** * Eats a prey. * @param p a prey */ void eatPrey(Prey p); }
18
Bi 4. Giao din GV. Trn Trng Hiu

Mt s v d khc v giao din


/** * A lion is a super predator. */ public class Lion implements Predator { public boolean chasePrey(Prey p) {
// implement to chase prey p (specifically for a lion)

} public void eatPrey(Prey p) {


// implement to eat prey p (specifically for a lion)

} } /** * A frog can be a predator or a prey. */ public class Frog implements Predator, Prey {
// implement methods of the two interfaces here...

}
19
Bi 4. Giao din GV. Trn Trng Hiu

Mt s v d khc v giao din


/** * A venomous predator. */ public interface VenomousPredator extends Predator, Venomous{ // interface body }

/**
* A snake is a venomous predator and it can be prey.

*/ public class Snake implements VenomousPredator, Prey { // implement methods of the two interfaces here }

20

Bi 4. Giao din

GV. Trn Trng Hiu

You might also like