Java- 基本封裝

來源:互聯網
上載者:User

標籤:ceo   pack   自身   電腦   語言   大於   int   eth   override   

package test.studet_manager;public class Student {    static int num = 100;    // 編號-->唯一的    private  int number = -1;    //姓名    private String name = "";    //性別    private String sex = "";    //年齡    private int age = -1;    //班級    private String grade = "";    //成績    private int score = -1;    /*     *  set和 get的方法     */    public void setSex(String sex) {        this.sex = sex;    }        public String getSex() {        return sex;    }        public void setName(String name) {        this.name = name;    }        public String getName() {        return name;    }        public void setAge(int age) {        // 年齡必須大於零        if (age>=0) {            this.age = age;        }            }        public int getAge() {        return age;    }        public void setGrade(String grade) {                this.grade = grade;    }        public String getGrade() {        return grade;    }        public void setScore(int score) {        if (score >= 0) {            this.score = score;        }            }        public int getScore() {        return score;    }        public void setNumber(int number) {        this.number =this.number+0;    }    public int getNumber(){        return number;    }        public Student() {        // TODO Auto-generated constructor stub        ++num;        this.number = num;    }    public  Student(int number,String name,String sex,int age,String grade,int score) {        getNumber();        getName();        getSex();        getAge();        getGrade();        getScore();    }     @Override    public String toString() {        // TODO Auto-generated method stub        return ""+this.number+"  "+this.name+"  "+this.sex+"  "+this.age+"  "+this.grade+"  "+this.score+"\n";    }        @Override    public boolean equals(Object obj) {        // TODO Auto-generated method stub        // 判斷要比較的對象是不是null        if (obj == null) {            return false;        }        //判斷要比較的對象是不是自己本身        if (obj == this) {            return true;        }        // 判斷要比較的對象是不是同一類型        if (obj instanceof Student) {            Student student = (Student)obj;            return student.number==this.number&&student.name==this.name&&student.sex==this.sex&&student.age==this.age&&student.grade==this.grade&&student.score==this.score;        }        return false;    }    @Override    // 注意:hashCode方法與equals方法中的屬性必須一致    public int hashCode() {        // TODO Auto-generated method stub        return this.number-this.name.hashCode()-this.sex.hashCode()-this.age-this.grade.hashCode();    }    // 寫一些自身的方法    public void learn() {        System.out.println(this.name+"在學習電腦語言");    }}

 

Java- 基本封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.