• 20175212童皓桢 类定义


    20175212童皓桢 类定义

    题目要求

    • 设计并实现一个Book类,定义义成Book.java,Book 包含书名,作者,出版社和出版日期,这些数据都要定义getter和setter。
    • 定义至少三个构造方法,接收并初始化这些数据。
    • 覆盖(Override)toString方法,返回良好的含有多行的书的描述信息。
    • 覆盖equals方法,书名,作者,出版社和出版日期完全一致才说明两本书是一样的。
    • 创建一个测试类Bookshelf, 其中的main方法创建并更新几个Book对象。Book至少包含三本本学期教材内容。

    设计思路

    • 按照要求定义Book类中的各种信息对象和方法
    • 覆盖toString方法和equals方法,使其返回的类型不变
    • 设计测试类

    核心代码

    class Book {
        String name;
        String author;
        String company;
        String date;
        void setname(String d){
            this.name = d;
        }
        String getname(){
            return name;
        }
    
        void setauthor(String a){
            this.author = a;
        }
        String getauthor(){
            return author;
        }
    
        void setcompany(String b){
            this.company = b;
        }
        String getcompany(){
            return company;
        }
    
        void setdate(String c){
            this.date = c;
        }
        String getdate(){
            return date;
        }
    
    
        @Override
        public String toString(){//覆盖toString方法
            return "Book name="+name+",Book author="+author+",Book company="+company+",Book date="+date;
        }
    
        public boolean equals(Book a){//覆盖equals方法
            if (this.author == a.author&&this. name==a.name&&this.company==a.company&&this.date==a.date) return true;
            else return false;
        }
    
    }
    
    

    运行截图

    代码托管

  • 相关阅读:
    高级数据结构实现——自顶向下伸展树
    优先队列——二项队列(binominal queue)
    优先队列——左式堆
    近似装箱问题(两种脱机算法实现)
    近似装箱问题(三种联机算法实现)
    Instruments
    CALayer之 customizing timing of an animation
    PKCS填充方式
    使用Xcode和Instruments调试解决iOS内存泄露
    apple网址
  • 原文地址:https://www.cnblogs.com/thz666/p/10666328.html
Copyright © 2020-2023  润新知