Вы находитесь на странице: 1из 34

Java

6.
20 2003
:
( Sun ) <vyazovick@itc.mipt.ru>
( Sun ) < gene@itc.mipt.ru>
Copyright 2003 Sun , , .

, Java-
.
Java. ,
- ,
Java.
, ( ),
. main,
Java-,
, .

Rendered by www.RenderX.com

ii

6. ........................................................................................ 1
1. ........................................................................................................................ 1
2. .................................................................................................. 2
2.1. ........................................................... 2
2.2. Java.............................................................................. 5
3. ....................................................................................................... 9
3.1. .................................................................................................... 9
3.2. ...........................................................................................................10
3.3. ............................................................................................... 11
3.4. ........................................................................................... 12
3.5. ................................................................................. 16
3.6. ...................................................................................................22
4. ............................................................................. 24
4.1. main............................................................................................................ 24
4.2. ............................................................................................. 25
4.3. ....................................................................................... 27
5. .................................................................................................................... 28
6. ...................................................................................................28

Java

Rendered by www.RenderX.com

. 1 33

6.
.
1. ....................................................................................................................... 1
2. ................................................................................................. 2
2.1. ................................................................. 2
2.2. Java.................................................................................... 5

3. ...................................................................................................... 9
3.1. .......................................................................................................... 9
3.2. .................................................................................................................10
3.3. ..................................................................................................... 11
3.4. ................................................................................................. 12
3.5. ....................................................................................... 16
3.6. .........................................................................................................22

4. .............................................................................24
4.1. main..................................................................................................................24
4.2. ................................................................................................... 25
4.3. .............................................................................................27

5. ................................................................................................................... 28
6. .................................................................................................. 28

1.
,
Java - . ,
, .
.
-
. Java,
, .
.
Java.
. ,
.

Java

Rendered by www.RenderX.com

. 2 33

2.
,
, , . ,
: public, , private,
, .

, , .

2.1.
,
: , ""
. , Human () age ( ),
-
, ,
. age private.
, ,
.
- , , .
,
. , Human age ,
, public.
public class Human {
public int age;
}

, , ,
, ,
.
, ,
. int double,
, Human age, ,
, , ,
:
Human h = getHuman();
int i=h.age; // !!

-
.
, ( , )
. ,
, .
, public, ,

Java

Rendered by www.RenderX.com

. 3 33

(, , , )
.
, private,
, :
public class Human {
private int age;
// , age
public int getAge() {
return age;
}
// , age
public void setAge(int a) {
age=a;
}
}

,
, Human,
private , ,
,
.
:
Human h = getHuman();
int i=h.getAge(); //

, age:
public class Human {
// double
private /*int*/ double age;
//
public int getAge() {
return (int)Math.round(age);
}
public void setAge(int a) {
age=a;
}
// double
public double getExactAge() {
return age;
}
Java

Rendered by www.RenderX.com

. 4 33

public void setExactAge(double a) {


age=a;
}
}

, , ,
. , ,
.
.
Human h = getHuman();
int i=h.getAge(); //

, i .
.
, , ,
:
Human h = getHuman();
double d=h.getExactAge(); //

, ,
.
? ,
, (
, public). ,
, ,
, .

, :
(accessors) (getters) (setters). ,
. ,
,
, .
, , .
. ,
. ,
:
public void setAge(int a) {
(if a>=0) {
age=a;
}
}

Java

Rendered by www.RenderX.com

Java

. 5 33

age .
( ,
, , , ,
;
Java).
. ,
, , ,
. - ,
persistent storage ( , , )
/ . ,
, ,
, , ,
.
. ,
, ,
, .
,
.
, .
, - .
,
, , , , ,
.
, ,
, ,
, .
,
, . Java
2 (public private), 4 .
Java .

2.2. Java
,
.
.
Java :

( ) ;

(, , );

, ,
, .

Java

Rendered by www.RenderX.com

. 6 33

4 . :

public;

private;

protected;

3 ,
(default).

. ( )
- ,
. Java ,
,
, .
, protected . ,
,
.
,
. protected
,
-, .
protected ,
. , (
- ):
private
(none) default
protected
public


.
,
.

, ( ,
public, ,
).

( ) .
: public .
public, , .
public, :
, .

, ,
(, ).

. 4
. public.

Java

Rendered by www.RenderX.com

Java

. 7 33

4 .
private- -, .
, , .

( ),
, , .
.
.
:
public class Wheel {
private double radius;
public double getRadius() {
return radius;
}
}

radius , getRadius()
.
:
package first;
// Parent
public class Parent {
}
package first;
// Child Parent,
//
class Child extends Parent {
}
public class Provider {
public Parent getValue() {
return new Child();
}
}

getValue() Provider ,
first, public. ,
Child, .
:
package second;

Java

Rendered by www.RenderX.com

. 8 33

import first.*;
public class Test {
public static void main(String s[]) {
Provider pr = new Provider();
Parent p = pr.getValue();
System.out.println(p.getClass().getName());
// (Child)p - !
}
}

:
first.Child

, Test
Child, , Parent.
. , "" ,
.
:
public class Point {
private int x, y;
public boolean equals(Object o) {
if (o instanceof Point) {
Point p = (Point)o;
return p.x==x && p.y==y;
}
return false;
}
}

Point ,
. , - private.
equals() , ,
Point, (
intsanceof) .
, -
private- !
, , private
, ,
.
Java .

Java

Rendered by www.RenderX.com

. 9 33

3.
.
.

3.1.
.
. public, - .
final.
. . String , , final-.
class, Java-. ,
:
class A {}

, .
.
, (
), , .
, - .
extends,
( ) -final .
. extends ,
Object. extends Object .
class Parent {} // = class Parent extends Object {}
final class LastChild extends Parent {}
// class WrongChild extends LastChild {} // !!

final- .
A extends B, A
B.
A B :

A B;

C, B, A
C ( ).

.
, ,
:
Java

Rendered by www.RenderX.com

. 10 33

//
class A extends B
class B extends C
class C extends A


{}
{}
{} // !

implements,
( ,
) :
public final class String implements Serializable, Comparable {}

, .
, .
implements , ,
.
,
.

3.2.
(members) :

( );

, - - .
,
.
.
4 . ,
.
- , .
. , .
.
,
- ( Object - ,
)
. , ,
.

Java

Rendered by www.RenderX.com

. 11 33

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

3.3.
.
3 , ,
.
final, ,
. final-
- :
final double PI=3.1415;

final- .
,
, :
final long creationTime=System.currentTimeMillis();

.
- transient volatile. .
.
:
int a;
int b=3, c=b+5, d;
Point p, p1=null, p2=new Point();

.
.
.
.
.
int y=x;
int x=3;

:
class Point {
int getX() {return x;}

Java

Rendered by www.RenderX.com

. 12 33

int y=getX();
int x=3;
public static void main (String s[]) {
Point p=new Point();
System.out.println(p.x+", "+p.y);
}
}

:
3, 0

, ,
:

0;

false;

, y
getX(), x, 0.
x 3.

3.4.
. :

( );

void;

( );

throws-.

.
3 .
.
, final, ,
. , final-,
private- final.
, native. , ,
Java. (C/C++, Fortran
..) (, DLL
Windows). JNI (Java Native Interface),
native-.
Java,
, .
Java,
, . , Java-
Java

Rendered by www.RenderX.com

. 13 33

, , .
JNI .

( , native-),
, , , -"",
Java- JNI, ,
.
, synchronized,
, .
( )
; , .
, void.
.
.
.
.
. ,
.
:
// void calc (double x, y); - !
void calc (double x, double y);

, .
. , ,
, .
final .
( ,
).
public void process(int x, final double y) {
x=x*x+Math.sqrt(x);
// y=Math.sin(x); - , .. y - final!
}

,
.
(signature) .
(, , ).
,
.
,
class Point {
void get() {}

Java

Rendered by www.RenderX.com

. 14 33

void get(int x) {}
void get(int x, double y) {}
void get(double x, int y) {}
}

.
:
void get() {}
int get() {}
void get(int x) {}
void get(int y) {}
public int get() {}
private int get() {}

, , ,
. , ,
.
, ,
:
//
class Test {
int get() {
return 5;
}
Point get() {
return new Point(3,5);
}
void print(int x) {
System.out.println("it's int! "+x);
}
void print(Point p) {
System.out.println("it's Point! "+p.x+", "+p.y);
}
public static void main (String s[]) {
Test t = new Test();
t.print(t.get()); // !
}
}

get()
. main,
, .
print() ( , ,

Java

Rendered by www.RenderX.com

. 15 33

), , ,
get(), .
, . ,
,
. ,
,
.
, ,
,
.
, ,
.
, throws-.
Java .
:
public final java.awt.Point createPositivePoint(int x, int y)
throws IllegalArgumentException
{
return (x>0 && y>0) ? new Point(x, y) : null;
}

, . ,
" ". native-
, .
, ,
.
,
:
public void empty() {}

, void,
return-.
, ,
. ,
:
//
public int get() {
if (condition) {
return 5;
}
}

Java

Rendered by www.RenderX.com

. 16 33

, return-,
.
:
public int get() {
if (condition) {
return 5;
} else {
return 3;
}
}

, , return,
(
" ").
( void)
return . ,
:
public void calculate(int x, int y) {
if (x<=0 || y<=0) {
return; // ,
}
... //
}

return ( / )
. , ,
.

3.5.
.
. , -,
( ). ,
. ,
. , -
, ,
. ,
:
public class Human {
private int age;
protected Human(int a) {
age=a;
}

Java

Rendered by www.RenderX.com

. 17 33

public Human(String name, Human mother, Human father) {


age=0;
}
}

,
, . throws-.
, -
.
, .

( ).
(, , - ,
, )
, . ,
, .
return- .
,
.
.
, ,

. -
,
super.
public class Parent {
private int x, y;
public Parent() {
x=y=0;
}
public Parent(int newx, int newy) {
x=newx;
y=newy;
}
}
public class Child extends Parent {
public Child() {
super();
}
public Child(int newx, int newy) {
super(newx, newy);

Java

Rendered by www.RenderX.com

. 18 33

}
}

, super,
. ,
. 2
,
( , , , ).
.
new, ,
, .
, , .
. ,

. , ,
Object, . (
, ), ,
JVM , .
.
, Object,
. ,

. , ,
new, .
, new
, .
:
public class GraphicElement {
private int x, y; //
public GraphicElement(int nx, int ny) {
super(); // Object
System.out.println("GraphicElement");
x=nx;
y=nx;
}
}
public class Square extends GraphicElement {
private int side;
public Square(int x, int y, int nside) {
super(x, y);
System.out.println("Square");
side=nside;
}
}
Java

Rendered by www.RenderX.com

. 19 33

public class SmallColorSquare extends Square {


private Color color;
public SmallColorSquare(int x, int y, Color c) {
super(x, y, 5);
System.out.println("SmallColorSquare");
color=c;
}
}

:
GraphicElement
Square
SmallColorSquare

super .
.
(super()).
, super
( ),
.
, , -, ,
. , .
, ,
, - ,
.
,
, . ,
. public-
, . ,
, ,
. ,
( , ),
:
/*
* .
*/
public class One {
//
// Object
// .
}
/*
Java

Rendered by www.RenderX.com

. 20 33

* .
*/
public class Two {
// Second.
// new Second() !
public Second(int x) {
}
}
/*
* .
*/
public class Three extends Two {
public Three() {
super(1); // super
}
public Three(int x) {
super(x); // super
}
}

,
super, this - ,
.
:
public class Vector {
private int vx, vy;
protected double length;
public Vector(int x, int y) {
super();
vx=x;
vy=y;
length=Math.sqrt(vx*vx+vy*vy);
}
public Vector(int x1, int y1, int x2, int y2) {
super();
vx=x2-x1;
vy=y2-y1;
length=Math.sqrt(vx*vx+vy*vy);
}
}

, ,
:

Java

Rendered by www.RenderX.com

. 21 33

public class Vector {


private int vx, vy;
protected double length;
public Vector(int x, int y) {
super();
vx=x;
vy=y;
length=Math.sqrt(vx*vx+vy*vy);
}
public Vector(int x1, int y1, int x2, int y2) {
this(x2-x1, y2-y1);
}
}

,
. ,
(, ),
- .
,
.
,
, . this
super, ( )
. , ,
, .
new Test(0) :
Test()
Test(int x)

.
private.
, .
private . :

private- ,
this,
- ;

, ,
Math;

Singleton,
,
-private .

Java

Rendered by www.RenderX.com

. 22 33

3.6.
,
. -
.
public class Test {
private int x, y, z;
//
{
x=3;
if (x>0)
y=4;
z=Math.max(x, y);
}
}

,
, (, ,
-).
- ,
, .
,
.
:

(
), ;


, ;

,
. .

. -, ,
,
.
-, final. -
. :

(
);

,
. ,
this, final-,

Java

Rendered by www.RenderX.com

. 23 33

this- super,
.

:
public class Test {
{
System.out.println("initializer");
}
int x, y=getY();
final int z;
{
System.out.println("initializer2");
}
private int getY() {
System.out.println("getY() "+z);
return z;
}
public Test() {
System.out.println("Test()");
z=3;
}
public Test(int x) {
this();
System.out.println("Test(int)");
// z=4; - ! final-
}
}

new Test() :
initializer
getY() 0
initializer2
Test()

, y getY(),
final- z, .
y 0, z
3, .
new Test(3) :
initializer
getY() 0
initializer2
Java

Rendered by www.RenderX.com

. 24 33

Test()
Test(int)

4.
Java.
, Java.

4.1. main
,
. , Java, . ,
, .
, C/C++, main().
:
public static void main(String[] args) {
}

static .
main(), . ,
C . Java
System.exit(), int.
main() .
, .
package test.first;
public class Test {
public static void main(String[] args) {
for (int i=0; i<args.length; i++) {
System.out.print(args[i]+" ");
}
System.out.println();
}
}

,
main(). , ,
(.class .java),
( ), .
, , .
Test.java,
test\first, :

Java

Rendered by www.RenderX.com

. 25 33

javac test\first\Test.java

:
java test.first.Test

, :
java test.first.Test Hello, World!

:
Hello, World!

4.2.
Java
.
-, - ?
, , .
, .
- ?
int x=3;
process(x);
print(x);

, :
public void process(int x) {
x=5;
}

?
, Java.
,
,
. process(), x,
.
5 . , 3,
.
-
:
public int double(int x) {
return x+x;
}
Java

Rendered by www.RenderX.com

. 26 33

public void test() {


int x=3;
x=double(x);
}

.
public void process(Point p) {
p.x=3;
}
public void test() {
Point p = new Point(1,2);
process(p);
print(p.x);
}

,
. process()
, , . ,
,
. 3.
Java .
, , :
public void process(Point p) {
p = new Point(4,5);
}
public void test() {
Point p = new Point(1,2);
process(p);
print(p.x);
}

process()
, p, ,
1. , ,
.
,
final. ( ,
) ,
final ,
. , final Point p p.x= 5
( p=new Point(5, 5)).

Java

Rendered by www.RenderX.com

. 27 33

4.3.
(overloading)
. ,
.
, ,
, - , Java
,
.
.
, , ,
,
. ,
Parent Child, Child Parent, :
void process(Parent p, Child c) {}
void process(Child c, Parent p) {}

, , .
process(new Child(), new Child());

, . ,
:
process(Object o) {}
process(String s) {}

:
process(new Object());
process(new Point(4,5));
process("abc");

, ,
. ,
String "", , Object.
, String Object,
. ,
, .
.
.
, .
, , ,
:
process((Parent)(new Child()), new Child());

Java

Rendered by www.RenderX.com

. 28 33

//
process(new Child(),(Parent)(new Child()));

null:
process((Parent)null, null);
//
process(null,(Parent)null);

5.
Java
.
.
.
,
.
,
, .
4 ,
.
. ,
.
.
. (members)
. , .
.
, .
, ,
. .
main,
. , ,
.
Java .

6.
6-1.

?
a.) public protected, (
).

6-2.

, ,
.
?

Java

Rendered by www.RenderX.com

. 29 33

a.) private.
, , .
.
.
6-3.

User .
e-mail , , , ,
, . ?
public class User {
public String login; // e-mail
private String password; //
}

a.) ,
.
login ,
. login
, login.
:
public class User {
private String login; // e-mail
private String password; //
public String getLogin(){
return login;
}
}

6-4.

?
public class Test {
private int id;
public Test(int i) {
id=i;
}
public static boolean test(Test t, int id) {
return t.id==id;
}
}

a.) . test Test, ,


id .
6-5.

? ?

Java

Rendered by www.RenderX.com

. 30 33

a.) ( ):
1. (public, abstract, final)
2. class
3. extends
4. implements

5.
( ):

6-6.

( )

,
?
a.) .
.

6-7.

? ?
a.) ( ):
1. ( public|private|protected
static, final, native, synchronized)
2. void,
3.
4.
5. throws
,
( void), (
, ).

6-8.

.
double float
. float
(byte), (int). double int long .
, ?
a.) , 4 :
byte round(float x) { ... }
int roundToInt(float x) { ... }

Java

Rendered by www.RenderX.com

. 31 33

int round(double x) { ... }


long roundToLong(double x) { ... }

, ,
,
.
, 2 3 ,
. .
, , , round(1f)
1 , , .
. float
int round((double)1f), 3
, .
6-9.

?
a.) , .
, .
(

), ,
.

6-10. ?
class Test
private
private
private

{
long id=getId();
String name=getName();
String login;

public Test(int domain) {


login=domain+ +name;
System.out.println(login);
}
private static long getId() {
int id = 3;
System.out.println(id);
return id;
}
private String getName() {
String name=name+getId();
System.out.println(name);
return name;
}
}

Java

Rendered by www.RenderX.com

. 32 33

a.) :
3
3
name3
5 name3

id ( 3 ).
name,
getId() ( 3), name3.
5 name3.
6-11. main?
a.)

public static void main(String[] args)

6-12. String,
?
a.) , String, , , .

Java

Rendered by www.RenderX.com