Java Object Oriented Programming 03

Embed Size (px)

DESCRIPTION

Interfaces.pdf

Citation preview

  • Giao din

    GV. Trn Trng Hiu Bi 4. Giao din

  • 2

    Ni dung

    nh ngha mt giao din

    Thc thi mt giao din

    Dng giao din nh mt kiu

    Vit li giao din

    V d

    GV. Trn Trng Hiu Bi 4. Giao din

  • 3

    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.

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

    nhin khng nn xp hai i tng ny vo cng mt lp i tng

    no c

    GV. Trn Trng Hiu Bi 4. Giao din

  • 4

    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

    public interface Relatable {

    public int isLargerThan(Relatable other);

    }

    GV. Trn Trng Hiu Bi 4. Giao din

    Mt kiu tru tng thun ty

  • 5

    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;

    void doSomething(int i, double x);

    int doSomthingElse(String s);

    }

    Vy GroupedInterface c bao nhiu ch k phng thc?

    GV. Trn Trng Hiu Bi 4. Giao din

    Mt hng s

    Hai ch k phng thc

  • 6

    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.

    GV. Trn Trng Hiu Bi 4. Giao din

  • 7

    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 .

    GV. Trn Trng Hiu Bi 4. Giao din

  • 8

    Thc thi mt giao din

    public class Person implements Relatable {

    private double weight;

    public Person(double w) {

    weight = w;

    }

    public double getWeight() {

    return weight;

    }

    public int isLargerThan(Relatable other) {

    Person person = (Person) other;

    if (this.getWeight() < person.getWeight())

    return -1;

    else if (this.getWeight() > person.getWeight())

    return 1;

    else

    return 0;

    }

    }

    GV. Trn Trng Hiu Bi 4. Giao din

    Thc thi phng thc c m t

    trong giao din Relatable.

    Ch ton t p kiu

  • 9

    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;

    height = h;

    }

    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;

    }

    }

    GV. Trn Trng Hiu Bi 4. Giao din

    Thc thi phng thc bt buc so

    snh hai hnh ch nht.

  • 10

    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();

    GV. Trn Trng Hiu Bi 4. Giao din

    Khai bo ny li nu lp A khng thc thi

    giao din Relatable.

  • 11

    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;

    }

    GV. Trn Trng Hiu Bi 4. Giao din

    Phng thc ny cho php so snh hai i

    tng kiu Person hay hai i tng kiu Rectangle.

  • 12

    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.

    GV. Trn Trng Hiu Bi 4. Giao din

  • 13

    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.

    GV. Trn Trng Hiu Bi 4. Giao din

  • 14

    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);

    }

    GV. Trn Trng Hiu Bi 4. Giao din

  • 15

    Mt s v d khc v giao din

    /**

    * Describes an interface for any class whose

    * objects can be measured.

    */

    public interface Measurable {

    /**

    * Gets the measure of the object.

    * @return a double

    */

    double getMeasure();

    }

    Ta nn lm th no nu mun m hnh th g m c th o

    c?

    GV. Trn Trng Hiu Bi 4. Giao din

    Ti liu v giao din

    Ti liu v phng thc

  • 16

    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); }

    GV. Trn Trng Hiu Bi 4. Giao din

  • 17

    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 {

    /**

    * 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);

    }

    GV. Trn Trng Hiu Bi 4. Giao din

  • 18

    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 true if success, false if failure. */ boolean chasePrey(Prey p); /** * Eats a prey. * @param p a prey */ void eatPrey(Prey p); }

    GV. Trn Trng Hiu Bi 4. Giao din

  • 19

    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)

    }

    }

    GV. Trn Trng Hiu Bi 4. Giao din

    /**

    * A frog can be a predator or a prey.

    */

    public class Frog implements Predator, Prey {

    // implement methods of the two interfaces here...

    }

  • 20

    Mt s v d khc v giao din

    /**

    * A venomous predator.

    */

    public interface VenomousPredator extends Predator, Venomous{

    // interface body

    }

    GV. Trn Trng Hiu Bi 4. Giao din

    /**

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

    */

    public class Snake implements VenomousPredator, Prey {

    // implement methods of the two interfaces here

    }