码迷,mamicode.com
首页 > 其他好文 > 详细

类定义(课下选做)

时间:2019-04-06 12:29:33      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:tps   void   mic   art   out   lse   detail   obj   project   

一、设计思路

  • 之前在课上做过类似的题目,首先分析,这个题的重点在重写toString类和equals类
  • 首先,是toString类,直接重写public String toString(){return 字符串;}返回要输出的字符串即可
  • 然后是equals类
    • public boolean equals(Object o){内容}重写
    • 先用if(this==o)判断地址是否相同,若相同直接true
    • 再用if(o==null)判断对象是否为空```
    • 接着用if (getClass()!=o.getClass())判断对象类型是否相等
    • 最后判断成员变量是否相等
    • 为提高程序效率,应重写hasCode方法

二、代码

Book.java

public class Book {
    String name;
    String auther;
    String publish;
    String date;
    Book(){}
    Book(String name){}
    Book(String auther,String publish,String date){}
    Book(String name,String auther,String publish,String date){
        this.name=name;
        this.auther=auther;
        this.publish=publish;
        this.date=date;
    }
    public void setName(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    public void setAuther(String auther){
        this.auther=auther;
    }
    public String getAuther(){
        return auther;
    }
    public void setPublish(String publish){
        this.publish=publish;
    }
    public String getPublish(){
        return publish;
    }
    public void setDate(String date){
        this.date=date;
    }
    public String getDate(){
        return date;
    }
    public String toString(){                  //重写toString类
        return "书名:"+name+",作者:"+auther+",出版社:"+publish+",出版日期:"+date;
    }
    public boolean equals(Object o){
        if (this==o)        //判断地址是否相等,若相等,直接返回true
            return true;
        if (o==null)         //判断对象是否为空
            return false;
        if (getClass()!=o.getClass())    //判断对象类型是否相等
            return false;
        Book book=(Book)o;         //向下转型,判断成员变量是否相等
        if (book.name==this.name&&book.auther==this.auther&&book.publish==this.publish&&book.date==this.date)
            return true;
        else
            return false;
    }
        public int hashCode() {                 //重写hasCode类,提高程序效率
        int result = name.hashCode();
        result = 31 * result + auther.hashCode();
        result = 31 * result + publish.hashCode();
        result = 31 * result + date.hashCode();
        return result;
    }
}

bookShelf.java

public class bookShelf {
    public static void main(String[] args){
        Book b0 = new Book("Java","耿祥义","清华大学出版社","2017.9");
        Book b1 = new Book("汇编语言与程序设计","钱晓捷","电子工业出版社","2018.6");
        Book b2 = new Book("计算机网络","谢希仁","电子工业出版社","2017.1");
        System.out.println(b0.toString());
        System.out.println(b1.toString());
        System.out.println(b2.toString());
        System.out.println(b0.getName());
        System.out.println(b1.getAuther());
        System.out.println(b2.getDate());
        System.out.println(b0.equals(b1));
        System.out.println(b2.equals(b2));
    }
}

三、运行结果截图

技术图片

四、码云代码链接

五、参考资料

类定义(课下选做)

标签:tps   void   mic   art   out   lse   detail   obj   project   

原文地址:https://www.cnblogs.com/wyf20175217/p/10661292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!