??sa???? st? Java (????? B - PowerPoint PPT Presentation

About This Presentation
Title:

??sa???? st? Java (????? B

Description:

???s? java.lang.Thread ?a? d?epaf? java.lang.Runnable. ... implements Runnable { String msg; public PrintThread(String msg) { this.msg = msg; ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 24
Provided by: stou7
Category:
Tags: java | runnable

less

Transcript and Presenter's Notes

Title: ??sa???? st? Java (????? B


1
??sa???? st? Java (????? B)
  • G?a ?s??? ????????? C

?as???? St??µp?? stoumpos_at_di.uoa.gr
2
?e????af? ?a???s?as??
  • ?????f???e? ??a t? Java
  • ??µ? ?????aµµ?t??
  • ?a?a?t???st??? G??ssa?
  • ??t??e?µe??st?af?? ???t??? st? Java
  • ??a?e???s? ???µ??
  • ??a???se?? (Exceptions)
  • S???????sµ?? (Synchronization)

3
?e????af? ?a???s?as??
  • ?????f???e? ??a t? Java
  • ??µ? ?????aµµ?t??
  • ?a?a?t???st??? G??ssa?
  • ??t??e?µe??st?af?? ???t??? st? Java
  • ?a??ta, ???se??, d?epaf?? ?a? a?t??e?µe?a.
  • ????????s? (encapsulation).
  • ???????µ???t?ta.
  • ????µ??f?sµ??.
  • ??a?e???s? ???µ??
  • ??a???se?? (Exceptions)
  • S???????sµ?? (Synchronization)

4
?a??ta
  • ??????s? ???se?? se pa??ta.
  • ???µata ?p?? java.io, javax.swing, org.apache,
  • ??at?t?ta ???se??
  • public
  • package
  • ???s? pa??t?? µe import.
  • ????s? pa??t?? µe package.

5
???se?? (1)
  • ?ed?a (fields).
  • ?ed?a ???se?? (class fields).
  • ?e??d??? (methods).
  • ?e??d??? ???se?? (class methods).
  • ?e??d??? d?µ??????a? (constructors).
  • ??af??? this.

public class Counter private static int
OBJECTS 0 int value 0 public Counter()
this(0) Counter(int value)
Counter.OBJECTS this.value value
public int increment() return
this.value
6
???se?? (2)
  • ?p??? stat???? a?????p???s?? (static
    initializers).
  • ?s?te????? ???se?? (inner classes).
  • ?????µe? es?te????? ???se?? (anonymous inner
    classes).
  • ???s???
  • ???s?
  • ??t??e?µe??
  • ??af???

public class Counter private static
ObjectCounter OBJECTS int value 0 static
Counter.OBJECTS new
ObjectCounter() private class
ObjectCounter //
7
????????s? (Encapsulation)
  • ?p?????? es?te????? d?µ?? µe p??sd????sµ?
    ??at?t?ta? µe???.
  • ??at?t?te?
  • public
  • private
  • protected
  • package
  • ??epaf?? (Interfaces)
  • ???se??, ????? ped?a ?a? µe t?? d???s? µe??d??
    µ???.
  • ???sd???????? t??p? ???s?? a?t??e?µ????.

public interface Counter public int
increment() public int decrement() public
int getValue() public void setValue(int v)
8
???????µ???t?ta (1)
  • ??ass??? pa??de??µa
  • Ge???? (abstract) ???s?.
  • ?e?a???a ???se??.
  • ??aµ??t? ???????µ???t?ta?
  • ??a ???s? ap?te?e? ta?t?????a e?e?d??e?s? d??
    ???se?? µe ????? p??????.

Shape
Polygon
Circle
Text
Rectangle
Triangle
CircledText
9
???????µ???t?ta (2)
public abstract class Shape public Shape()
abstract void draw()
Shape
public class Circle extends Shape Point
centre int radius public Circle(int
radious) super() void draw()
Circle
Text

public final class Text extends Shape
//
10
???????µ???t?ta (3)
  • ???s? ??epaf??
  • ?e?????f??µe t??p??? ???s??.
  • ??a a?t??e?µe?? µp??e? ?a ???s?µ?p????e? µe p???
    ap? ??a t??p???.
  • ?e? ????µe p??ß??µata d?aµa?t???.
  • ???a??? st? S?ed?asµ?!

Shape
Text
Polygon
Circle
Simple Text
Rectangle
Triangle
CircledText
11
???????µ???t?ta (4)
public interface Shape abstract void
draw()
Shape
Text
public interface Text abstract String
getText()
Circled Text
Circle
public class Circle implements Shape Point
centre int radius void draw() //

public class CircledText implements Shape,
Text public void draw() public String
getText()
12
????µ??f?sµ??
  • ?p? t? p??????µe?? pa??de??µa
  • ?a???µe t?? draw se µetaß??t? t?p?? Shape.
  • ?a?e?ta? ? draw t?? p?a?µat???? a?t??e?µ????.
  • ?p????s? (extends) ? ???p???s? (implements).
  • ??e? ?? µ???d?? st? Java e??a? ?p?? ?? virtual
    t?? C.

Shape shapes new Shapes3 shapes0 new
CircledText() shapes1 new
Triangle() shapes2 new Circle() for (int
i0 iltshapes.length i) shapesi.draw()
13
?e????af? ?a???s?as??
  • ?????f???e? ??a t? Java
  • ??µ? ?????aµµ?t??
  • ?a?a?t???st??? G??ssa?
  • ??t??e?µe??st?af?? ???t??? st? Java
  • ??a?e???s? ???µ??
  • ?atast??f? a?t??e?µ????.
  • S???p?d????? (garbage collector).
  • ??a???se?? (Exceptions)
  • S???????sµ?? (Synchronization)

14
??a?e???s? ???µ?? (1)
  • ??µ??????a ??t??e?µ????
  • ????d?? d?µ??????a? (constructors).
  • ?????? t??p?? d?µ??????a?.
  • ??af??? se ??t??e?µe?a
  • ??t? de? ????µe µetaß??t? st? p?a?µat???
    a?t??e?µe??.
  • ??e? ?? µetaß??t?? e??a? a?af????.
  • ?atast??f?

void drawWithBorder(Circle c) int r
c.getRadius() Circle outer new Circle(r1)
c.draw() outer.draw()
Circle list for (int i0 iltlist.length
i) Circle c listi border(c)
15
??a?e???s? ???µ?? (2)
  • ?atast??f? ??t??e?µ????
  • ?µµesa, µ???? pa???? ?a ???s?µ?p?????ta?.
  • ?et??t?? a?af????.
  • S???p?d????? (garbage collector).

void border(Circle c) int r c.getRadius()
Circle outer new Circle(r1) c.draw()
outer.draw()
outer
c
16
?e????af? ?a???s?as??
  • ?????f???e? ??a t? Java
  • ??µ? ?????aµµ?t??
  • ?a?a?t???st??? G??ssa?
  • ??t??e?µe??st?af?? ???t??? st? Java
  • ??a?e???s? ???µ??
  • ??a???se?? (Exceptions)
  • ?a??de??µa ???s?? e?a???se??.
  • ?at?????e? e?a???se??.
  • S???????sµ?? (Synchronization)

17
??a???se?? (1)
  • ??a?e???s? ??a??et???? ?e??pt?se??
  • ?? ?a?????? ?e?t?????a µ???.
  • ?a?ade??µata
  • ?e? ?p???e? t? a??e??.
  • ?????e ? s??des?.
  • ??a??es? µe t? µ?d??.
  • ?at?????e?
  • ???s?µa ???? (error/failure)
  • ??a???se?? ?at? t?? e?t??es? (runtime exception)
  • ??a???se?? ?fa?µ???? (exception)
  • ???e f??? p?? ?p???e? e?a??es? ??e???eta? e?d????
    ?e???sµ?? (exception handling).

18
??a???se?? (2)
InputStream in null try in new
FileInputStream( new File(file.dat))
catch(FileNotFoundException fnfe) //
catch(SecurityException se) // finally
//
java.lang. Throwable
java.lang. Error
java.lang. Exception
java.lang. Runtime Exception
ex
ex
ex
19
?e????af? ?a???s?as??
  • ?????f???e? ??a t? Java
  • ??µ? ?????aµµ?t??
  • ?a?a?t???st??? G??ssa?
  • ??t??e?µe??st?af?? ???t??? st? Java
  • ??a?e???s? ???µ??
  • ??a???se?? (Exceptions)
  • S???????sµ?? (Synchronization)
  • ??µata (threads).
  • ??e?d?µata se ??????? p?????.

20
??µata
  • ??µa
  • ??a ??µa e??a? µ?a ??? e??????.
  • ? e?t??es? t?? main ???eta? se ??a ??µa.
  • ? s???p?d????? d???e?e? se ??a ??µa.
  • ????s? ap?d?s?? p????aµµ?t??.
  • ???at?t?ta ??a p???p???e? efa?µ????.
  • ??µa st? Java
  • ???s? java.lang.Thread ?a? d?epaf?
    java.lang.Runnable.
  • ?pe?te????µe t?? ???s? ? ???p????µe t? d?epaf?
    ??a ?a ft?????µe d??? µa? ??µa.
  • ??t?µata
  • ?????f???a S???????sµ??.

21
?p??tas? Thread
public class ThreadTest public static void
main(String s) PrintThread t1 new
PrintThread(a) PrintThread t1 new
PrintThread(b) t1.start()
t2.start() try t1.join()
t2.join() catch(InterruptedException ie)

public class PrintThread
extends Thread String msg public
PrintThread(String msg) super()
this.msg msg public void run()
for (int k0 klt100 k)
System.out.println( k
this.msg)
22
???p???s? Runnable
public class ThreadTest public static void
main(String s) PrintThread t1 new
Thread( new PrintThread(a))
PrintThread t1 new Thread( new
PrintThread(b)) t1.start()
t2.start() try t1.join()
t2.join() catch(InterruptedException ie)

public class PrintThread implements
Runnable String msg public
PrintThread(String msg) this.msg msg
public void run() for (int k0 klt100
k) System.out.println( k
this.msg)
23
?????? ?????
  • ?at? t?? d?µ??????a t?? ??µ?t?? d??eta? a?af???
    st?? ????? p???.
  • ?a ??µata p??pe? ?a ????? s???????sµ??a
    p??sßas? st?? ????? p???.
  • ???s?µ?p????µe synchronized µp???.

public class Resources public synchronized
Resource consume() public void synchronized
produce(Resource r)
Write a Comment
User Comments (0)
About PowerShow.com