object-oriented design and programming (java). 2 topics covered today 2.1 implementing classes...
TRANSCRIPT
![Page 1: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/1.jpg)
Object-Oriented Design and Programming (Java)
![Page 2: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/2.jpg)
2
Topics Covered Today
• 2.1 Implementing Classes – 2.1.3 Method equals and Method toString – 1.2.6 Implementing Classes the Library System
![Page 3: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/3.jpg)
3
The Object Class
• Java defines the class java.lang.Object that is defined as a superclass for all classes.
• If a class doesn’t specify explicitly which class it is derived from, then it will be implicitly derived from class Object.
• So, in the previous example, Employee was derived from Person which in turn was derived from Object.
![Page 4: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/4.jpg)
4
Person
Employee
Object
Class Hierarchy Diagram
![Page 5: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/5.jpg)
5
Method toString in Object Class
• The Object class defines a set of methods that are inherited by all classes.
• One of these is the toString() method that is used whenever we want to get a String representation of an object.
• The version of toString method defined in class Object returns a String with the following format: ClassName@number
• When you define a new class, you can override the toString() method in order to have a suitable representation of the new type of objects as Strings.
![Page 6: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/6.jpg)
6
Point
public class Point { private int x,y;
public Point(int initialX, int initialY) {x = initialX;y = initialY;
}public int getX() { return x; }public int getY() { return y; }
public String toString() {return "(" + getX() + "," + getY() + ")";
} }
![Page 7: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/7.jpg)
7
Method toString
Point pointOne = new Point(10, 100);
Point pointTwo = new Point(-20, 200);
Point pointThree = new Point(50, -500);
System.out.println(pointOne);
System.out.println(pointTwo);
System.out.println(pointThree);
System.out.println();
Output :(10,100)
(-20,200)
(50,-500)
![Page 8: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/8.jpg)
8
Method equals
• In JAVA, all classes inherit the equals() method of Object class.
• This default implementation is exactly the same as obj1 == obj2 (compare pointers)
• boolean equals(Object obj)
![Page 9: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/9.jpg)
9
Compare Point objectsPoint pointOne = new Point(10, 100);
Point pointTwo = new Point(10, 100);
Point pointThree = pointOne;
if (pointOne.equals(pointTwo)) {
System.out.println("pointOne and pointTwo are equal");
} else {
System.out.println("pointOne and pointTwo are different");
}
if (pointOne.equals(pointThree)) {
System.out.println("pointOne and pointThree are equal");
} else {
System.out.println("pointOne and pointThree are different");}
![Page 10: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/10.jpg)
10
Compare Point objects
pointOne pointTwo pointThree(null) (null)
Point pointOne = new Point(10, 100);Point pointTwo = new Point(10, 100);
Point pointThree = pointOne;
![Page 11: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/11.jpg)
11
Compare Point objects
pointOnepointThree(null)
Point pointOne = new Point(10, 100);Point pointTwo = new Point(10, 100);
Point pointThree = pointOne;
pointTwo
![Page 12: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/12.jpg)
12
Compare Point objects
pointOnepointThree
Point pointOne = new Point(10, 100);Point pointTwo = new Point(10, 100);
Point pointThree = pointOne;
pointTwo
![Page 13: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/13.jpg)
13
Compare Point objects
Point pointOne = new Point(10, 100);Point pointTwo = new Point(10, 100);
Point pointThree = pointOne;
pointOne
pointThree X=10Y=100
pointTwo
X=10Y=100
![Page 14: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/14.jpg)
14
Override method equals
public boolean equals(Object object) {
if (object instanceof Point) {
Point point = (Point) object;
return point.getX() == getX() &&
point.getY() == getY();
} else {
return false;
} }
• 在大部分类中继承自 Object 类的 equals() 方法都不太适用,因此每个类都需要重写该方法;
• 该方法比较两个对象,只有当两个对象的状态都一样时,返回 true ;
![Page 15: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/15.jpg)
15
Override method equals
Point pointOne = new Point(10, 100);
Point pointTwo = new Point(10, 100);
Point pointThree = new Point(50, 500);
if (pointOne.equals(pointTwo)) {
System.out.println("pointOne and pointTwo are equal");
} else {
System.out.println("pointOne and pointTwo are different");
}
if (pointOne.equals(pointThree)) {
System.out.println("pointOne and pointThree are equal");
} else {
System.out.println("pointOne and pointThree are different");
}
![Page 16: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/16.jpg)
overload和override的区别
16
override (重写) 1 、方法名、参数、返回值相同。2 、子类方法不能缩小父类方法的访问权限。3 、子类方法不能抛出比父类方法更多的异常 ( 但子类方法可以不抛出异常 ) 。4 、存在于父类和子类之间。5 、方法被定义为 final 不能被重写。
overload (重载)1 、参数类型、个数、顺序至少有一个不相同。 2 、不能重载只有返回值不同的方法名。3 、存在于父类和子类、同类中。
![Page 17: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/17.jpg)
17
1.2.6 Implementing Classes the Library System
![Page 18: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/18.jpg)
18
Part of Class Diagram
![Page 19: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/19.jpg)
19
Class CatalogItempublic class CatalogItem { /* Code of the item. */ private String code;
/* Title of the item. */ private String title;
/* Year the item was published. */ private int year;
/* Indicates if the item is available */
private boolean available;
![Page 20: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/20.jpg)
20
Class CatalogItem /**
* Constructs a <code>CatalogItem</code> object.
* <p>
* Sets the instance variable <code>available</code>
* to <code>true</code>.
* <p>
*
* @param initialCode the code of the item.
* @param initialTitle the title of the item.
* @param initialyear the year the item was published.
*/
public CatalogItem(String initialCode, String initialTitle, int initialyear) {
code = initialCode;
title = initialTitle;
year = initialyear;
available = true;
}
![Page 21: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/21.jpg)
21
Class CatalogItem/*** Returns the code of this item.
* * @return the code of this item. */
public String getCode() { return code;}
/** * Returns the title of this item. * * @return the title of this item. */
public String getTitle() { return title; }
/** * Returns the year this item was published. * * @return the year this item was published. */
public int getYear() { return year; }
![Page 22: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/22.jpg)
22
Class CatalogItem/**
* Sets the value of instance variable <code>available</code>. * * @param value the new value. */ public void setAvailable(boolean value) {
available = value;}
/** * Returns <code>true</code> if the item is available. * * @return <code>true</code> if the item is available; * <code>false</code> otherwise. */ public boolean isAvailable() { return available; }
![Page 23: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/23.jpg)
23
Class CatalogItem /**
* Returns <code>true</code> if the code of this catalog item
* is equal to the code of the argument
* </p>
*
* @param object object with which this catalog item is compared.
* @return <code>true</code> if the code of this catalog item is
* equal to the code of the argument; <code>false</code>
* otherwise.
*/
public boolean equals(Object object) {
return object instanceof CatalogItem &&
getCode().equals(((CatalogItem) object).getCode());
}
![Page 24: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/24.jpg)
24
Class CatalogItem/**
* Returns the string representation of this catalog item.
*
* @return the string representation of this catalog item.
*/
public String toString() {
return getCode() + "_" + getTitle() + "_" + getYear() + "_" + isAvailable();
}
}
![Page 25: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/25.jpg)
25
Class Bookpublic class Book extends CatalogItem { /* Author of the book.*/ private String author;
/* Number of pages in the book.*/ private int numberOfPages;
![Page 26: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/26.jpg)
26
Class Book /** * Constructs a <code>Book</code> object.
* * @param initialCode the code of the book. * @param initialTitle the title of the book. * @param initialYear the year the book was published. * @param initialAuthor the author of the book. * @param initialNumberOfPages the number of pages in the book. */ public Book(String initialCode, String initialTitle, int initialYear, String initialAuthor, int initialNumberOfPages) { super(initialCode, initialTitle, initialYear); author = initialAuthor; numberOfPages = initialNumberOfPages; }
![Page 27: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/27.jpg)
27
Class Book/*** Returns the author of this book.*
* @return the author of this book. */ public String getAuthor() {
return author;}
/** * Returns the number of pages in this book. * * @return the number of pages in this book. */
public int getNumberOfPages() { return numberOfPages; }
![Page 28: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/28.jpg)
28
Class Book/**
* Returns the string representation of this book. * * @return the string representation of this book. */ public String toString() {
return super.toString() + "_" + getAuthor() + "_" + getNumberOfPages();
}}
![Page 29: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/29.jpg)
29
Class Recording/** * This class models a recording. It extends {@link CatalogItem} and * adds the following information: * <ol> * <li>the performer of the recording, a <code>String</code></li> * <li>the format of the recording, a <code>String</code></li> * </ol> * * @author author name * @version 1.0.0 * @see CatalogItem */public class Recording extends CatalogItem { /* Performer of the recording. */ private String performer;
/* Format of the recording. */ private String format;
![Page 30: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/30.jpg)
30
Class Recording/**
* Constructs an <code>Recording</code> object. * * @param initialCode the code of the catalog item. * @param initialTitle the title of the catalog item. * @param initialYear the year of the catalog item. * @param initialPerformer the performer of the recording. * @param initialFormat the format of the recording. */
public Recording(String initialCode, String initialTitle, int initialYear, String initialPerformer, String initialFormat) {
super(initialCode, initialTitle, initialYear); performer = initialPerformer; format = initialFormat; }
![Page 31: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/31.jpg)
31
Class Recording/*** Returns the performer of this recording.
* * @return the performer of this recording. */ public String getPerformer() { return performer; }
/** * Returns the format of this recording. * * @return the format of this recording. */ public String getFormat() { return format; }
![Page 32: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/32.jpg)
32
Class Recording/**
* Returns the string representation of this recording. * * @return the string representation of this recording. */ public String toString() { return super.toString() + "_" + getPerformer() + "_“ + getFormat(); }}
![Page 33: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/33.jpg)
33
Class Borrower/** * This class models a library user. It contains the following * information: * <ol> * <li>the id of the borrower, a <code>String</code></li> * <li>the name of the borrower, a <code>String</code></li> * </ol> * * @author author name * @version 1.0.0 */public class Borrower { /* Identification number of the borrower.*/ private String id;
/* Name of the borrower.*/ private String name;
![Page 34: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/34.jpg)
34
Class Borrower /**
* Constructs a <code>Borrower</code> object. * * @param initialId the identification number of the borrower. * @param initialName the name of the borrower. */ public Borrower(String initialId, String initialName) {
id = initialId; name = initialName;
}
![Page 35: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/35.jpg)
35
Class Borrower /** * Returns the identification number of this borrower.
* * @return the identification number of this borrower. */ public String getId() { return id; } /** * Returns the name of this borrower. * * @return the name of this borrower. */ public String getName () { return name; }
![Page 36: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/36.jpg)
36
Class Borrower /**
* Returns <code>true</code> if the id of this borrower is * equal to the id of the argument. * </p> * * @param object object with which this borrower is compared. * @return <code>true</code> if the id of this borrower is * equal to the id of the argument; <code>false</code> * otherwise. */ public boolean equals(Object object) { return object instanceof Borrower && getId().equals(((Borrower) object).getId()); }
![Page 37: Object-Oriented Design and Programming (Java). 2 Topics Covered Today 2.1 Implementing Classes –2.1.3 Method equals and Method toString –1.2.6 Implementing](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfd91a28abf838cafc4f/html5/thumbnails/37.jpg)
37
Class Borrower /**
* Returns the string representation of this borrower. * * @return the string representation of this borrower. */ public String toString() { return getId() + "_" + getName(); }}