码迷,mamicode.com
首页 > 编程语言 > 详细

Java- 基本封装

时间:2016-11-18 22:44:16      阅读:161      评论:0      收藏:0      [点我收藏+]

标签: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- 基本封装

标签:ceo   pack   自身   计算机   语言   大于   int   eth   override   

原文地址:http://www.cnblogs.com/lantu1989/p/6078834.html

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