2015-11-05 40 views
0

我的要求是我有一个接口,其中一个方法是声明和两个类实现相同的接口,其中一个类有返回类型Set<object type>和其他LIST<object type> ...请帮助我纠正代码错误。覆盖在接口中声明的方法的返回类型,其实现在类

这里是下面的示例代码:

报告文件 ---接口

public interface Reportfile 
{ 
    public <T> parseReadfile(); 
} 

Masterfile.java ----类一

public class Masterfile implements Reportfile 
    { 
Set<Masterpojo > mset = new TreeSet<Masterpojo>(); 

     public String[] newline = null; 
     public Set<Masterpojo> parseReadfile() 
      { 
       try{ 
        Masterpojo mo = new Masterpojo(); 
        CSVReader mread = new CSVReader(new FileReader(mfile),(delimiter)); 
        while ((newline = mread.readNext()) != null) 
         {   
          mo.setmline(newline);    
          mset.add(mo); 
         } 
       }catch(Exception e) 
        { 
         log.error("file not found"); 
        } 
       return mset; 
      } 
    } 

Transfile .java ---- class 2

public class Transfile implements Reportfile 
    { 

List<Transpojo> tlist = new ArrayList<Transpojo>(); 

     public String[] newline = null; 

     public List<Transpojo> parseReadfile() 
      { 
       try{ 
        Transpojo to = new Transpojo(); 
        CSVReader tread = new CSVReader(new FileReader(Transfile),(delimiter)); 
        while ((newline = tread.readNext()) != null) 
         { 
          to.settline(newline); 
          tlist.add(to); 
         } 
       }catch(Exception e) 
        { 
         log.error("Transaction file not found"); 
        } 
       return tlist; 
      } 
    } 

Masterpojo

public class Masterpojo 
    { 
     public String[] mline = null;  
     public String[] getmline() 
      { 
       return mline; 
      } 
     public void setmline(String[] mline) 
      { 
       this.mline = mline; 
      }  
    } 

Transpojo

public class Transpojo 
    { 

     public String[] tline = null; 

     public String[] gettline() 
      { 
       return tline; 
      } 
     public void settline(String[] tline) 
      { 
       this.tline = tline; 
      } 

    } 

我错误等贝洛当我编译在CMD的代码:

Masterfile.java:6: error: Masterfile is not abstract and does not override abstract method <E>parseReadfi 
le() in Reportfile 
public class Masterfile implements Reportfile 
    ^
    where E is a type-variable: 
    E extends Object declared in method <E>parseReadfile() 
Masterfile.java:34: error: parseReadfile() in Masterfile cannot implement <E>parseReadfile() in Reportfil 
e 
       public Set<Masterpojo> parseReadfile() 
            ^
    return type Set<Masterpojo> is not compatible with void 
    where E is a type-variable: 
    E extends Object declared in method <E>parseReadfile() 
Transfile.java:7: error: Transfile is not abstract and does not override abstract method <E>parseReadfile 
() in Reportfile 
public class Transfile implements Reportfile 
    ^
    where E is a type-variable: 
    E extends Object declared in method <E>parseReadfile() 
Transfile.java:33: error: parseReadfile() in Transfile cannot implement <E>parseReadfile() in Reportfile 
       public List<Transpojo> parseReadfile() 
            ^
    return type List<Transpojo> is not compatible with void 
    where E is a type-variable: 
    E extends Object declared in method <E>parseReadfile() 
4 errors 
+0

请你屁股界面的代码 – NecroTheif

+0

...对不起忘了。 ..请看看它.... – Svati

+0

当我编译报告文件,我得到这个错误如下:Reportfile.java:4:错误:预计 公众< T > parseReadfile(); ^ 1错误 – Svati

回答

2
public interface Reportfile<T> 
{ 
    public T parseReadFile(); 
} 

public class Masterfile implements Reportfile<Set<Masterpojo>> 
{ 
    public Set<Masterpojo> parseReadfile() 
    { 
     ... 
    } 
} 

public class Transfile implements Reportfile<List<Transpojo>> 
{ 
    public List<Transpojo> parseReadfile() 
    { 
     ... 
    } 
} 

但是,您的代码包含大量重复!看看TranspojoMasterpojo。完全一样! (除了命名) 看看MasterfileTransfile,两个parseReadFile都以完全相同的方式实现!除了一个返回Set和另一个List。这一切都可以被简化成类似(不再需要的仿制药):

public interface Reportfile 
{ 
    public Collection<Pojo> parseReadFile(); 
} 

public final class CsvFile implements Reportfile { 
    @Override 
    public Collection<Pojo> parseReadFile() { 
     Collection<Pojo> result = new ArrayList<>(); 
     String[] newline; 
     try { 
      CSVReader reader = new CSVReader(new FileReader(file), delimiter); 
      while ((newline = reader.readNext()) != null) { 
       result.add(new Pojo(newline)); 
      } 
     } catch (Exception e) { 
      log.error("file not found"); 
     } 
     return result; 
    } 
} 

随着Pojo:我现在又增加了它

public final class Pojo { 
    private final String[] line; 

    public Pojo(String[] line) { 
     this.line = line; 
    } 

    public String[] getLine() { 
     return line; 
    } 
} 
+0

编译正确....但请有人可以解释我泛型的概念......我发现很难在互联网上学习,因为我无法理解它 – Svati

+0

@Svati当你声明一个类型为T的返回值的方法时,这意味着只有在有人决定通过用任何具体类型(Integer,Double,File等)替换T来调用此方法时才能在运行时知道此类型。 – Spotted

+0

@Svati我也更新了我的答案。 – Spotted

1

你的接口应该声明方法parseReadfile(不是方法本身)的T是什么,所以如果你使它成为一个通用类型的ReportFile,你可以在实现它的类中声明它。

对于ReportFile.java

public interface Reportfile<T> { public <T> parseReadfile(); } 

对于Masterfile.java

public class Masterfile implements Reportfile<Masterpojo> 

对于Transfile.java

public class Transfile implements Reportfile<Transpojo> 

你也必须改变方法返回牛逼得不到ŧ现在像这样:

public T parseReadfile(); 
+0

很抱歉忘了添加接口文件...我现在添加了它...请看看它... – Svati

+0

更新我的答案,以适合 – NecroTheif

+0

是T是我尝试的泛型类型,因为主和trans文件类有不同的返回类型他们的parseReadfile方法。但我仍然得到报告文件的错误,如:Reportfile.java:4:错误:预计 公众< T > parseReadfile(); ^ 1错误 – Svati

相关问题