Download presentation
Presentation is loading. Please wait.
1
Класс Object Макаревич Л. Г.
2
Object – главный в иерархии класс
При переопределении методов соблюдается следующее правило: модификатор доступа к методу в дочернем классе не может быть более закрытым, чем в родительском
3
Method Summary protected Object clone() throws CloneNotSupportedException Creates and returns a copy of this object. boolean equals(Object obj) Indicates whether some other object is "equal to" this one. protected void finalize()throws Throwable Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. Class getClass() Returns the runtime class of an object. int hashCode() Returns a hash code value for the object. void notify() Wakes up a single thread that is waiting on this object's monitor. notifyAll() Wakes up all threads that are waiting on this object's monitor. String toString() Returns a string representation of the object. wait() Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. wait(long timeout) Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. wait(long timeout, int nanos) Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed. Методы Object
4
class My { int i; My(int i){this.i=i;} } class Clone1 public static void main(String [] args) My m1 = new My(55); My m2 = new My(55); if ( m1 == m2 ) System.out.println("Equal!!!"); else System.out.println("Not equal!!!"); class My { int i; My(int i){this.i=i;} } class Clone1 public static void main(String [] args) My m1 = new My(55); My m2 = new My(55); if ( m1.equals(m2) ) System.out.println("Equal!!!"); else System.out.println("Not equal!!!");
5
class My { int i; My(int i){this.i=i;} public boolean equals(Object o) My m = (My)o; if ( i == m.i) return true; else return false; } class Clone1 public static void main(String [] args) My m1 = new My(55); My m2 = new My(55); if ( m1.equals(m2) ) System.out.println("Equal!!!"); else System.out.println("Not equal!!!");
6
Клонирование (мелкое)
class My implements Cloneable { int i; My(int i){this.i=i;} public Object clone() throws CloneNotSupportedException return super.clone(); } public boolean equals(Object o) My m = (My)o; if ( i == m.i) return true; else return false; public static void main(String [] args)throws Exception My m1 = new My(55); My m2; m2=(My)m1.clone(); if ( m1.equals(m2) ) System.out.println("Equal!!!"); else System.out.println("Not equal!!!"); if ( m1==m2 ) System.out.println("References Equal!!!"); else System.out.println("References Not equal!!!");
7
class ClMy { int m; ClMy(int i){m=i;} } class My implements Cloneable int i; ClMy clm; My(int i){this.i=i; clm=new ClMy(i);} /* public Object clone() throws CloneNotSupportedException My m = (My)super.clone(); m.clm= new ClMy(i); return m; */ public boolean equals(Object o) My m = (My)o; if ( i == m.i) return true; else return false; public static void main(String [] args)throws Exception My m1 = new My(55); My m2; m2=(My)m1.clone(); if ( m1.equals(m2) ) System.out.println("Equal!!!"); else System.out.println("Not equal!!!"); if ( m1==m2 ) System.out.println("References Equal!!!"); else System.out.println("References Not equal!!!"); // m1=null; System.out.println(m2); System.out.println(m2.clm); System.out.println(m1.clm);
8
class ClMy { int m; ClMy(int i){m=i;} } class My implements Cloneable int i; ClMy clm; My(int i){this.i=i; clm=new ClMy(i);} public Object clone() throws CloneNotSupportedException My m = (My)super.clone(); m.clm= new ClMy(i); return m; public boolean equals(Object o) My m = (My)o; if ( i == m.i) return true; else return false; public static void main(String [] args)throws Exception My m1 = new My(55); My m2; m2=(My)m1.clone(); if ( m1.equals(m2) ) System.out.println("Equal!!!"); else System.out.println("Not equal!!!"); if ( m1==m2 ) System.out.println("References Equal!!!"); else System.out.println("References Not equal!!!"); // m1=null; System.out.println(m2); System.out.println(m2.clm); System.out.println(m1.clm);
9
Клонирование (глубокое)
class ClMy { int m; ClMy(int i){m=i;} } class My implements Cloneable int i; ClMy clm; My(int i){this.i=i; clm=new ClMy(i);} public Object clone() throws CloneNotSupportedException My m = (My)super.clone(); m.clm= new ClMy(i); return m; public boolean equals(Object o) My m = (My)o; if ( i == m.i) return true; else return false; public static void main(String [] args)throws Exception My m1 = new My(55); My m2; m2=(My)m1.clone(); if ( m1.equals(m2) ) System.out.println("Equal!!!"); else System.out.println("Not equal!!!"); if ( m1==m2 ) System.out.println("References Equal!!!"); else System.out.println("References Not equal!!!"); Вначале мелкое клонирование, а потом - глубокое
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.