Класс Object Макаревич Л. Г..

Slides:



Advertisements
Similar presentations
Uguaglianza equals() e clone(). Fondamenti di Java Uguali o identici?
Advertisements

Clonazione La clonazione... Ovvero: come costruire una copia (probabilmente che ritorni true su equals?)
Class 18: Concurrency on Mars Fall 2010 UVa David Evans cs2220: Engineering Software Image: Michael Dewey-Vogt.
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
1 Class Design CS 3331 Fall Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode method.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
CSE 11 February 6, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
28-Jun-15 Producer-Consumer An example of using Threads.
CMSC 132 Week2 Lab1 Comparable vs Comparator clone() finalize()
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Java Implementation: Part 3S CompSci 230 Software Construction.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Viswanathan Inheritance and Polymorphism Course Lecture Slides 2 nd June 2010 “ We are.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
The java.lang Package chapter 8 Java Certification Study Group February 2, 1998 Seth Ladd.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
Synchronizing threads, thread pools, etc.
1 TCSS 143, Autumn 2004 Lecture Notes Creating Java Classes.
Overriding toString()
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Java Implementation: Part 4 Software Construction Lecture 9.
Java for C++ Programmers A Brief Tutorial. Overview Classes and Objects Simple Program Constructors Arrays Strings Inheritance and Interfaces Exceptions.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Java Type System and Object Model Horstmann ch , 7.7.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
TreeBag a BST implementation. Example class - TreeBag  remember Bag?  collection of items, order does not matter  repeated items allowed public void.
1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Computer Science 320 A First Program in Parallel Java.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Classes Revisited Chapter 8.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
1 CSE 331 The Object class; Object equality and the equals method slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R.
The Object class Object package java.lang Object clone equals hashCode toString aCopy toThis hash string ! yesOrNo.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
CSC 205 Java Programming II
Lecture 17: Polymorphism (Part II)
Chapter 8 Classes and Objects
Interfaces.
OOPs.
The Language Package.
CSE 1030: Implementing Mixing static and non-static features
Everything the light touches, Simba, will be yours
Exercise 11.1 Write a code fragment that performs the same function as the statement below without using the crash method Toolbox.crash(amount < 0,
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Multithreading.
Sampath Kumar S Assistant Professor, SECE
Generic Classes and Methods
class PrintOnetoTen { public static void main(String args[]) {
Lecture 6 Inheritance CSE /26/2018.
Lecture 18: Polymorphism (Part II)
Lecture 25: Inheritance and Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
Sampath Kumar S Assistant Professor, SECE
Lecture 6 Inheritance CSE /26/2018.
Presentation transcript:

Класс Object Макаревич Л. Г.

Object – главный в иерархии класс При переопределении методов соблюдается следующее правило: модификатор доступа к методу в дочернем классе не может быть более закрытым, чем в родительском

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  

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!!!");

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!!!");

Клонирование (мелкое) 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!!!");

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);

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);

Клонирование (глубокое) 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!!!"); Вначале мелкое клонирование, а потом - глубокое