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

Java

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


Java Java,
, , ,
. , Java
.
,
.
, .

Rendered by www.RenderX.com

ii

8. Java................................................................................. 1
1. .......................................................................................................................... 1
2. ....................................................................................................1
3. this super...........................................................................................5
4. abstract................................................................................................. 8
5. ...................................................................................................................10
5.1. ................................................................................... 10
5.2. ......................................................................................11
5.3. ................................................................................... 13
6. ................................................................................................................ 14
6.1. .......................................................................................................................15
6.2. .................................................................................................................. 17
6.3. ..................................................................................... 20
7. .................................................................................................................... 21
8. ...................................................................................................22

Java

Rendered by www.RenderX.com

. 1 27

8.
Java
.
1. ......................................................................................................................... 1
2. ...................................................................................................1
3. this super.......................................................................................... 5
4. abstract................................................................................................ 8
5. .................................................................................................................. 10
5.1. ......................................................................................... 10
5.2. ............................................................................................11
5.3. ......................................................................................... 13

6. ............................................................................................................... 14
6.1. .............................................................................................................................15
6.2. ........................................................................................................................ 17
6.3. ........................................................................................... 20

7. ................................................................................................................... 21
8. .................................................................................................. 22

1.
,
, .
Java,
- .

2.
,
. :
class Human {

Java

Rendered by www.RenderX.com

. 2 27

private String name;


}

name
Human, , -
.
. ,
( Human, ). ,
- ,
. " " " ".
static:
class Human {
public static int totalCount;
}

, ,
:
Humans.totalCount++; //

:
Human h = new Human();
h.totalCount=100;

. ,
h Human,
:
Human.totalCount=100;

, :
Human h = null;
h.totalCount+=10;

null, .
. ,
:
Human h1 = new Human(), h2 = new Human();
Human.totalCount=5;
h1.totalCount++;
System.out.println(h2.totalCount);

totalCount ,
6.

Java

Rendered by www.RenderX.com

. 3 27

, ,
.
.
class Human {
private static int totalCount;
public static int getTotalCount() {
return totalCount;
}
}

.
Human.getTotalCount();

,
:
Human h=null;
h.getTotalCount(); //
Human.getTotalCount(); //

,
,
.
,
. ,
main() , .
.
, .
.
static:
class Human {
static {
System.out.println("Class loaded");
}
}

,
.
,
, :
class Test {

Java

Rendered by www.RenderX.com

. 4 27

static int a;
static {
a=5;
// b=7; // !
}
static int b=a;
}

.
, (
), :
class Test {
static int b=Test.a;
static int a=3;
static {
System.out.println("a="+a+", b="+b);
}
}

, :
a=3, b=0

, b a, .. 0.
a 3.
final, ,
,
. , final, ,
-.

. -
. ,
, .
.
,
. , ,
, .
, . ,
, main() ,
. , , MyClass.staticMethod(),
MyClass .
Math , .
,
. .

Java

Rendered by www.RenderX.com

. 5 27

,
..
class Test {
public void process() {
}
public static void main(String s[]) {
// process(); - ! ?
Test test = new Test();
test.process(); //
}
}

3. this super
,
. .
, ,
. this
:
class Test {
public Object getThis() {
return this; // ,
}
public static void main(String s[]) {
Test t = new Test();
System.out.println(t.getThis()==t); //
}
}

:
true

, this ,
. , , ,
- .
class Human {
public static void register(Human h) {
System.out.println(h.name+" is registered.");
}
private String name;
Java

Rendered by www.RenderX.com

. 6 27

this super

public Human (String s) {


name = s;
register(this); //
}
public static void main(String s[]) {
new Human("John");
}
}

:
John is registered.

this "" :
class Human {
private String name;
public void setName(String name) {
this.name=name;
}
}

this , :
class Test {
// int b=a; !
int b=this.a;
int a=5;
{
System.out.println("a="+a+", b="+b);;
}
public static void main(String s[]) {
new Test();
}
}

:
a=5, b=0

, - b
a, .. , a 5.
, this
. super,
.

Java

Rendered by www.RenderX.com

. 7 27

super
. , (overriding)
.
,
.
class Parent {
public int getValue() {
return 5;
}
}
class Child extends Parent {
//
public int getValue() {
return 3;
}
public static void main(String s[]) {
Child c = new Child();
//
System.our.println(c.getValue());
}
}

,
. ,
3. , Child, getVaue()
5, .
,
. , ,
.
, - ,
. super.
:
class Parent {
public int getValue() {
return 5;
}
}
class Child extends Parent {
//
public int getValue() {
Java

Rendered by www.RenderX.com

. 8 27

abstract

//
return super.getValue()+1;
}
public static void main(String s[]) {
Child c = new Child();
System.our.println(c.getValue());
}
}

6.
super
, .. , .
, ,
.
this super , ..
, .

4. abstract
, - abstract.
, ,
, . ,
, .
. , ,
, . ,
- , , ..; - ,
.. . ,
, . ,
, , , (
paint()) -.
. ,
.
, , ,
.. ,
, , .. ,
,
, .
?
- , . paint()
.
, ,
.
.
, , .
Java

Rendered by www.RenderX.com

. 9 27

, ,
, .
:
//
abstract class Operation {
public abstract int calculate(int a, int b);
}
//
class Addition {
public int calculate(int a, int b) {
return a+b;
}
}
//
class Subtraction {
public int calculate(int a, int b) {
return a-b;
}
}
class Test {
public static void main(String s[]) {
Operation o1 = new Addition();
Operation o2 = new Subtraction();
o1.calculate(2, 3);
o2.calculate(3, 5);
}
}

, main()
.
- ,
. , ,
. ,
, . .
, ,
, .
abstract ,
. - (implements)
( ) ,
.
, abstract final. .
, private, native, static.
Java

Rendered by www.RenderX.com

. 10 27

.
abstract class Test {
public abstract int getX();
public abstract int getY();
pulic double getLength() {
return Math.sqrt(getX()*getX()+getY()*getY());
}
}

, getLength() .
,
Test, .
.
null ,
.

5.

. Java ,
,
. , ,
, ?
, ?
,
. ,
, ,
.
Java. ,
.

5.1.
.
. .
public, ,
,
. abstract ,
, ,
, .
interface .
extends ,
. ,

Java

Rendered by www.RenderX.com

. 11 27

, ,
.
. ,
A B, B A, C
. , , A ,
B.
.
public interface Drawble extends Colorable, Resizable {
}

, -
.
public final static,
, .
, .
public
int
int
int
int
}

interface Directions {
RIGTH=1;
LETFT=2;
UP=3;
DOWN=4;

public abstract,
.
public interface Moveable {
void moveRight();
void moveLeft();
void moveUp();
void moveDown();
}

, , .

5.2.
.
,
,
.
,
, . ,
, :
interface A {
Java

Rendered by www.RenderX.com

. 12 27

int getValue();
}
interface B {
double getValue();
}

, ,
. ,
.
, .
- :
interface A {
int value=3;
}
interface B {
double value=5.4;
}
class C implements A, B {
public static void main(String s[]) {
C c = new C();
// System.out.println(c.value); - !
System.out.println(((A)c).value);
System.out.println(((B)c).value);
}
}

, ,
, .
.
, implements ,
. ,
.
A B, A, ,
B -
- A, .
, C1 C2, C2 A1,
A1 A2, C1
A2.
, .
null , ,
. ,
Object, ,
Object.
Java

Rendered by www.RenderX.com

. 13 27

5.3.
- ,
, . ,
.
, - ,
, , .
, ,
, ,
.
.
. , .
,
, .
- . ,
,
.
,
. ,
, . ,
- , , ,
, - . , , ""
-.
(, consumeInsect(Insect))
. , ,
- -
, , - .
Java . InsectConsumer:
public interface InsectConsumer {
void consumeInsect(Insect i);
}

:
//
public class Sundew extends Plant implements InsectConsumer {
public void consumeInsect(Insect i) {
...
}
}
//
public class Swallow extends Bird implements InsectConsumer {
public void consumeInsect(Insect i) {
...

Java

Rendered by www.RenderX.com

. 14 27

}
}
//
public class AntEater extends Mammal implements InsectConsumer {
public void consumeInsect(Insect i) {
...
}
}

, ,
:
// , ,
class FeedWorker extends Worker {
//
// , ,
public void feedOnInsects(InsectConsumer consumer) {
...
consumer.consumeInsect(insect);
...
}
}


, . ,
,
, .
, ( )
( ).
.
, ,
.

6.
.
.
.

. ,
.

Java

Rendered by www.RenderX.com

. 15 27

6.1.
-, , .
, . , ,
. , Child
,
, ( ),
.
, :
Child c = new Child();
System.out.println(c.a);
Parent p = c;
System.out.println(p.a);

, Child,
, - Parent. :
3
2

- "" .
- "" (hiding). ,
"" (shadowing) "" (obscuring) .
, , .
:
class Child extends Parent {
int a=3; //
int b=((Parent)this).a; //
int c=super.a; //
}

b c , a.
super ,
. ,
, .
, b.
:
class Parent {
int x=0;
public void printX() {
System.out.println(x);
}

Java

Rendered by www.RenderX.com

. 16 27

}
class Child extends Parent {
int x=-1;
}

?
new Child().printX();

?
Child, . , Parent,
, , x
Parent. 0.
. , ,
, .
:
class Parent {
static int a=2;
}
class Child extends Parent {
static int a=3;
}

?
Child c = new Child();
System.out.println(c.a);
Parent p = c;
System.out.println(p.a);

,
. , .
, null. .
:
System.out.println(Child.a);
System.out.println(Parent.a);

:
3
2

. ,
. - (hiding)
Java

Rendered by www.RenderX.com

. 17 27

.
, .
:
class Parent {
static int a;
}
class Child extends Parent {
}

?
Child.a=10;
Parent.a=5;
System.out.println(Child.a);

a Child.
, :
5

, ,
.
, ,
. .
, ,
.

6.2.
(overriding) :
class Parent {
public int getValue() {
return 0;
}
}
class Child extends Parent {
public int getValue() {
return 1;
}
}

, :
Child c = new Child();
Java

Rendered by www.RenderX.com

. 18 27

System.out.println(c.getValue());
Parent p = c;
System.out.println(p.getValue());

:
1
1

, , 0
, Child.
- ,
. , ,
, -
super.
:
class Parent {
public int getValue() {
return 0;
}
public void print() {
System.out.println(getValue());
}
}
class Child extends Parent {
public int getValue() {
return 1;
}
}

?
Parent p = new Child();
p.print();

Parent print(), Parent.


getValue(), Parent 0.
,
.
, . Child,
getValue().
:
1

,
. ,
Java

Rendered by www.RenderX.com

. 19 27

(, null ,
),
, , . ,
.
, , ,
throws-. ( ) ,
. ,
.
.
class Parent {
protected int getValue() {
return 0;
}
}
class Child extends Parent {
/* ??? */ protected int getValue() {
return 1;
}
}

protected. ,
, (public)
( )? :
Parent p = new Child();
p.getValue();

Parent.
, . , Child,
. ,
, . ,
, protected public. protected-
public, public
.
private-, , ,

, ..
throws-,
.
, ,
(implements). ,
.
(),

Java

Rendered by www.RenderX.com

. 20 27

throws-. ,
.
. :
class Parent {
static public int getValue() {
return 0;
}
}
class Child extends Parent {
static public int getValue() {
return 1;
}
}

, :
Child c = new Child();
System.out.println(c.getValue());
Parent p = c;
System.out.println(p.getValue());

,
,
:
System.out.println(Child.getValue());
System.out.println(Parent.getValue());

:
1
0

, , , ,
.
, .

6.3.
, .
-, ,
. :

Java

Rendered by www.RenderX.com

. 21 27

, :
-

private-;

"" ( ,
);

( ).

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

null

null
, , :
-

( implements);
;
;
- ,

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

7.
Java. , -,
,
. , ,
, ,
.

.

Java

Rendered by www.RenderX.com

. 22 27

this super. this


, , .
, .
super ,
, .
.
, , . ,
abstract, ,
.
,
.
Java, .
, , final static.
, .
.
, .
, ,
.
.
, , ,
.
.

8.
8-1.

, , Java. ,
?

, ;

, -
;

.
a.) 1, 4, 6 ,

2, 3, 5 , ,
.

8-2.

x?

Java

Rendered by www.RenderX.com

. 23 27

public class Test {


static void perform() {
...
}
private Test x;
public static void main(String s[]) {
x.perform(); // ?
}
}

a.) , .
, , ,

, , ,
.
8-3.

?
public class Parent {
int x=2;
}
public class Child extends Parent {
int x=3;
void print(int x) {
System.out.println(x);
System.out.println(this.x);
System.out.println(super.x);
}
public static void main(String s[]) {
new Child().print(0);
}
}

a.) :
0
3
2

,
main, 0.
, Child, 3. ,

Java

Rendered by www.RenderX.com

. 24 27

,
Parent, 2.
8-4.

this?
a.) :

,
:

8-5.

perform()
super.perform()?
a.) , super.perform()
, , ,
.

8-6.


?
a.) , .

8-7.

,
?
a.) , , null, ,
-. ,
.

8-8.

,
?
a.) public final static.
public abstract.

8-9.

,
implements?
a.) , .
public abstract.

Java

Rendered by www.RenderX.com

. 25 27

8-10. - ,
?
a.) , ,
.
8-11. ?
a.) .
8-12. ?
public class Parent {
int x = 2;
public void print() {
System.out.println(x);
}
}
public class Child extends Parent {
int x = 3;
public static void main(String s[]) {
new Child().print();
}
}

a.) 2, Parent,
, .
8-13. ,
Child ?
public void print() {
System.out.println(x);
}

a.) , ,
Child,
3.
8-14. , , ?
public class Test {
public static void test(Test t) {
System.out.println(test +t);
}
public static void main(String s[]) {
Test t = null;
t.test(t);
Java

Rendered by www.RenderX.com

. 26 27

}
}

a.) , null
, .
test test null.
8-15. ? ? ,
?
a.) , .
null.
, -.
,
, .

Java

Rendered by www.RenderX.com