What are the shortcut to Auto-generating toString Method in Eclipse?

后端 未结 9 1323
遥遥无期
遥遥无期 2020-12-03 13:17

Is it good or bad practice auto-generating toString methods for some simple classes?

I was thinking of generating something like below where it takes th

9条回答
  •  一个人的身影
    2020-12-03 14:14

    Considering some old answers including @Steve's, I'd like to add answer as per latest library.

    Add dependency

            
                org.apache.commons
                commons-lang3
                3.10
            
    

    In your class

    import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
    
    public class User {
         ... 
    
         @Override
         public String toString() {
              return ReflectionToStringBuilder.toString(this);
         }
    }
    

    You can exclude certain fields as below

        ... 
        @Override
        public String toString() {
            return ReflectionToStringBuilder.toStringExclude(this, "name"); // Name will be excluded from toString output 
        }
    

提交回复
热议问题