.


:




:

































 

 

 

 


10 1




mng , . , .

Object

Object, . Object , , . Object , :

protected Object clone() ;

boolean equals(Object ob)
;

Class<? extends Object> getClass() Class;

protected void finalize() (garbage collection);

int hashCode() - ;

String toString() .

notify(), notifyAll() wait() .

, , : equals(Object ob) hashCode(). , , . equals() , , . equals() , Java, :

;

x.equals(y) true, y.equals(x) true;

equals() true x y, y z, x z true;

;

null false.

hashCode() toString(), .

hashCode() , , , , . , equals(). hashCode() - , :

- , ;

-;

-.

hashCode(), , , # 10.

toString() , (), , (), , ( ), -, Object. toString() Object :

getClass().getName() + '@' + Integer.toHexString(hashCode())

, println(), print() .

/* # 10: equals(), hashCode, toString():

Student.java */

package chapt04;

public class Student {

private int id;

private String name;

private int age;

 

public Student(int id, String name, int age){

this. id = id;

this. name = name;

this. age = age;

}

public int getId() {

return id;

}

public String getName() {

return name;

}

public int getAge() {

return age;

}

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (obj instanceof Student){ //warning

Student temp = (Student) obj;

return this. id == temp.id &&

name.equals(temp.name) &&

this. age == temp.age;

} else

return false;

}

public int hashCode() {

return (int)(31 * id + age

+ ((name == null)? 0: name.hashCode()));

}

public String toString() {

return getClass().getName()+"@name" +name

+ " id:" + id + " age:" + age;

}

}

31 * id + age , id=1 age=2, 33, , 63. .

equals() Student , , Student , id, name age , .

/* # 11: : SubStudent.java */

package chapt04;

 

public class SubStudent extends Student {

private int idFaculty;

 

public SubStudent (int id, String n, int a, int idf){

super (id, n, a);

this. idFaculty = idf;

}

}

/* # 12: equals() :

StudentEq.java */

package chapt04;

 

public class StudentEq {

public static void main(String[] args) {

Student p1 = new Student(71, "", 19);

Student p2 = new Student(71, "", 19);

SubStudent p3 =

new SubStudent(71, "", 19, 5);

System. out. println(p1.equals(p2));

System. out. println(p1.equals(p3));

System. out. println(p3.equals(p1));

}

}

:

True

True

True

equals() , , . .

, //warning equals() Student Class :

if (getClass() == obj.getClass())

:

True

False

False

equals() SubStudent , Student.

, , . , . () . Object protected - clone(), . clone() public . super. clone(), . , Cloneable. Cloneable (tagged) , , clone() Object . CloneNotSupportedException. , . C++ .

/* # 13: , : Student.java */

package chapt04;

public class Student implements Cloneable { /*

*/

private int id = 71;

public int getId() {

return id;

}

public void setId(int value) {

id = value;

}

public Object clone() { //

try {

return super. clone(); //

} catch (CloneNotSupportedException e) {

throw new AssertionError("!");

}

}

}

/* # 14: : DemoSimpleClone.java */

package chapt04;

public class DemoSimpleClone {

private static void changeId(Student p) {

p = (Student) p.clone(); //

p.setId(1000);

System. out. println("->id = " + p.getId());

}

public static void main(String[] args) {

Student ob = new Student();

System. out. println("id = " + ob.getId());

changeId(ob);

System. out. println("id = " + ob.getId());

}

}

:

id = 71

->id = 1000

id = 71

clone(), :

id = 71

->id = 1000

id = 1000

, (immutable) . , . , . .

/* # 15: : Student.java */

package chapt04;

import java.util.ArrayList;

public class Student implements Cloneable {

private int id = 71;

private ArrayList<Mark> lm = new ArrayList<Mark>();

public int getId() {

return id;

}

public void setId(int id) {

this. id = id;

}

public ArrayList<Mark> getMark() {

return lm;

}

public void setMark(ArrayList<Mark> lm) {

this. lm = lm;

}

public Object clone() {

try {

Student copy =(Student) super. clone();

copy.lm = (ArrayList<Mark>)lm.clone();

return copy;

} catch (CloneNotSupportedException e) {

throw new AssertionError(

" Cloneable!");

}

}

}

, Cloneable clone(). , . , , . final .

new, , . , , . , , , , , , . . , System.gc() Runtime.getRuntime().gc(), , . System.runFinalization() finalize() .

. , . : try-finally finalization. try-finally , . finalization . -
finalize() , JVM. , . , . finalize(), , , .

finalize() :

protected void finalize(){

//

}

protected finalize() , . finalize() , , , , finalize() , . ! .

/* # 16: Manager finalization: Manager.java */

package chapt04;

 

class Manager {

private int id;

public Manager(int value) {

id = value;

}

protected void finalize() throws Throwable {

try {

//

System. out. println(" , id=" + id);

} finally {

super. finalize();

}

}

}

package chapt04;

 

public class FinalizeDemo {

public static void main(String[] args) {

Manager d1 = new Manager(1);

d1 = null;

Manager d2 = new Manager(2);

Object d3 = d2; //1

//Object d3 = new Manager (3); //2

d2 = d1;

System.gc(); // " "

}

}

System.gc() .

, id=1

1 2, gc() .

, id=1

, id=2

finalize() , . , , , .

4

A

, , . equals(), hashCode(), toString().

1. , . : , , .

2. , . : , , , .

3. , . : , , .

4. , . : , , , .

5. , . : , , .

6. , , , . : , , .

7. , , , . : , , , .

8. , , . : , , , , .

9. , , . : , , .

10. , , . : , , ( ).

11. , , . : , , , .

12. , . : , , , , .

13. , . : , , (, , ).

14. , . : , , , , .

15. , , . : , , .

16. , , . : , , .

17. , . : , , , .

18. , . : , , .

19. , . : , , .

20. , , . : , , , .

21. , , . : , (, , , ).

22. , . : , .

23. , . : , , .

24. , . : , .

.

1. . . , , . , .

2. . (). , , . .

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

4. . , . . , .

5. . . . . .

6. . () . , , .

7. . , . , .

8. . . . . .

9. -. . . .

10. . , . . . , , .

11. . , . . .





:


: 2016-04-03; !; : 822 |


:

:

, .
==> ...

1465 - | 1407 -


© 2015-2024 lektsii.org - -

: 0.137 .