zoukankan      html  css  js  c++  java
  • Lombok(1.14.8)

    @Getter / @Setter

    @Getter 和 @Setter,分别实现了 Gette r和 Setter 方法。

    package com.huey.hello.bean;
    
    import java.util.Date;
    
    import lombok.Getter;
    import lombok.Setter;
    
    public class Student {
    
        @Getter @Setter
        private int studNo;
        @Getter @Setter
        private String studName;
        @Getter @Setter
        private Date birthday;
    
        public static void main(String[] args) {
            Student stud = new Student();
            stud.setStudName("huey");
            System.out.println(stud.getStudName());    // [OUTPUT]: huey
        }
    }

    @ToString

    @ToString,实现了 toString() 方法。

    package com.huey.hello.bean;
    
    import java.util.Date;
    
    import lombok.Getter;
    import lombok.Setter;
    import lombok.ToString;
    
    @ToString
    public class Student {
    
        @Getter @Setter
        private int studNo;
        @Getter @Setter
        private String studName;
        @Getter @Setter
        private Date birthday;
    
        public static void main(String[] args) {
            Student stud = new Student();
            stud.setStudNo(10001);
            stud.setStudName("huey");
            stud.setBirthday(new Date());
            System.out.println(stud);        // [OUTPUT]: Student(studNo=10001, studName=huey, birthday=Fri Jul 31 23:01:56 CST 2015)
        }
    }

    @EqualsAndHashCode

    @EqualsAndHashCode,实现了 equals() 和 hashCode() 方法。

    package com.huey.hello.bean;
    
    import java.util.Date;
    
    import lombok.EqualsAndHashCode;
    import lombok.Getter;
    import lombok.Setter;
    
    @EqualsAndHashCode
    public class Student {
    
        @Getter @Setter
        private int studNo;
        @Getter @Setter
        private String studName;
        @Getter @Setter
        private Date birthday;
    
        public static void main(String[] args) {
            Student stud1 = new Student();
            stud1.setStudNo(10001);
            stud1.setStudName("huey");
            stud1.setBirthday(new Date());
    
            Student stud2 = new Student();
            stud2.setStudNo(10001);
            stud2.setStudName("huey");
            stud2.setBirthday(new Date());
    
            System.out.println(stud1.hashCode());        // [OUTPUT]: -349449895
            System.out.println(stud2.hashCode());        // [OUTPUT]: -349449895
            System.out.println(stud1.equals(stud2));     // [OUTPUT]: true
        }
    }

    @Data

    @Data,相当上述三个 annotation 以及 @RequiredArgsConstructor。

    package com.huey.hello.bean;
    
    import java.util.Date;
    
    import lombok.Data;
    
    @Data
    public class Student {
    
        private int studNo;
        private String studName;
        private Date birthday;
    
        public static void main(String[] args) {
            Student stud1 = new Student();
            stud1.setStudNo(10001);
            stud1.setStudName("huey");
            stud1.setBirthday(new Date());
    
            Student stud2 = new Student();
            stud2.setStudNo(10001);
            stud2.setStudName("huey");
            stud2.setBirthday(new Date());
    
            System.out.println(stud1.hashCode());        // [OUTPUT]: -349006373
            System.out.println(stud2.hashCode());        // [OUTPUT]: -349006373
            System.out.println(stud1.equals(stud2));     // [OUTPUT]: true
            System.out.println(stud1);                   // [OUTPUT]: Student(studNo=10001, studName=huey, birthday=Fri Jul 31 23:13:43 CST 2015)
        }
    }
  • 相关阅读:
    Visual Stdio VS 错误 error : 0xC00000FD: Stack overflow. 更改堆栈空间解决栈溢出问题
    OpenCV Mat 只能用静态数组定义时初始化,动态数组赋值给Mat需要逐元素进行. MATLAB,OpenCV,VS混合编程
    【转】Ubuntu 10.10升级显卡驱动后开机动画低分辨率问题
    linux 文件[名]编码
    L337 Speak及国外论坛、IRC常用缩写
    UCS2 手机SMS的PDU编码
    setuid
    【转】CentOS5.5硬盘安装
    SWT CTabFolder 简记
    [转] 程序员的十层楼
  • 原文地址:https://www.cnblogs.com/huey/p/4693389.html
Copyright © 2011-2022 走看看