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

Java

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

Java
(packages), .
Java .
, .
. ,
, ,
Sun.
,
.
.

Rendered by www.RenderX.com

ii

5. . ................................................................................................... 1
1. ......................................................................................................................... 1
2. ............................................................................................................................. 2
2.1. . . ........................................................... 2
2.2. ....................................................................................2
2.3. () ........................................................................... 3
3. ............................................................................................................................. 4
3.1. ................................................................................................... 5
3.2. ...................................................................5
3.3. ............................................................................................. 7
3.3.1. ..................................................................................... 8
3.3.2. - ......................................................................................9
3.3.3. ...................................................................12
3.4. ................................................................................14
4. ............................................................................................15
4.1. "" (Shadowing) ........................................................... 16
4.2. "" (Obscuring) .......................................................... 17
5. ...................................................................................... 17
6. .................................................................................................................... 20
7. ...................................................................................................20

Java

Rendered by www.RenderX.com

. 1 24

5. .
.
1. ........................................................................................................................ 1
2. .............................................................................................................................2
3. ............................................................................................................................ 4
4. ........................................................................................... 15
5. ......................................................................................17
6. ................................................................................................................... 20
7. .................................................................................................. 20

1.
(names) (declared)
"", "", "" ( -
, , ). , Java
:

(member) :

:
-

, ,
. , ,
, .
Java

Rendered by www.RenderX.com

. 2 24

, (packages) Java - ,
,
.
.
.

2.
2.1. . .
(simple), (
), (qualified),
, .
.
(, , ) (members).
, , ,
, .
(type).
, ,
. ,
, , , .
, Java - java.lang ( ,
lang, java).
, reflection,
. reflect, , , - java.lang.reflect.
, Object, String, Point.
,
, . , java.lang.Object,
java.lang.reflect.Method com.myfirm.MainClass. :
com, - myfirm,
myfirm - MainClass. com.myfirm -
, MainClass, MainClass - .
- com.myfirm.MainClass.
,
( ). ,
,
. . , toString(),
PI, InnerClass.
. , ref.toString(),
java.lang.Math.PI, OuterClass.InnerClass.
.

2.2.
, ,
(, ) . ,
Java

Rendered by www.RenderX.com

. 3 24

, - .
.
-, (declaration) .
,
.
-,
, ,
. :
country.getCity().getStreet();

getStreet , ,
,
getCity(). , country.getCity - .
, (label).
, , ,
.
num:
for (int
int n
while
if

num = 2; num <= 100; num++) {


= (int)Math.sqrt(num)+1;
(--n != 0) {
(num%n==0) {
continue num;

}
}
System.out.print(num+" ");
}

, 100:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

, num,
while for.
, , , ..
. , ,
, ,
.
- .

2.3. ()
,
, , ,
(scope). , ,

Java

Rendered by www.RenderX.com

. 4 24

, ,
, - .
,
class Point {
int x,y;
int getX() {
return x; //
}
}
class Test {
void main() {
Point p = new Point();
p.x=3; //
}
}

, x .
.
, , .
.
, , .

3.
Java (packages).
, .
, ,
, . -,
, " ". -,
. , ,
,
"" , . ,
. ,
, ,
, .

.
, ,
. ,
.

Java

Rendered by www.RenderX.com

. 5 24

3.1.
, (
). ,
.
.
, JDK 1.0 java : applet, awt, io, lang, net, util;
. java.awt image 46
.

. , Object java.lang
java.lang.Object, image java.awt - java.awt.image.

, , ,
,
. , space.sun, space.sun.ray, space.moon factory.store
"" ,
.

3.2.
.
, ,
.
java,
, , , applet, awt, io, lang, net, util.
, ,
Sun, Moon Test, space.sunsystem.
space,
, - sunsystem, .
, .java, -
.class. , sunsystem
:
Moon.java
Moon.class
Sun.java
Sun.class
Test.java
Test.class

,
space.sunsystem.Moon
space.sunsystem.Sun
space.sunsystem.Test
Java

Rendered by www.RenderX.com

. 6 24


space\sunsystem\Moon.java
space\sunsystem\Sun.java
space\sunsystem\Test.java

- .class-. ,
. () ( Windows \).
,
.
, Java space\sunsystem
Test, , -
. , Java,
, .
, , Java , ,
.
,
, .
, (
). ,
. ,
, . ,
, ,
Java.
Java SDK (, ..),
, - ,
, , , . ,
, -
, Java .
, .
,
, .
.
Java ,
classpath. , path
, Java-.
, .
1.1 ZIP JAR (Java ARchive) - ,
ZIP Java.
, classpath :
.;c:\java\classes;d:\lib\3Dengine.zip;d:\lib\fire.jar

""
. Java
Java

Rendered by www.RenderX.com

. 7 24

. ,
( ).
, . ,
.
, .
, ,
classpath , ,
, .
, - , Java
.
.
,
.
, , -, Java-, , , classpath.
,
. ,
. ,
.java-, ,
. ,
/ / .

3.3.
(complication unit) - .java-
. :

import-;

,
. , ,
( - ).
import- ,
"" . .
, .
" " ,
, ,
. , , ,
. , .class-
.
, ..
Java ,
.
3 .
Java

Rendered by www.RenderX.com

. 8 24

3.3.1.
- .
package, .
, ( ) java/lang/Object.java :
package java.lang;

lang, java, ,
Object, .
java.lang.Object.
,
. Java. , ,
.
, :
class Simple {
public static void main(String s[]) {
System.out.println("Hello!");
}
}

.
Java
, .
, ,
. .
,
. , ,
:

, :
package space.star;
class Sun {
}

, , Java-,
space star ( space.star)
Java.

Java

Rendered by www.RenderX.com

. 9 24

, -
. , ,
Java .
, java.lang java.io, java,
Java-, ,
.

3.3.2. -
, - ,
. ,
. ,
, .
(, JDK1.2
com.sun.image.codec.jpeg),
,
.
import-,
.
:

, , ,
. ,
, . ,
, ,
,- ,
. , ,
-
.
,
. , ,
.
.
, , import
. :
import java.net.URL;

,
URL java.net. ,
, .
, , .
,
.

Java

Rendered by www.RenderX.com

. 10 24

, ,
.
import java.awt.*;

, java.awt,
. , , .
,
. , .
:
//
import java.awt.image;

, java.awt.image
, , image.ImageFilter.
, , java.awt
image.
,
import java.awt.*;

java.awt.image, .
java.lang ,
, .
, -
.
.
- :
import java.awt.*;
import java.awt.Point;

, - ,
? - ?
,
. :

, ;

, , ,
;

, , .

, ,
, .

Java

Rendered by www.RenderX.com

. 11 24

:
//
package my_geom;
import java.awt.Point;
class Point {
}

, Point
java.awt.Point,
, , .
:
package my_geom;
class Point {
}

:
package my_geom;
import java.awt.Point;
class Line {
void main() {
System.out.println(new Point());
}
}

- , my_geom.Point
java.awt.Point, ? :
java.awt.Point[x=0,y=0]

, Point .
: my_geom.Point.
, ""
,
.
. " ",
, ""
,
.
( ) .

Java

Rendered by www.RenderX.com

. 12 24

:
package my_geom;
import java.awt.*;
class Line {
void main() {
System.out.println(new Point());
System.out.println(new Rectangle());
}
}

:
my_geom.Point@92d342
java.awt.Rectangle[x=0,y=0,width=0,height=0]

Point ,
java.awt. Rectangle,
, java.awt.
:
package my_geom;
import java.awt.*;
class Point {
}

,
.
,
. , ,
,
. ,
, , ,
.

3.3.3.

. ,
.
class, - interface.
, . :
package first;

Java

Rendered by www.RenderX.com

. 13 24

class FirstClass {
}
interface MyInterface {
}

- , .
, .
Java .
.
, public:
package second;
public class OpenClass {
}
public interface PublicInterface {
}

.
, .
Java 1.1 (inner) ,
.
,
, .
, , Java
.
,
, (, ,java), :

public, , , ;

, ,
.
, public ,
. -public ,
,
.
,
.

, , public . .
, space.sun.Size space\sun\Size.java,

Java

Rendered by www.RenderX.com

. 14 24

- Size.class .
Java.
, ,
. , :
package test;
/*
* Human,
*/
class Human {
String name;
Car car; //
}
/*
* Car,
*/
class Car {
String model;
Human driver; // ,
}

, Car , .
, .
.

3.4.
Java ,
, ,
, .
.
.
Java .
, -,
, , , ,
company.com, ,
: com.company.
, , , ..
, ASCII-
:

com, edu, gov, mil, net, org ( );

, , ru, su, de, uk .

Java,
:
Java

Rendered by www.RenderX.com

. 15 24

, ,
;

,
;

, .

, :
com.sun.image.codec.jpeg
org.omg.CORBA.ORBPackage
oracle.jdbc.driver.OracleDriver

, , , Java-
-, .
,
.

4.
,
.
,
.
- ,
. ,
. java.lang
lang.

.
( ) ,
. ,
,
.
- ,
. ,
.
, super
this, .
,
.

, . ,
.

Java

Rendered by www.RenderX.com

. 16 24

int x;
for (int i=0; i<10; i++) {
int t=5+i;
}
// t ,
// ,
//
// x ,
//

,
.

4.1. "" (Shadowing)


Human () age ().
setAge(), .
setAge() ,
age ( ).
, setAge() age=age,
, - . , ,
,
. ,
.
-, , - .
- , .
, .
.
,
"" (shadowing) .
, .
, .
this (
). this ,
. setAge() Human
this , .
:
class Human {
int age;//
void setAge(int age) {
this.age=age; // !
}
}

Java

Rendered by www.RenderX.com

. 17 24

, - ,
this
. ,
, -
.

4.2. "" (Obscuring)


,
, .
, :
import java.awt.*;
public class Obscuring {
static Point Test = new Point(3,2);
public static void main (String s[]) {
print(Test.x);
}
}
class Test {
static int x = -5;
}

main() Test Oscruing


, - Test.
x, java.awt.Point Test.
3, .
, , . ,
,
.
"" (obscuring).
,
Java.

5.
, Java,
, , ,
Java.
Java , .
:

Java

Rendered by www.RenderX.com

. 18 24

( );

-;

.;

.
.
, ,
. ,
.
.
java javax (Java eXtension) Sun
Java.
"" (obscuring)
, .
.
,
. , ,
, "".
, , :
Human
HighGreenOak
ArrayIndexOutOfBoundsException

( - , ,
).
,
. able:
Runnable
Serializable
Cloneable

"" (obscuring) ,
() ,
- .
,
. , ,
.
:

Java

Rendered by www.RenderX.com

. 19 24

,
get set . , size
getSize() setSize().

, , length(), , String.

, , is, ,
isVisible() .

, F, toF(), ,
toSting(), .

, , , ,
Java, , .
, , -
, ,
. , , name
Human size Planet.
"" (obscuring) .
, final.
, , .
, :
PI
MIN_VALUE
MAX_VALUE

,
:
COLOR_RED
COLOR_GREEN
COLOR_BLUE

, ,
. , , ,
. , ( cp
ColorPoint) (buf buffer).
:
byte b;
char c;
int i,j,k;
long l;
float f;
double d;
Object o;
String s;
Exception e; // , Java
Java

Rendered by www.RenderX.com

. 20 24

-
-.

6.
.
,
, , .
(members),
. (
) .
, Java
,
. ( ).

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

.

, , .
.

.

7.
5-1.

Java ? ?
a.) :

(member) :
-

Java

Rendered by www.RenderX.com

. 21 24

:
-

,
.
5-2.

, ,
?
MyClass
MyClass.name
MyClass.name.toString()
MyClass.name.toString().hashCode()

a.) MyClass . MyClass.name, MyClass.name.toString


. hashCode .
5-3.

?
a.) ,
.

5-4.

,
, test.first.Start,
c:\Java\programs\test\first\Start.java?
a.) ,
. ,
c:\Java>javac programs\test\first\Start.java

c:\Java\programs\test>javac first\Start.java

5-5.

Java
, ?
a.) JVM , ,
..
c:\Java\programs>java test.first.Start

Java

Rendered by www.RenderX.com

. 22 24

5-6.

, 2 ,
c:\ ?
a.) , classpath .

5-7.

.
, Point test.demo,
java.awt.Point
java.net?
a)
package test.demo;
import java.awt.Point;
import java.net.*;
b)
import java.awt.*;
import java.net.*;
package test.demo;
c)
package test.demo;
import java.net.*;
import java.awt.*;
d)
package test.demo.*
import java.net.*;
import java.awt.*;

a.) a) , java.awt.Point
Point, .
b) , . d) ,
( )
. c) .
5-8.

? , ?
class Box {
private int weight=0;
public int getWeight() {
return weight;
}
void setWieght(int weight) {
weight=weigth;
}

Java

Rendered by www.RenderX.com

. 23 24

a.) , setWeight()
.
,
. , ,
.
5-9.


?
public class flat{
private int floor_number;
private int r; //
public int rooms() {
retun r;
}
public int GetFloorNumber() {
return floor_number;
}
}

a.) :

, Flat.

floor_number ,
, floorNumber.

r ,
, , rooms roomsNumber

rooms , ,
, getRoomsNumber.

GetFloorNumber ,
getFloorNumber.

,
. - (,
),
,
.

Java

Rendered by www.RenderX.com

Java ()

Java

Rendered by www.RenderX.com