. , , ,

,,,

, , — ,

" "

" "

, ,

2004 .


.. 3

. 3

. 3

. 3

. 4

. 5

1. .. 6

2. .. 22

3. .. 28


, Java. , , .

 

, . .

 

, .

, ActionListener ( ), , , .

 

:

1.         , .

2.         , .

3.         .

 

1.         Java?

2.         ?

3.         ?.

4.         Java?.

5.         ?

6.         ?.

7.         ?


:

1.         , , . Java.

2.         Java http://java.sun.com/ ( ).

3.         Java 2 SDK, Standard Edition Documentation http://java.sun.com/products/jdk/1.5/index.html.

4.         , , . Java (The Java Language Specification http://www.javasoft.com/docs/books/jls/). http://www.uni-vologda.ac.ru/java/jls/index.html

5.         Eclipse http://www.eclipse.org/.

:

1.         . Java.

2.         . Java 2: .

3.         . . Java.

4.         .. Java.

5.         . .


1.

Java API . . . . . , , .

Java (packages). Java . (subpackages). .

. class ( -), , . .

(namespace). , , . , . , , . , : .. .

, private, protected public , "" .

private, protected, public, , , , , . public, , , public, .

Java- package ;, :

package mypack;

mypack , , mypack. , .

. , , subpack, ;

package mypack.subpack;

subpack mypack.

, -

package mypack.subpack.sub;

. . .

package ; , .

Java mypack, a subpack, class- -.

: mypack.A, mypack.subpack..

SUN , , , , . , SUN , , :

com.sun.developer

. , . class- , Java-.

. . , Java 2 API java, javax, org.omg. Java applet, awt, beans, io, lang, math, net, rmi, security, sql, text, util . , , () java.awt color, datatransfer, dnd, event, font, geometry, image, print.

, .

. , , . .1.

. .1.

Base.java : inpi, Base Derivedpi, Base. pi. Base , f() inp1 Derivedp1 . . . 3.1 .

.1. Base.java p1

package p1;

class Inp1{

public void f () {

Base b = new Base();

// b.priv = 1; // "priv has private access in p1.Base"

b.pack = 1;

b.prot = 1;

b.publ = 1;

}

}

public class Base{

private int priv = 0;

int pack = 0;

protected int prot = 0;

public int publ = 0;

}

class Derivedpi extends Base{

public void f(Base a) {

// a.priv = 1; // "priv hs private access in pi.Base"

a.pack = 1;

a.prot = 1;

a.publ = 1;

// priv = 1; // "priv has private access in pi.Base"

pack = 1;

prot = 1;

publ = 1;

}

}

.1, , private, .

Inp2.java : Inp2 Derivedp2, Base. 2. Base. . .2 .

, Base p1 public, 2 .

.2. Inp2.java 2

package p2;

import pl.Base;

class Inp2{

public static void main(String[] args){

Base b = new Base();

// b.priv = 1; // "priv has private access in pl.Base"

// b.pack = 1; // "pack is not public in pl.Base;

// cannot be accessed from outside package"

// b.prot = 1; //"prot has protected access in pi.Base"

b.publ = 1;

}

}

class Derivedp2 extends Base{

public void, f (Base a){

// a.priv = 1; // "priv has private access in. p1.Base"

// a.pack = 1; // "pack, is not public in pi.Base; cannot

//be accessed from outside package"

// a.prot = 1; // "prot has protected access in p1.Base"

a.publ = 1;

// priv = 1; // "priv has private access in pi.Base"

// pack = 1; // "pack is not public in pi.Base; cannot

// be accessed from outside package"

prot = 1;

publ = 1;

super.prot = 1;

}

}

, , .

, public, . , protected, , , .

, . . .1.

 

.1.

private +
"package" + +
protected + + *
public + + + +

protected- .

, class-, - , , .

. D:\jdkl.3\MyProgs\ch3 classes Base.java Inp2.java, .1 .2. . .2 .

.

1. classes 1 2.

2. Base.java 1 1 .

3. Base.java, 1 : Base.class, Inpl.class, Derivedpl.class.

4. Inp2java 2.

5. classes.

6. , p2\Inp2.java.

7. java p2.inp2.

2 3 class- , pi. class- .

5 6 , Inp2.java p1.Base, p1.Base.class, .

, 7 .

(options) , .

1. -d , :

javac -d classes Base.java

classes 1 class-.

2. -classpath , classes, pi:

javac -classpath classes -d classes Inp2.java

, -d, classes 2 class-, "" pi, -classpath.

3. classes.

4. java p2.inp2.

. .2.

, , - IDE, .

. .2 .

.2 import. ?

, , , , . . , .2 Base p1.Base.

, , import, .

import : import , , , . , import .

import , *. .

import p1.*;

, , public. java.lang ( ) , . import .

, import "" .

import include /++. .

Java-

Java.

           package.

           import.

           .

.

           public-.

           , .

, , , .

. ,, , .

Java , , . .4, . "", bject. D , . .4, . (multiple inheritance). . , , . .4 . "" .

. .4.

f (), D. , f () , , . . A.f ()? , ? , : B.f() .f()? , , .

-, , f().

Java . extends . super .

, - ? , Automobile, Truck . Pickup. , .

Java . , , , .

(interface), , .

, , class-.

interface, public, , , . public , .

interface , . extends - . , , , , , . ( ).

, , . , , abstract . , static final .

, public.

:

interface Automobile{ . . . }

interface Car extends Automobile{ . . . }

interface Truck extends Automobile{ . . . }

interface Pickup extends Car, Truck{ . . . }

, , . , , , .

, , ?

, (implementation). , . , , implements , , .

:

interface Automobile{ . . . }

interface Car extends Automobile! . . . }

class Truck implements Automobile! . . . }

class Pickup extends Truck implements Car{ . . . }

:

interface Automobile{ . . . }

interface Car extends Automobile{ . . . }

interface Truck extends Automobile{ . . . }

class Pickup implements Car, Truck{ . . . }

, , . , abstract.

ickup f(), , Truck ? . Pickup. -.

, Java - , .

, , , , , .

, . , - . .

.3 , .

.3.

interface Voice{

void voice();

}

class Dog implements Voice{

public void voice (){

System.out.println("Gav-gav!");

}

}

class Cat implements Voice{

public void voice (){

System.out.println("Miaou!");

}

}

class Cow implements Voice{

public void voice(){

System.out.println("Mu-u-u!");

}

}

public class Chorus{

public static void main(String[] args){

Voiced singer = new Voice[3];

singer[0] = new Dog();

singer[1] = new Cat();

singer[2] = new Cow();

for(int i = 0; i < singer.length; i++)

singer[i].voice();

}

}

voice .

: ? .

, - , bject. , , .

, . , .

: , public, , .

, . , , voice.

, , .4.

.4.

interface Lights{

int RED = 0;

int YELLOW = 1;

int GREEN = 2;

int ERROR = -1;

}

class Timer implements Lights{

private int delay;

private static int light = RED;

Timer(int sec)(delay = 1000 * sec;}

public int shift(){

int count = (light++) % 3;

try{

switch(count){

case RED: Thread.sleep(delay); break;

case YELLOW: Thread.sleep(delay/3); break;

case GREEN: Thread.sleep(delay/2); break;

}

}catch(Exception e){return ERROR;}

return count;

}

}

class TrafficRegulator{

private static Timer t = new Timer(1);

public static void main(String[] args){

for (int k = 0; k < 10; k++)

switch(t.shift()){

case Lights.RED: System.out.println("Stop!"); break;

case Lights.YELLOW: System.out.println("Wait!"); break;

case Lights.GREEN: System.out.println("Go!"); break;

case Lights.ERROR: System.err.println("Time Error"); break;

default: System.err.println("Unknown light."); return;

}

}

}

, Lights, , .

Timer . shift () . sleep() Thread , . try{} catch() {}.

TrafficReguiator Lights Lights.RED .. , RED, YELLOW GREEN .


2.

, (nested) . , (inner) . . ? , .

           , ? , , , !

           , , ? . , new .

- (member classes), , (local classes), / . , , .

- static. - , . (nestee tep-level classes), - . . , .

(inner). .

, , , . (anonymous classes).

.

class Nested{

static private int pr; // pr a

//

String s = "Member of Nested";

// .

static class .{ // Nested.A

private int a=pr;

String s = "Member of A";

//

static class AB{ // Nested..

private int ab=pr;

String s = "Member of AB";

}

}

// Nested

class { // Nested.

private int b=pr;

String s = "Member of B";

//

class { // Nested..

private int bc=pr;

String s = "Member of ";

}

void f(final int i){ // final i j

final int j = 99; // D

class D{ // D f()

private int d=pr;

String s = "Member of D";

void pr(){

// ,

// "s"

System.out.println(s + (i+j)); // "s" "this.s"

System.out.println(B.this.s);

System.out.println(Nested.this.s);

// System.out.println(AB.this.s); //

// System.out.println(A.this.s); //

}

}

D d = new D(); // , f()

d.pr(); // f()

}

}

void m(){

new Object(){ // ,

//

private int e = pr;

void g(){

System.out.println("From g()) ;

}

}.g(); //

}

}

public class NestedClasses{

public static void main(String[] args){

Nested nest = new Nested(); //

//

Nested.A theA = nest.new A(); //

// new.

Nested.A.AB theAB = theA.new AB(); // .

// new

Nested. theB = nest.new B(); //

Nested.. theBC = theB.new BC();

theB.f(999); //

nest.m();

}

}

.

           , Nested , pr. Java . , .

           Java this : Nested.this, .this.

           , , , object. .

           , , new nest.new, theA.new, theB.new.

           , new .

.

? .

           ? super , this.

           ? .

, , - :

           Nested$l$D.class , Nested;

           NestedSl.class ;

           Nested$A$AB.class Nested.A.AB;

           Nested$A.class Nested.;

           Nested$B$BC.class Nested.B.BC;

           NestedSB.class Nested.B;

           Nested.class Nested;

           NestedClasses.class - main ().

, , . , , . . .

, . Java . . . , . . , final. . , private, .

. Java , , , .

? " " "".

" " ""

. , .

, , , , ? - ?

, P Q " Q P" ("a class Q is a class ") " Q P" ("a class Q has a class P").

: " " " "? , "is-a", Dog Pet.

"is-a" "-", , .

"has-a" "-", .


3.

(callback) . , , . , , , .

. java.swing Timer, . , , Timer, . , , .

, ? , .

Java - . . .

, , . , , . , ActionListener java.awt.event.

public interface ActionListener

{

void actionPerformed (ActionEvent event);

}

actionPerformed.

. 10 , . , ActionListener. , , actionPerformed.

class Timerprinter implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

Date now= new Date( );

System.out.println( : + now);

Toolkit.getDefaultToolkit( ).bep( );

}

}

Timer.

ActionListener listener=new TimerPrinter ( );

Timer t=new Timer (10000, listener);

Timer , . .

ActionListener

t.start( );

10 . 6.2. , .

import java.awt.*;

import java.awt.event*;

import javax.swing.*;

import javax.swing.Timer*; // javax.util.Timer

public class TimerTest

{

public static void main (string[ ] args)

{

ActionListener listener= new TimerPrinter ( );

// , 10 .

Timer t=new Timer (10000, listener);

t.start( );

JOptionPne.showMessageDialog(null,?);

System.exit(0);

}

}

class Timerprinter implements ActionListener

{

public void actionPerformed(ActionEvent event)

{

Date now= new Date( );

System.out.println( : + now);

Toolkit.getDefaultToolkit( ).bep( );

}

}

&quot; &quot; &quot; &quot; ,

 

 

 

! , , , .
. , :