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();
};
}
}
如何有效删除重复的代码。也许我应该考虑使用其他类对象中定义的静态方法?或者有更好的方法来解决我的问题?
public String toString() {
return "." + super.toString().toLowerCase();
};
答案 0 :(得分:3)
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();
}
}