java - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

java

Description:

????? 0 ?? ??? ????? ???? ?-ob, 1 (?????) ???? ???? ? 1- (?????) ?? ??? ????. ... Collection : ???? ????. Set: ????? (???? ? Collection) SortedSet: ????? ?????? ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 39
Provided by: csBg
Category:
Tags: collection | java

less

Transcript and Presenter's Notes

Title: java


1
???? ????? ????? ? java
  • ????? ????? ????? ????? 15-16
  • ??????, ?????? ???????.

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

3
????
  • ??? ??? ?? ????
  • ?? ???
  • ?) ?????.
  • ?) ?????.
  • ?) ?????????.
  • ?) ?? ??????? ??????.
  • ?? ?????, ??? ???????, ????? ?????? ????????.

4
??????? ?? ?????? - ?????
  • ????? ????? ????? ???? ?? ????.
  • ????? ??? ?????? ???? ????? (??? ?????)
  • ????? ?? ?????? ?????? 4

5
????? ???? ????? ??????
  • ????? ????? \ ????? ????????? ??? ????? ?????
    ?????? ??????, ?????? ?????? ?? ????? ???????
    ?????? ????????.
  • ???? ?? ????? ????? ???? ????? ?? ????? ????? ?"?
    ????? ????? ???????? (super()).
  • ????? ? Polymorphism ????? ??????? ???? ???
    ??? ???? ????? ???? (?? ??? ???? ???? ?? ????????
    ??????)
  • ???? ???? ???? ????, ??????? ????????

6
????? ???? ?? ?????
  • ?? ???? ???? ???
  • ?????? ??????? A, ? B (?????? ? A), ?? ?????
    ?-main

class B extends A B()super() int
a2 static int s 2 static int s()
return s int a() return a
class A A() int a1 static int s
1 static int s() return s int a()
return a
7
????? ???? ?? ?????
A b new B() B bb new B() System.out.println(
"b.s() "b.s()) System.out.println("bb.s()
"bb.s()) System.out.println("b.s
"b.s) System.out.println("bb.s
"bb.s) System.out.println("b.a
"b.a) System.out.println("b.a() "b.a())
?? ????? ? main ??? ?
8
????? ???? ?? ?????
A b new B() B bb new B() System.out.println(
"b.s() "b.s()) System.out.println("bb.s()
"bb.s()) System.out.println("b.s
"b.s) System.out.println("bb.s
"bb.s) System.out.println("b.a
"b.a) System.out.println("b.a() "b.a())
???? ?????
b.s() 1 bb.s() 2 b.s 1 bb.s 2 b.a
1 b.a() 2
9
????? ???? ????? ??????
  • ????? ????? \ ????? ????????? ??? ????? ?????
    ?????? ??????, ?????? ?????? ?? ????? ???????
    ?????? ????????.
  • ???? ?? ????? ????? ???? ????? ?? ????? ????? ?"?
    ????? ????? ???????? (super()).
  • ????? ? Polymorphism ????? ??????? ???? ???
    ??? ???? ????? ???? (?? ??? ???? ???? ?? ????????
    ??????)
  • ???? ???? ???? ????, ??????? ???????? ????, ?.?.

10
????? ?????? ???????
  • ???? ?? ????? ????? ???? ????? ??????? ?????
    ????? ???? ???!!
  • ?? ???? ???? ??? ??? ????? ??? ?? ??????
    ??????? ?????? ???? ?????????.
  • ?? ???? ?? ????? ?????? ???? ????? ????? ???
    ???? ????, ???? ?????? ?????, ???? ?????? ...
  • ???? ?? ????? ????? (interface)

11
?????? ????? ?????
  • ????? ??????!! (???? ?????? java)
  • ???? ?? ?????? ??? ??? (????? ?? ??????
    ??????????).
  • ???? ????? ???? ?? ?? ????? ?????? ???? ?????,
    ??????? ?????? ???? (??? ???).
  • ?????? ????? ????? ???? ???? ?? ?? ??????.
  • ??????? ???????
  • ???? ?????? ????, ?????.
  • ???? ?????? ?????, ???? ????? ?"? ????...
  • ???? ????? (?? ??????).

12
?????? - ????????
  • ?????? ????? ???? ???? ???????, ????? ??????,
    ?????? ?????? ?????????
  • ???????? ??? ???????? ?????.
  • ????? ????? ??????.
  • ????? ?? ????? "??????"

13
?????? ????? ?????? ?????
  • ????? ?????? ?????????.

public interface Set public void add(Object
element) public boolean isMember(Object
element) public int size() public
Object at(int i) public void union(Set
otherSet) public void intersect(Set
otherSet)
14
?????? ????? ?????? ?????
  • ???? ???? ??? ???? ???? (?? ??????)
  • ?????? ?????? ?????? (??? ?????)

public class MySet implements Set private int
_sp0 private Object _objs // ????????
???? ?????? ?? ????? public class MySet extends
MyVector implements Set //
15
?????? ????? ?????? ?????
  • ????? ?????? ?????? ???? ??? ????? ??? ????
    ?????? ?? ??????? ???????? ????? ?????!

public void union(Set other) for(int
i0iltother.size()ii1) this.add(other.at(i))

16
?????? ????? ?????? ?????
  • ????? ????? ????? ??????, ??? ???? "????"
  • ???? ?? this ???? ???? ???? ?????? ??????!

public void intersection(Set other) MySet
ms new MySet() for(int i0iltthis.size()i
i1) if(other.isMember(at(i)))
ms.add(at(i)) _sp ms._sp _objs ms._objs

17
???? ??????
  • ????? ????? ?????? (????) ?? ????????? ???????
    ?????.
  • ???? ???? ?? ????? ??? ???, ????, toString.
  • ?????? ?' ????? ???????? ???? ???? ????????? ???
    ??????.
  • ?????? ?' (?????) ????? ????? ?????? ?????? ???
    ?? ??? ????????? ?????????.

18
?????? ????? ????
  • ???? ???? ?????
  • ????? 0 ?? ??? ????? ???? ?-ob, 1 (?????) ????
    ???? ? 1- (?????) ?? ??? ????.

public interface Comparable public double
compareTo(Comparable ob)
19
?????? ????? ????
  • ????? ??? ???? ?? ?? ?????? ???? ?"? ????, ????
    ?????? ?????? ??? ??????? (???? ???????) ??????
    ????.

public abstract class Shape implements
Comparable // ????
20
?????? ????? ????
public abstract class Shape implements
Comparable // / by area! / public double
compareTo(Comparable t) double ans
1 if(t!null t instanceof Shape) Shape
other (Shape)t ans this.area() -
other.area() return ans // ...
21
???? ????
private static Comparable left(Comparable
arr) if(arrnull) return arr int mid
arr.length/2 Comparable ans new
Comparablemid for(int i0iltmidi)
ansi arri return ans
22
???? ????
public static Comparable mergeSort(Comparable
arr) if(arrnull arr.lengthlt2) return
arr Comparable left left(arr)
Comparable right right(arr) left
mergeSort(left) right
mergeSort(right) Comparable ans
merge(left,right) return ans
23
???? ????
Comparable merge(Comparable arr1,
Comparable arr2) int ind 0, i10,
i20 int len1 arr1.length,
len2arr2.length Comparable ans new
Comparablelen1len2 while(i1ltlen1 i2 lt
len2) if(arr1i1.compareTo(arr2i2) lt 0)
ansind arr1i1 i1 else
ansind arr2i2 i2 ind
//
24
???? ???? ?"? Comparator
  • ????? ???? ?????? ??? ??? ?"? ?? ??????????
  • ????? ???? ? java.util
  • public interface Comparator
  • public int compare(Object o1, Object o2)
  • public int equals(Object comp) // ??

25
???? ???? ?"? Comparator
  • ????? ????? ?????? ?? ?????
  • import java.util.Comparator
  • public class GenericComparator implements
    Comparator
  • GenericComparator()
  • / implies the lexicographic order on the object
    tostrings! /
  • public int compare(Object a, Object b)
  • return a.toString().compareTo(b.toString())

26
???? ???? ?????
Comparable arr1 new Comparable4 Object
arr2 new Object4 // arr1
Sort.mergeSort(arr1) GenericComparator comp
new GenericComparator() Arrays.sort(arr2,comp)
27
?????? ????? ?? java api
  • ??????? ?? ?????? ??? ???????? ? java
    (java.util)
  • Collection ???? ????.
  • Set ????? (???? ? Collection)
  • SortedSet ????? ?????? (???? ? Set)
  • Comperator ??? ???? ???? ?????? ??? ?? ???
    ?????????.
  • Iterator ????? (?????) ?"? ???? ????.
  • Arrays ????? ??????? ?"? ?????? ???? ???
    ????, ?????..

28
?????? ??? ????? ???????
  • ????? ??? ?????? ???? ????? ?? ??? (???? ???)
    ?????? ?????? ?????? ???? ????? ?????? ?? ??????
    ???.
  • ???? ??? ?????? ???? ????? ???? (??? ???)
  • ????? ????? ???? ?????? ???? ?? ???? ?? ??????
    ???.
  • ?? ????? ????? ??? ?????, ?? ????? ???? ????
    ????? ?????? - ????.

29
????? ???????.
Exceptions ???????? ?? ??, ??? ?? ????. ???
???? ???, ??? ????, ???? ??. ???? ?????? ???????
?????? try, catch throws, throw. ????? ??
?????? Exception.
30
????? ???????.
  • Exceptions ??? ?? ???? ????? ???? ???? \ ?????
  • ????? ??????? ??????
  • try ????? ??????? ????? ?? ???? ????? ?-catch
    ?????.
  • catch(Exception e) ?? ??????? ????? ??? ?????
    ??? ?????? ?????? ???? ????? ?????.
  • Exception ????? ? java ?????? ????? ?????, ?????
    ??????, ??????.

31
????? ???????.
  • ???????
  • ???? ???? ????? ????? ?? ???????? ?? ???? ??
    ?????? ????? ?????.
  • ???? ???? ????? ????? ??????? ??? ???? ??????
    (????? ???? ????? ??????)
  • ????? ?? ????.
  • ??????? ?? ??????.
  • ??? ????? ??????

32
????? ???????.
????? ?????? Double ?? ???? ????? ?????? ?????
????? ??? ???? Double. public Double(String s)
throws Exception // ?? ????? ???? ????
???? ???? ?????. ????? ??? ??? ????? ???? ????
???? ?????.
33
????? ???????.
public Point(String s) throws Exception
super(0) int i10 while(s.charAt(i1) !
'') i1i11 int i2i11
while(s.charAt(i2) ! ',') i2i21 int i3i21
while(s.charAt(i3) ! '') i3i31
String x s.substring(i11,i2),
ys.substring(i21,i3) _x new
Double(x).doubleValue() // may throw! _y
new Double(y).doubleValue() // may throw!

34
????? ???????.
public Point(String s) throws Exception
super(0) if(snull) throw new Exception(
ERR can not construct a Point from null
) //
35
????? ???????.
????? ????? ????? try Point p1 new
Point("1,3.2") // o.k. Point p2 new
Point("a,3.2") // fail! catch(Exception e)
System.out.println(" ERR "e"
") System.exit(1) // for termination.
36
????? ???????.
  • ???? ??????
  • ????? ?? ???? ?? ????? ??? ?? ????? ????? ??
    ?????? ??? ????? ???? ??????.
  • ????? ??? RuntimeException ????? ???? try ??????
    ????? ?????? try.
  • ??? ?????? ??? ????? ??? ???????.

37
?? ????? ????
  • ??????
  • ??? ????? ?????? ?? ???? ????? ??? ??????.
  • ???? ?? ????? ?????
  • ???? ???? ???? ??? ???? - ?? ??????.
  • ????? ???????
  • ??????, ????? ??????.
  • ?? ??????? ?? ??????.

38
????? 4?'
  • ????? 4?
  • ???? ??? ??? ????? (?????? javadoc).
  • ???? ????? ???????, "??????" ???.
  • ???? ?? ????? ???? ????
  • ????? ????? \ ?????.
  • ?????? ????.
  • ?????? ???? ??????.
Write a Comment
User Comments (0)
About PowerShow.com