.


:




:

































 

 

 

 


()




. - . , .

.

class A {

int x;

...

}

 

class B extends A {

int y;

...

}

 

B b = new B();

A a = b; // : B => A

 

upcasting downcasting. (upcasting) ( ) (). . (. ). , .

(downcasting) . . -, , ,

B b1 = (B)a;

-, , , , . , ClassCastException .

, , . , , , , B, B A, - A, , , B.

. A , - . , A.

, , B A. , A, B A (upcasting), A B ( ).

- A, B (downcasting). , , .

Java instanceof. (downcasting) . , , .

if (a instanceof B)

b1 = (B)a;

 

, , instanceof.

(Issue , Book , Newspaper - , Journal ).

2.

Issue Book . :

public class Issue {

String name;

public Issue(String name) {

this.name = name;

}

public void printName(PrintStream out) {

out.println(":");

out.println(name);

}

...

}

 

public class Book extends Issue {

String authors;

public Book(String name, String authors) {

super(name);

this.authors = authors;

}

 

public void printAuthors(PrintStream out) {

out.println(":");

out.println(authors);

}

...

}

 

-

Issue[] catalog = new Issue[] {

new Journal("Play Boy"),

new Newspaper(" "),

new Book(" ", "."), };

...

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

if (catalog[i] instanceof Book)

((Book) catalog[i]).printAuthors(System.out);

catalog[i].printName(System.out);

}

 

. ( ), , . (upcasting). . , . instanceof , Book. , , .. printAuthors(...) Book, Issue.

 

12)

.

  • , .

, , : (overloading) (overriding).

.

overloading , , . . . . , , . . . .

class X {

 

int f() {

...

}

 

void f(int k) {

...

}

...

}

 

X f(...), . , , , , . .

class Base {

 

int f(int k) {

...

}

...

}

 

class Derived extends Base {

 

int f(String s, int k) {

...

}

...

}

 

. Derived f(...). Base, Derived.

overloading overriding (, , ). (overriding) . , , .

class A {

int x;

int f(int a) {

return a+x;

}

...

}

 

class B extends A {

int y;

 

int f(int s) {

return s*x;

}

...

}

 

B b = new B();

A a = b; // : B => A

int c = a.f(10); //??? f(...) ???

 

. "a" A, B. , f() . B.f().

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

. (dynamic binding, late binding, run-time binding). C++ .

, . Issue Book .

public class Issue {

String name;

public Issue(String name) {

this.name = name;

}

public void print(PrintStream out) {

out.println(":");

out.println(name);

}

...

}

 

public class Book extends Issue {

String authors;

public Book(String name, String authors) {

super(name);

this.authors = authors;

}

 

public void print(PrintStream out) {

out.println(":");

out.println(authors);

super.print(out); //

}

...

}

 

, .

Issue[] catalog = new Issue[] {

new Journal("Play Boy"),

new Newspaper(" "),

new Book(" ", "."), };

...

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

catalog[i].print(System.out);

}

 

Issue Book printName(...) printAuthors(...) print(..). Book print(...) Issue.

  • print(...) Book super. .

Book. print(...) Book .

final ()

Java final, , , .

, , . final , .

final double pi = 3.14;

final static , .. , . pi .

static final double pi = 3.14;

(overriding) , final.

, final . , .

13)

- . , , (static final - ).

  • . , , , , .

, , , . . .. .

  • , , .

:

public interface XXX {

...

int f(String s);

}

 

XXX. ( ) (static final ). XXX , f(...).

public class R implements Serializable, XXX {

...

}

 

R Serializable XXX.

, , , . XXX f(...), R :

public class R implements Serializable, XXX {

...

public int f(String s) {

...

}

...

}

 

, f(...) public, R public. , public, . R .

, , .

  • . , , - . , , . , , . - . (deployment) -, ( , , ).

, Java.

14)





:


: 2016-12-06; !; : 403 |


:

:

.
==> ...

1710 - | 1497 -


© 2015-2024 lektsii.org - -

: 0.033 .