使用Commons-lang构建类

package com.entity.bean;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class Student {
   
    private int age ;
   
    private String name ;
   
    private String score ;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public boolean equals(Object arg0) {
        return EqualsBuilder.reflectionEquals(this, arg0);
    }
   
    public Student() {
        super();
    }

    public Student(int age, String name, String score) {
        super();
        this.age = age;
        this.name = name;
        this.score = score;
    }

   
    public static void main(String[] args) {
        Student stu1 = new Student(15 , "xiaoming" , "100");
        Student stu2 = new Student(18 , "zhangsan" , "89");
        System.out.println("stu1" + " : " + stu1);
        System.out.println("stu2" + " : " + stu2);
        System.out.println(stu1.equals(stu2) ? "They are equals" : "Ther are not Equals");
        System.out.println("stu1 hashcode : " + stu1.hashCode());
        System.out.println("stu2 hashcode : " + stu2.hashCode());
       
    }
   
}

你可能感兴趣的:(apache,bean)