Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

OPI_LR2011_ch3

.pdf
Скачиваний:
5
Добавлен:
07.02.2016
Размер:
1.31 Mб
Скачать

41

@ (instance variables)

'f.

0 , ,

(

. :, Automobile

. ; @

(class variables). E f

' , .

@@ Java + static. 3.3

f + number.

' 3.3. >

class Automobile {

private static int number; Automobile(){

number++;

System.out.println("From Automobile constructor:"+ " number = "+number);

} }

public class AutomobiieTest{

public static void main(String[] args){

Automobile lada2105

= new Automobile(),

fordScorpio = new Automobile(),

oka

= new Automobile!);

} }

 

. ( f , . 3.2.

= 3.2 –

E ( ' , Automobile.number, ' , lada2105.number,

42

( , i (

.

E

@@ , + static. E

static f g . ? @ Java

( @f ' @ g ,

f ( , ( ,

.

. - @

. m g , (

, i ( . E ' ' ( ' 'f), i

@. > f

Math, @@ , @

Math.abs(x), Math.sqrt(x).

; @ (class methods), , @

(instance methods).

@ g :

(

this super;

( , @@

, ;

( ;

@

.

> @ i

, (

(. G i ( f

, ,

,

@ static, i f :

static int[] a = new a[10]; static {

for(int k = 0; k < a.length; k++) a[k] = k * k;

}

., , @ ,

g ( , (

.

43

3.2.8 7 Complex

C @ +

, +, +, . ' 3.4. C Complex

class Complex {

private static final double EPS = le-12; // .2]789:; 2<]/9gO7; private double re, im; // ?8>97= > R7/R= ]=9:/7=

// ˆ2:/4/ P279:4NP:24/

Complex(double re, double im) { this, re = re; this.im = im;

}

Complex(double re){this(re, 0.0); } Complex(){this(0.0, 0.0); } Complex(Complex z){this(z.re, z.im) ; }

// hO:26/ 629:N0N

public double getRe(){return re;} public double getlmf){return im;}

public Complex get(){return new Complex(re, im);} public void setRe(double re){this.re = re;} public void setlm(double im){this.im = im;} public void set(Complex z){re = z.re; im = z.im;}

// h26Ng; 8 =4\NRO7: P2R0gOP972\2 ]/9g=

public double mod(){return Math.sqrt(re * re + im * im);} public double arg()(return Math.atan2(re, im);}

// •O4O@84P=: 68>97O ]/9g2?

public boolean isReal(){return Math.abs(im) < EPS;} public void pr(){ // i/@86 7= OP4=7

System.out.println(re +(im < 0.0 ? "": '"+") + im + "i");

}

//•O4O@/37=]O775 RO:268@ Pg=9N Object public boolean equals(Complex z){

return Math.abs(re -'z.re) < EPS && Math.abs(im - z.im) < EPS;

}

public String toString(){

return "Complex: " + re + " " + im;

}

//hO:26/, j2 4O=g83Nn:; 20O4=_8ƒ +=, -=, *=, /= public void add(Complex z){re += z.re; im += z.im;} public void sub(Complex z){re -= z.re; im -= z.im;} public void mul(Complex z){

double t = re * z.re - im * z. im; im = re * z.im + im * z.re; re = t;

}

public void div(Complex z){ double m = z.mod();

double t = re * z.re - im * z.im; im =(im * z.re - re * z.im) / m;

44

re = t / m;

}

// hO:26/, j2 4O=g83Nn:; 20O4=_8ƒ +, -, *, / public Complex plus(Complex z){

return new Complex(re + z.re, im + z im);

}

public Complex minus(Complex z){

return new Complex(re - z.re, im - z.im);

}

public Complex asterisk(Complex z){ return new Complex(

re * z.re - im * z.im, re * z.im + im * z re);

}

public Complex slash(Complex z){ double m = z.mod();

return new Complex(

(re * z.re - im * z.im) / m,(im * z.re - re * z.im) / m);

}

}

// •O4O@84/R2 42<2:N Pg=9N Complex public class ComplexTest{

public static void main(Stringf] args){ Complex zl = new Complex(),

z2 = new Complex(1.5),

z3 = new Complex(3.6, -2.2),

z4 = new Complex(z3);

System.out.printlnf); // a=g/f=bR2 0242^78> 4562P System.out.print("zl = "); zl.pr(); System.out.print("z2 = "); z2.pr(); System.out.print("z3 = "); z3.pr(); System.out.print("z4 = "}; z4.pr();

System.out.println(z4);

// •4=_nb RO:26 toString()

z2.add(z3);

 

System.out.print("z2 + z3 = "}; z2.pr(); z2.div(z3);

System.out.print("z2 / z3 = "); z2.pr(); z2 = z2.plus(z2);

System.out.print("z2 + z2 = "); z2.pr(); z3 = z2.slash(zl);

System.out.print("z2 / zl = "); z3.pr();

}

}

: . 3.3 f .

45

= 3.3 – ? ComplexTest

3.3 "9 ) 9 4

3.3.1.

@ , @ , (

.

3.3.2? :

:

5.= , i f -

. : i

.

6.= , i f -

. :

i .

0 :

1.= , i f +

(. :

. E g

46

2.= , i f +

. :

(. E

g

3.= , i f +

. :

(. E

g

4.= , i f +

@. :

i. E g

5.= , i f +

. :

( . E g

6.= , i f +

(. :

i . E g

7.= , i f +

. : i

. E g

8.= , i f +

. :

i (. E

g

9.= , i f +

@. :

i (. E

g

10.= , i f +

. :

47

i . E g

3.3.3.+ .

3.3.4? .

3.4 !, #",

3.4.1; .

3.4.2.

3.4.3C .

3.4.4; .

3.4.5C , i ( @

.

3.4.6?, i

(5 g. ), ( ( @

.

3.5 7] , # 4 )

3.5.1G + ? f ?

3.5.2G + ? f

?

3.5.3?

3.5.4E ( Object Java?

3.5.5G Java?

3.5.6E Java?

3.5.7= ( ( Complex Java.

3.5.8G Main Java?

3.5.9G Java?

3.5.10? , f

java?

48

4 ? 8 8 8 ? 4 3 @ 8` @

4.1 ( 4

: @ + Java.

4.2 " , ( ( 4-, ",9 ! ,

Java API . = @ Java @ @

(packages). ? Java @ . C

( +

(subpackages). @f

.

? + g class (@ - ), i

@@ , @ + . C ( @f (namespace). D

f, i , +

. 0 ( ,

. ; , ( , +

( . G i

, '

@f ' : . . ; '

f ' (fully qualified name).

' @ i , i

private, protected public i , " "

.

G i ( +

private, protected, public, , , @f

(default access), , (

- - ( . '

( @ - i

+ public, , , public,

g .

3 1( , ,9 1(

‘ g Java-+

package ' ;, :

49

package mypack;

; @f ' mypack

, +, mypack. ' @@ ( +,

@ f .

0' @f ' .

' , , subpack, g

+ ;

package mypack.subpack;

+ + ( g

subpack mypack. ( :

package mypack.subpack.sub;

. . (.

. package ' ; '

g +, ( f

.

C Java ( ( ' mypack, a subpack, class-+

- .

' 5, : mypack.A, mypack.subpack.B.

C ( @f

(unnamed package), f (current working directory) + . ; class-+ (

( , i Java-+. m ( i

( . m g i

. :, Java 2 API f

java, javax, org.omg. ' Java

applet, awt, beans, io, lang, math, net, rmi, security, sql, text, util

( . D @ , ,

’0' + java.awt color, datatransfer, dnd, event, font, geometry, im,image, print.

4.2.1 3 " 9 9 -7( ," 17

( ( , public g . ( i

50

i, protected ,

, .

? , . m g . 4.1.

; 4.1 – '

 

C

'

 

'

?

private

 

+

 

 

 

 

"package"

 

+

+

 

 

 

protected

 

 

+

 

+

*

public

 

+

+

 

+

+

.

 

protected- (

@.

4.2.2 ! 17 ," , 1( ,"

' import: f

import , , ' . >

, import g.

; ( f + import -

f ' ,

*. D f

. :, (

import p1.*;

: f, i ( ,

+ public.

' java.lang f (, '

. 0 g

import, .

. import

include – @ f +.

4.2.3 ( +( 4

? object. 5 f

. D f

( (multiple inheritance).

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]