370 likes | 387 Views
Learn about implementing classes in Java, overriding methods, and comparing objects using method.equals. Dive into Object class methods like toString and understand class hierarchy in a library system.
E N D
Topics Covered Today • 2.1 Implementing Classes • 2.1.3 Method equals and Method toString • 1.2.6 Implementing Classes the Library System
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.
Object Person Employee Class Hierarchy Diagram
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.
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() + ")"; } }
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)
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)
Compare Point objects Point 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");}
Compare Point objects Point pointOne = new Point(10, 100); Point pointTwo = new Point(10, 100); Point pointThree = pointOne; pointThree pointOne pointTwo (null) (null)
Compare Point objects Point pointOne = new Point(10, 100); Point pointTwo = new Point(10, 100); Point pointThree = pointOne; pointTwo pointOne pointThree (null)
Compare Point objects Point pointOne = new Point(10, 100); Point pointTwo = new Point(10, 100); Point pointThree = pointOne; pointTwo pointOne pointThree
X=10 Y=100 Compare Point objects Point pointOne = new Point(10, 100); Point pointTwo = new Point(10, 100); Point pointThree = pointOne; pointTwo pointOne pointThree X=10 Y=100
Override method equals • 在大部分类中继承自Object类的equals()方法都不太适用,因此每个类都需要重写该方法; • 该方法比较两个对象,只有当两个对象的状态都一样时,返回true; public boolean equals(Object object) { if (object instanceof Point) { Point point = (Point) object; return point.getX() == getX() && point.getY() == getY(); } else { return false; } }
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"); }
overload和override的区别 override(重写) 1、方法名、参数、返回值相同。2、子类方法不能缩小父类方法的访问权限。3、子类方法不能抛出比父类方法更多的异常(但子类方法可以不抛出异常)。4、存在于父类和子类之间。5、方法被定义为final不能被重写。 overload(重载)1、参数类型、个数、顺序至少有一个不相同。 2、不能重载只有返回值不同的方法名。3、存在于父类和子类、同类中。
Class CatalogItem public 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;
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; }
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; }
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; }
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()); }
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(); } }
Class Book public class Book extends CatalogItem { /* Author of the book.*/ private String author; /* Number of pages in the book.*/ private int numberOfPages;
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; }
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; }
Class Book /** * Returns the string representation of this book. * * @return the string representation of this book. */ public String toString() { return super.toString() + "_" + getAuthor() + "_" + getNumberOfPages(); } }
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;
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; }
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; }
Class Recording /** * Returns the string representation of this recording. * * @return the string representation of this recording. */ public String toString() { return super.toString() + "_" + getPerformer() + "_“ + getFormat(); } }
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;
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; }
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; }
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()); }
Class Borrower /** * Returns the string representation of this borrower. * * @return the string representation of this borrower. */ public String toString() { return getId() + "_" + getName(); } }