java - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

java

Description:

sp[2] = new Shape(...); ???? ????? ?? ?????? ????? ????, ??????, ... class Circle. ??? ????? ??? ???? ???? ?? ???? ???? ?? ????? ????. prc033. 21. 11/22/09 ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 38
Provided by: csBg
Category:
Tags: class | java

less

Transcript and Presenter's Notes

Title: java


1
???? ?????? ? java
  • ????? ????? ????? ????? 4
  • ?????

2
????? ??? ???
  • ???? ?? ????? 4 ?'
  • ???? ?????? ?????, ???? ????, Object.
  • ?????
  • ????, ?????,
  • ?????? ? polymorphism
  • ????? ??????? ???? ???.
  • ???? ????????? ??????.

3
????? - ????????
  • ??? ????????? ??????? ?? ????? ????? ?????.
  • ?????? ??????? ??? ???????.
  • ?????? ???? ????? ??? ??????.
  • ????? ??? (???? ???? ?? windows).
  • ???? ???? ?? ????.

4
???? ??????? ?????
  • ????? ??????
  • ????, ?????, ?????, ??????.
  • ????? ?????? ??????, toString().
  • ???? ?? ????????
  • ????? ????? ?? ??????.
  • ???? ????? ????? ?? ?? ?????? ??? (?????)
  • ????? ??????, ???? ????????.

5
???? ???? ????????
  • ?????? ????? ??? ????? ?? ????????
    StudentContainer
  • ??? ????? ???? ?? ?????????
  • ?????, ????, ??????

6
???? ???? ???????? 1
public class StudentContainer1 // data
members public static final int
INIT_SIZE10 // the first (init) size. public
static final int RESCALE10 // the re-scale
factor private int _sp0 private Student
_students / Constructors creates a empty
set / public StudentContainer1() _sp0 _
students new StudentINIT_SIZE
7
???? ???? ???????? 1
/ this method returns a list of all the
Students / public String toString() String
ans "StudentContainer1 "size()"\n" for(
int i0 iltthis.size()ii1) ans (i1)")
"at(i)"\n" return ans
8
???? ???? ???????? 1
9
???? ???? ???????? 3
  • ?????? ??? ????? ????
  • ????? ?????? ?????.
  • ???? ??? ??? ???? ?"? ?????????.
  • class Student //
  • public boolean equals(Student other)
  • return other!null this.getID()
    other.getID()
  • public boolean smaller(Student other)
  • return othernull this.getID() lt
    other.getID()

10
???? ???? ???????? 3
public class StudentContainer3 // data
members StudentContainer1 _sc public
StudentContainer3() _sc new
StudentContainer1() public Student
search(Student s) return at(find(s))
public Student search(long id) return
search(new Student(id,"Dammi"))
11
???? ???? ???????? 3
public boolean member(Student s) return
this.search(s)!null / add a Student to
this set! / public void add (Student
p) if(p!null !this.member(p))
_sc.add(p) sort() else
System.out.println(" ERR can not add "p" to
the set! ")
12
????? - ??????? ?? ???????
  • ??? ?????? ???? ????? ??? ??????? ???? ?????.
  • ??? ?????? ??? ??? ?????? ????? ???? ??? ?????
    ?? ??????.
  • ??? ???? ?????? ???? ???? ?? ?????????

13
??????? ?? ?????? - ?????
  • ????? ???? ???? ?? ???? (???? ???? ?????)
  • ????? ??? ?????? ???? ????? (??? ?????)
  • ????? ?? ?????? ???? ????? 4

14
??????? ?? ?????? - ?????
  • ???? ?????? ????? ????????? ??????? ?????
  • ????? ????? ?????.
  • Shape sp new Shape4
  • sp0 new Rect()
  • sp1 new Circle(new Point(1,1),4)
  • sp2 new Shape()
  • ???? ????? ?? ?????? ????? ????, ??????,

15
??????? ?? ?????? - ?????
  • ?????? Shape, ???? ????? ??? ?????
  • ?????
  • ???, ????, toString()
  • ??? ?????, ????? ???..
  • ????? Constructor
  • ???? ??? (???? ???? ?? int)

16
??????? ?? ?????? - ?????
public class Shape private int
_color public Shape(int c) this._color
c public void setColor(int c)this._color
c public int getColor() return
_color public double area() ??? return 1
17
????? ????
  • ?? ????? ????? ???? ???? ?????? ??? color
  • ???? ????? ??????? ??????? ???? ???? ???????
    ??????? - area().
  • ??? ????? ???? ?? ?????? ???? ?????? ?????
    (????? ????? ?????? extends ?????? ??????).

18
?????? Circle ????? ? Shape
  • ????? ?????
  • ???, toString
  • ??? ???? ???? ?????

class Circle extends Shape public double
area() // over riding!! return
Math.PI_radiud_radius public String
toString() return Circle
cen_center rad _darius // class
Circle
19
????? ????
  • ???? ???? ?? ????!! ??? ???? ?????? ?? ???? ????
    ????? ???? ????? ????? ?? ????? ???? ?????!
  • ? java ????? ????? ???? ?????? ??? ????!.
  • ?????? ???? ????? ???? super (????? ? this)

20
?????? Circle ????? ? Shape
??? ????? ??? ???? ???? ?? ???? ???? ?? ?????
????
class Circle extends Shape public
Circle(point p, double r, int color)
super(color) // must be the first line!
// class Circle
  • ???? ?? ????? ????? ???? ?????
  • ??????? ????? ????? ???? ???!!

21
????? ?????? ?????? ??????
  • Java ????? (???? ????) ?? ??????? ????? ????? ???
    ????.
  • ???? ????? ???? ?? ???????
  • ???? ??? ????? ????? ?????? (????? ????????)
  • ?? ?? ???? ???? ??? ?????? ???? ???? (??? ????).
  • ??????? ??? ???? poly-morphism ?????
  • ????? ?? ???? ?? ????? ????? ?????? ?? ???? ?? ??
    ???? ????? ?????? ???? ?? ???????? ??????!!

22
????? ?????? ?????? ??????
  • ??? ?? ???????
  • ???? ?? ??? ?? ?????? ????? (???? ???? ????? ???
    ??????)
  • ?????? ????? ?????? ????????, ?????? ???? ???????
    ?????? (?????? ??? \ ????? ????? ????).

23
????? ??????? ???? ???
  • ??? ???? ???? ??? ?????? ???? ??? ??????? ??
    ???? ????? ?? ?????? (????? ?? ??????? ???????).
  • ????? ???? ?????? instanceof, ?????
  • if(arri instanceof Circle)
  • instanceof ??????? ?????? ????? ??????? (?????)
    ?????? (?????) ????? ??? ?? ???????? ????
    ?????? ?? ??????.
  • ???? ?? ????? ??? ????? ???? ???
  • If(arri instanceof Shape)

24
????? ??????? ???? ???
  • ????????? ???? ???? ?? ???? ???? ?? ????? ??????
    ???? ?? ????? ????? ??? ?????? ?????? ?? ????
    ???? ????, ?????? ???????.
  • ?????
  • if(arri instanceof Circle) // run time.
  • Circle c (Circle)arri // compile time.
  • double rad c.radius() // return this radius
  • double rad2 arri.radius() // Error (compile
    time)
  • //.

25
????? ??????? ???? ??? - Object
  • ?? ???????? ??? ??????? ?????? ? Object
  • ???? ????? ???? Object arr new Object5
  • ???? ?????? ?? ????? ?? ????? (?????? ??????? ..)
  • ? Object ???? ????? ???? toString()
  • ????? ?????? ?? ?????? Object (java api)
  • ?? ??????? ???? ?????? Object!

26
???? ???? ???? - Collection
  • ??? ?????
  • ?????? ?? ???? ????
  • ????? ?????.
  • ?????, ????? ??????, ????? ????????.
  • ???? ??????, ???? ?????.
  • ?????? ????.
  • ??????.
  • ????? ????? ????

27
???? ???? ???? - Collection
  • ?????? ?????
  • ???? ????? ????? ????????? ???????? ?????.
  • ???? ???? ?? ????? ????? ????? ????, ??????.
  • ???? ?????? Object
  • ????? ???? ????? ?? ????? ???? ? java
  • ????? ???? ???? ????? ??? ?? ???????.

28
???? ???? ???? - Collection
  • ????? ?????? Object ??? ?????? ???? ????
    (vector)
  • ?????
  • ?????
  • ?????
  • ????
  • ????? ????? ???????, ????????? \ ????

29
???? ???? ???? - Collection
public class MyVector // data members
public static final int INIT_SIZE10,
RESCALE10 private int _sp0 private
Object _objs / Constructors creates a
empty set / public MyVector() _sp0 _obj
s new ObjectINIT_SIZE
30
???? ???? ???? - Collection
public int size() return _sp public void add
(Object p) if (p ! null) if(_sp_objs.len
gth) rescale(RESCALE) _objs_sp p //
reference copy semantic. _sp1 public
Object at(int p) return _objsp // ?????!!
31
???? ???? ???? - Collection
/ return true if obj equals to one of the
vector elements/ public boolean member(Object
obj) boolean ans false for(int
i0obj!null !ans iltthis.size()ii1)
if(obj.equals(at(i))) ans true return
ans
32
???? ???? ???? - Collection
class StudentContainer4 extends MyVector public
StudentContainer4() super() public void add
(Object p) // over rides!!! if(p instanceof
Student !member(p)) super.add(p) el
se System.out.println(" ERR )
33
???? ???? ???? - Collection
  • ????? ? at ?? ???? ????????? StudentContainer4
    ????? ???????
  • ?? ????? ?????? ?????? ????????? ???? ??????
    ???? ???? (down cast), ?????? (??? class13.java )
  • SOP( ( (Student)sc1.at(0) ).getID() )
  • ???? ?? ??????? ??? ??? ???? ???? ?????????.

34
?????? ??? ???? ????????
  • ???? ????? ?? ??????? ?????? ?????!
  • ???? ??????? ???? (?? ???????), ?????? ????????
    ????? ????? (????? ?????) ??? ????? ????? ???
    ????? ???? ?? ????????, ????? ?? ???? this.
  • ??????? ????? ???? ?????? ?? ?? ??????? (???? ???
    ???? ????? ?? this).

35
???? ????????? ??????
  • ???? ????? ????? ?????? ?? ??????? (????? ??
    this).
  • ???? ?????? ????? ??????? ???? ?????? ???? ???.
  • ???? ??? ??? ???? ??? ???????? ????? ??????
    ???????
  • ???? ???? ???? ????? ?????? ??? ??????? ??? ????
    ???? ???? ???? ?????? (?? ??????? ???? ????
    ?????? ????).
  • ??????? ????? ???? ????? ?? this ???? ?????
    ????? ?? ?? ???? ???? ?? ??????.

36
???? ????????? ?????? - ?????
  • public class Shape
  • private static int _counter 0
  • private int _color
  • public Shape(int c)
  • this._color c
  • _counter
  • public static int instances() return _counter

37
?? ????? ????
  • ?????
  • ???????? ?????? ???????.
  • ?????? ??????, (???? ???? ?? ????).
  • ?????? ?????? ?? ????? polymorohism
  • ????? ??????? ???? ???, ???? (casting).
  • ????? ???? ???? ???? Object
  • ???? ????????? ??????.
Write a Comment
User Comments (0)
About PowerShow.com