简体   繁体   中英

how to remove the duplicated code for the code example in java?

package org.test.toolkit.file;

public interface FileType {

    public enum Image implements FileType {

        JPG;

        public String toString() {  //duplicated code
            return "." + super.toString().toLowerCase();
        };
    }

    public enum Office implements FileType {

        DOC;

        public String toString() {
            return "." + super.toString().toLowerCase();
        };
    }

    public enum PlainText implements FileType {

        TXT;

        public String toString() {  //duplicated code
            return "." + super.toString().toLowerCase();
        };
    }

}

How to efficiently remove duplicate code. Perhaps i should consider using a static method defined in other class object? Or is there a better way to solve my problem?

    public String toString() {
        return "." + super.toString().toLowerCase();
    };
public enum Type
{
  Image,Document,Text;
}
public enum FileType
{
   DOC(Type.Document), JPG(Type.Image),GIF(Type.Image),TXT(Type.Text);
   private Type type;
   private FileType(Type t)
   {
      this.type=t;
   }
   public Type getType()
   {
      return type;
   }
   public String toString()
   {
       return "." + super.toString().toLowerCase();
   }
}

?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM