instanceof - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

instanceof

Description:

class Tiger implements Predator. ????? ??? ????? ???? ?? ?? ????? ????? (????? ?????????) ... 21. ?????. ?????? ????? ?? ???? ?????? ??? ?????????? ???????? ????????. ... – PowerPoint PPT presentation

Number of Views:590
Avg rating:3.0/5.0
Slides: 41
Provided by: max9188
Category:
Tags: instanceof | tiger

less

Transcript and Presenter's Notes

Title: instanceof


1
????? ???? 10
  • ??????
  • ????? ???????
  • ????? ???? ??????
  • ????? ???? ?????
  • ?????
  • ????? ???????
  • ?????
  • ??????? instanceof

2
??????
  • ???? ????? ????? ?????.
  • ?????? ??????, ????? ??? ????? ?????? ?? ?????
    ????? ????. ???? ???? ?? ?????????????? ???????
    ???? ??????? ?????? ????.
  • ?????? ??????? ????? ???? ??????? (?????????)
    ????? ?????? ????? ?? ????? ????? ??? ????.
  • ?? ???? ???? ???? ????? ???????? ?????? ?? ??
    ???? ????? ?????.

3
??????
  • ????? (interface) ???? ??? ?-Java ?????? ??????
    ?????? ??? ????? ??????.
  • ?????? ?????? ?????? ???? ???? ???? ???? ?? ????
    ?????? ?? ?????? ?????? ???? ??? ????? ?????
    ????? ??? ????? ?????? ?????? ?????? ????? ??
    ???? ??????? ??????.

4
??????
  • ?????? ???? ????? ???? ????? ????? ??? ?? ??
    ??-????? ????? ?"? ???? ???.
  • ???? ??? ??????? ??????? (????????) ?? ????? ??
    ?? ??-?????, ???? ??????????? ??? ??-???????
    ????? ??? ????.
  • ?? ?? ????? ????? ???? ??? ????? ?? ??-???????
    ????? ??? ????? ?????? ???? ??????? ???? ????.

5
?????? ????? ???????
  • ????? ?? ????
  • public interface ltnamegt
  • ltconstants listgt
  • ltmethods listgt
  • ?????? ?????? ????? (????????) ??? ?????.
  • ?? ?????? ????? ?? ???????? (public) - ?? ?? ??
    ?????? ?? ??????
  • ????? ???? ?????? ????? ??? ???? ?????????.
  • ?? ????? ????? ?? ???????? ?????? ????? ??????
    ?????? (public static final) - ?? ?? ?? ??????
    ?? ??????.

6
?????? ????? ???????
  • ??????
  • public interface Predator
  • boolean chasePrey(Prey p)
  • void eatPrey(Prey p)
  • ?????? ?????? ????? (????????) ??? ?????.
  • ?? ?????? ????? ?? ???????? (public) - ?? ?? ??
    ?????? ?? ??????
  • ????? ???? ?????? ????? ??? ???? ?????????.
  • ?? ????? ????? ?? ???????? ?????? ????? ??????
    ?????? (public static final) - ?? ?? ?? ??????
    ?? ??????.

7
?????? ????? ???????
  • ????, ????? ??????, ????? ?????.????, ??????
    ???????? ?????? ????? ??????? ????? ????
    ????.????, ???? ?????? ?? ????? x ????? ????
    Predator
  • Predator x
  • ?? ???? ????? ??????? ?????. ????
  • x new Predator()

// will not compile !!!
8
?????? ????? ???????
  • ????? ?????? ???? ????? ???? ?? ?? ???????. ????
    ?????? ?? ????? ????? ????? ???? ??
  • class Tiger implements Predator
  • ????? ??? ????? ???? ?? ?? ????? ????? (?????
    ?????????). ?????? ?? ???? ??? ?? ??????
    ??????????, ????? ???? ????? ?????. ?????
    ????????? ?? ????? ???? ?? ?? ?????? ?? ????.
  • ????? ????? ???? ???? ????? ???! ???? ?????
    ?????

9
  • public interface Predator
  • boolean chasePrey(Prey p)
  • void eatPrey(Prey p)
  • public class Tiger implements Predator
  • public boolean chasePrey(Prey p)
  • // code to chase prey p (specifically for a
    tiger)
  • return runAfterPrey(p)
  • public void eatPrey (Prey p)
  • // code to eat prey p (for a tiger)
  • chew(p)
  • swallow(p)
  • ...

10
  • public interface Predator
  • boolean chasePrey(Prey p)
  • void eatPrey(Prey p)
  • public class Shark implements Predator
  • public boolean chasePrey(Prey p)
  • // code to chase prey p (specifically for a
    shark)
  • return swimAfterPrey(p)
  • public void eatPrey (Prey p)
  • // code to eat prey p //(specifically for a
    shark)
  • bite(p)
  • swallow(p)

11
  • ???? ??? ???? ??? ??? ??? ????.
  • ????? ????? ?????? ???? ?? ?????, ????? ??????
    ????, ?????? ???? ?? ?????? ??????? ??????
    ??????? ?? ?? ??? ???????? ?????.
  • Predator preds new Predator3
  • preds0 new Tiger()
  • preds1 new Shark()
  • preds2 new Shark()
  • Prey froggy new Frog()
  • for (int i0 iltpreds.length froggy.isAlive()
    ii1)
  • frog.runAway()
  • if (predsi.chasePrey(froggy))
  • predsi.eatPrey(froggy)
  • ???? ????? ?????? ?? ??? ?????, ????? ?"?
    ??????? ?????. ??? ???? ?? ??? ????? ????, ?????
    ???? ????? ???? ?? ??? interface Prey.

??? ???? ??? ?????? froggy.jump() ?
12
  • ?? ???? ???? ????? ???? ???? ???? ???? ????
    ?????? ????????
  • public static void simulateChase(Predator predat,
    Prey prey)
  • prey.runAway()
  • if (predat.chasePrey(prey))
  • predat.eatPrey(prey)
  • ????? ?????? ?????? ?? ??????? ????? ???? ??????
    ?????, ????? ?????? ?????? ???? ???? ?? ???
    ?????? ?????? ????????.
  • Shark sharky new Shark()
  • Frog kermit new Frog()
  • simulateChase(sharky, kermit)
  • ???? ??, ???? ??? ?? ?????
  • Predator sharky new Shark()
  • Prey kermit new Frog()
  • simulateChase(sharky, kermit)

13
  • ????, ?????, ?????? ?????? (?????) ???? (????)
    ??????? ??????, ??? ??? ????? ???? ?????. ??????
  • public class Shark implements Predator
  • String name
  • int numOfTeeth
  • public Shark(String name)
  • this.name name
  • numOfTeeth 3000 (int)(Math.random()1000)
  • private void swallow(Prey p)
  • p.die()
  • public int getNumOfTeeth() return numOfTeeth
  • public void swimForFun() ...
  • public void eatPrey (Prey p)
  • bite(p)
  • swallow(p)

14
  • "???" ??? ?? ?? ???? ???? ?? ????, ?????? ????
    ???? ?????? ???????. ???? ?? ?? ?? ????
  • public interface Prey
  • public boolean isAlive()
  • public void die()
  • public void runAway()
  • ????? ?????? ?????? ???? "???"
  • public class Frog implements Prey
  • private boolean living
  • public Frog()
  • living true
  • public boolean isAlive() return living
  • public void die() living false
  • public void runAway() ...

15
  • ????? ???? ??????
  • ???? ???? ???? ????? ???.
  • ?? ?????? ??????? ???? ?? ????????? ?? ??
    ???????.
  • ?? ????? ?????? ??? ?????? ???? ???? ???? ?? ???
    ???? foo ??
  • ?? ???? ? foo ?? ????? ???? ?? ?????? ???????
    ????? ???? ?? ?????.
  • ?? ???? ? foo ?? ???? ????? ????? ????? ????? ??
    ?????? ?????? ?? foo ???.
  • ?? ???? ? foo ?? ???? ????? ?? ????? ????? ????
    - ???? ?????????. (?? ???? ???? ?? ??? ???????
    ???).
  • ?? ??? ????? ?? ????? ????. ??? ???? ????? ????
    ?? ????? (?????? ???? ???? ??? ????? ??)
  • public class Frog implements Prey, Predator
  • public boolean chasePrey(Prey p)
  • ...
  • public void eatPrey (Prey p)
  • ...
  • ...

16
  • ??? ?????? ????? ???? ?? ??? ????????
  • Shark sharky new Shark()
  • Frog kermit new Frog()
  • Fly bzzit new Fly()
  • simulateChase(sharky, kermit)
  • if(kermit.isAlive())
  • simulateChase(kermit, bzzit)
  • sharky.swimForFun()

??? ???? ??? ?????? ?? sharky ?-Predator? ???
???? ??? ?????? ?? kermit ?-Prey? ??? ???? ???
?????? ?? bzzit ?-Prey?
?? ?? ??
???? ?? ????? (?? ????) ?? ???? ????? ??????
17
  • ???? ?????
  • ????? ???? ?"??? ????" ?? ?????????.
  • ???? ???? ?? ??? ?????? ???? ????? ????? ???.
    ???? ?????? ??? ?????. ???? ????? ????? ??? ????,
    ???? ?? ???? ??? ????. ??? ?? ????? ?? ??????
    equals ?????? ?? ???? ????? ???? ???? ???.
  • lt first element, second element gt
  • ?? ???? ????? ???? ??? ????. ???? ????? ???
    ??????, first ?-second, ?? ????? ???? ????? 2.
  • ???? ?? ?? ????? orderedEquals ????? ??? ????
    ???, ?????? ?????? ???? ??? ????? (??? ??????,
    ???? ?? ????). ?? ???? ?????? ????? ??? ????
    ?????? ?????? (?????) OrderedPair ???? ???.
  • public interface OrderedPair
  • public Object getFirst()
  • public Object getSecond()
  • public boolean orderedEquals(OrderedPair
    another)
  • public boolean unorderedEquals(OrderedPair
    another)

18
  • ???? ?????

public class PairAsFields implements OrderedPair
private Object first, second public
PairAsFields(Object x, Object y) first
x second y public Object getFirst()
return first public Object getSecond()
return second public boolean
orderedEquals(OrderedPair another) return
first.equals(another.getFirst())
second.equals(another.getSecond()) public
boolean unorderedEquals(OrderedPair another)
return (getFirst().equals(another.getFirst())
getSecond().equals(another.getSecond()))
(getFirst().equals(another.getSecond())
getSecond().equals(another.getFirst()))

19
  • ???? ?????

public class PairAsArray implements OrderedPair
private Object pair public
PairAsArray(Object a, Object b) pair new
Object2 pair0 a pair1 b
public Object getFirst() return pair0
public Object getSecond() return pair1
public boolean orderedEquals(OrderedPair another)
return pair0.equals(another.getFirst())
pair1.equals(another.getSecond()) public
boolean unorderedEquals(OrderedPair another)
return (getFirst().equals(another.getFirst())
getSecond().equals(another.getSecond()))
(getFirst().equals(another.getSecond())
getSecond().equals(another.getFirst()))

20
?????
21
?????
  • ?????? ????? ?? ???? ?????? ??? ??????????
    ???????? ????????.
  • ????? ????? ????? ?? ???? ????? ??. ?????
  • ????? ??????? ??????? ????? ????????? ???? ???
    ??????, ????? ??????.
  • ??? ?????? (subclasses), ??????, ??????? ?????
    ????, ????? ???? ???????? ?? ????? ??? ?? ???
    ?? ??????? (?? ???????? ??????? ?????).
  • ????? ???????? ??? ????? ?? ?? (superclass) ??
    ?????? ????? ???? ?????? ????.

22
?????
  • ?? ?? ????? ????? ?????? ??? ??? ??????? ????.
  • ??? ?????? ???????? ??????
  • ??????? ??? (????) ??????? ??? ?????? ????????,
    ???
  • ????????? (?????) ??????? ??? ?????, ?????,
    ????? ???????.
  • ??????? ??? ??????? ???? ?????? ???????? ?????
    ???.
  • ??? ?????? ?????? ?????? ???? ?????? ?????, ????
  • ????? ???? ?????? ????? ????
  • ???????? ?? ????? ??? ?? ????? ???, ???'.

23
?????
  • ??? ?????? ????????? ?????? ??? ????? ?? ??????
    ???????? ???? (????? ????).
  • ??? ??????? ??? ???????, ?? ?????? ???? ???????.
  • ????? ???? ??????? ????? ?????? ???? ???? ???????
    ??????.
  • ??? ?????? ?????? ????? ?? ?????? ???? ?? ??????.
  • ???? ????? ??????? ??? ????? ?????? ???????,
    ???? ???? ???? ????? ????? ???? ??? ?? ???
    ??????? ???????, ???'
  • ????? ???? ?????? ???? ???. ???? ?????? ??
    ?????? ???? ?? ??????? ??? ????.
  • ????? ???? ???????

24
????? - ?????
  • ????? ????? ??????
  • 1. ????? ????? ?????? (?????) ?? ????? ???.(????
    ?????? ??????? ?? ?? ???? ?????????? ??????)

25
????? - ?????
  • 2. ????? B ????? ?? A ?? ???? ????
  • "B ??? ??? ?? A".
  • ?-Java ????? class B extends A

???????
A
????? ????
B
26
????? - ?????
  • 3. ??????? ????? (?????? B) ???? ????? ??
    ???????? ?????? (?????? A).
  • 4. A ??? ? parent class ?? ? super class ?? BB
    ??? ? subclass ?? child class ?? A

????? ???? - public void race()
???????
A
  • ???????
  • - color Red
  • speed 50 km/h- public void ride()

????? ????
B
27
????? - ?????
  • 5. B ????? ?? ?? ?????? ???????? ????? private
    ?-A???? ??????.?? ???? ???? ???? B ?????? ??
    ???? ?????? ?-A ?? ?? ??? ?????? (???? ????? ????
    ????? ????? ?????, ?????? ?? protected)

class A private int aNum class B
extends A private int bNum public
B() aNum0 bNum 0
// ? compilation error
28
????? - ?????
  • 6. ????? ???? ?????? ??????! ??? ?? ?????? ?????
    ?????
  • 7. ???????? ??????? ??? ???? B, ????? ?? ????? A
    ???? ??????.
  • ???? ????? ?? ????? ?????, ???? ???? ?????
    ??????? ????? ???-??????? ?? A.
  • ????? ?????? ????? ?"? super(). super ???? ???
    ????? ???? Java,??? ?????? ?? ????? ???.

class A private int aNum public
A(int n) aNum n
class B extends A private int bNum
public B() super(0) bNum 0

//calls the constructor of A
29
????? - ?????
  • ?? ???? ????? ???

class B extends A private int bNum
public B() bNum 0
class A private int aNum public
A(int n) aNum n
?? ????? ?????????! no empty constructor for
A Cannot find symbol constructor A()
30
????? - ?????
  • 8. ?????? ? super ????? ????? ????? ??????? ?????
    ?? B
  • ?? ???? ????? ???

class B extends A private int bNum
public B() bNum 0 super(0)

class A private int aNum public
A(int n) aNum n
????????? ???? ??? ?- 2 ?????? ?????????!
  • Cannot find symbol constructor A()
  • Call to super must be first statement in
    constructor

31
????? - ?????
  • ????? Overriding
  • ????? ????? ???? ??-????? ?????? ???? ???? ?????
    ??? ????? ??????? ?????? ???
  • ???? ????? ??????, ?????? ?? ???????? (instance)
    ?? ????? ???? ??? ?????? ??????? ???????
    ????? ???? ??????? ?????.
  • ?????? ????? ?????? ???????? ??? ??????
    (reference) ?? ????? ?????? ?????? ?????
    ???????
  • ????? ????? ?? ?????? ????? private.
  • super - ?? ????? ????? ????? ????? ???????? ??
    ??? ???? ?????? ????? super.

32
????? - ?????
  • 11.(?????) vs. Overloading Overriding (?????)
  • ????? ??? ???? ???? ???? ?????? ???????? ????
    ???? ?? ?? ?? ??????? ????? ????? ??????.
  • ????? ??? ????? ?? ??? ????? ????? ????? ???, ???
    ?????? ??? ???? ?????? ???.
  • ????? ?????? ????? ???? ???? data ????
  • ????? ?????? ???? ????? ???? ???? ????? ???????
    ????.

33
????? - ?????
  • 12. ???? ????? ???????? ????? ?????, ?? ?????? ??
    ?? ????? ??? ????? ????? ????? ?? ?? ????? ????.

34
????? - ?????
  • 13. ?? ????? ?????? ????? ????? ?? ????? ??
    ?????? Object, ???? ????? ????????? ??????.

Object
35
????? - ?????
  • ?????? Object ??????? ???????? ????????????
    (?????? ?????, ?? ????? default) ?? ??????
    toString, equals ????
  • public String toString()
  • public boolean equals(Object obj)
  • ???? ????? ??? ?????? ???????? ?"? ????? ????
    ???????

?? ?? ???? ?? ????? ???? ????? A ?? ?????
???? ?? public boolean equals(A other)
!! ?? ?????? ????? ?? ????? equals ?? ????? ???
Object, ???? ??????? ????.
36
????? 1
class A private int aNum public A(int
n) aNum n public void
inc(int n) aNum aNum n
class B extends A private int bNum
public B() super(0) bNum 0
public void inc(int amount) //
overrides inc(int) of A
super.inc(amount) //increment aNum
bNum amount //increment bNum

B - bNum 16 - void inc(int)
  • A
  • - aNum 10
  • void inc(int)

37
????? 2
class A private int aNum public A(int
n) aNum n public void
inc(int n) aNum aNum n
class B extends A private int bNum
public B() super(0) bNum 0
public int inc(int amount) //
compilation error! Cannot override!
super.inc(amount) bNum
amount return bNum
- ?? ???? ????? ???? ?? ????? ??? ?????? ?????
????
B - bNum 16 - int inc(int)
  • A
  • - aNum 10
  • void inc(int)

???? ?? ???? ???? ????? ?????? ??? ???????
????? ?????? ?????? ?? ????? ??????, ?? ?? ????
??? ????
38
????? - ?????
  • 15. ??????? instanceof??? ??????? ?????? true
    ?? ??? ?? ???????? ?????? ??? ???? (instance) ??
    ?????? ?????? ?? ????????. ???????

class Cat interface ShowAnimal class Siamese
extends Cat implements ShowAnimal
39
????? - ?????
class Cat interface ShowAnimal class Siamese
extends Cat implements ShowAnimal
Cat myaoo new Cat() Cat mitzi new
Siamese()
//true //true //Compilation Error
System.out.println(myaoo instanceof Cat)
System.out.println(mitzi instanceof Cat)
System.out.println(myaoo instanceof "Cat")
System.out.println(mitzi instanceof
Siamese) System.out.println(myaoo instanceof
Siamese)
//true //false
//true //true
System.out.println(mitzi instanceof
ShowAnimal) System.out.println(mitzi instanceof
Object)
40
????? - ?????
class Cat interface ShowAnimal class Siamese
extends Cat implements ShowAnimal
Cat myaoo new Cat() Cat mitzi new
Siamese()
System.out.println(mitzi instanceof myaoo)
//Compilation Error
//Compilation Error // operator ! cannot be
applied to Cat
System.out.println(!mitzi instanceof Cat)
//false
System.out.println(!(mitzi instanceof Cat))
Cat pitzi null System.out.println(pitzi
instanceof Cat) System.out.println(null
instanceof String)
//false //false
Write a Comment
User Comments (0)
About PowerShow.com