2017-06-16 74 views
2
最後一類

實現一個接口的類這可能是一個愚蠢的問題,但我發現在這裏混淆。我有以下情況:的Java:在

Main.java

public class Main { 

    public static void main (String args[]){ 
     GenericTag[] arr = new GenericTag[2]; 
     arr[0] = new Authentication("", "", "", ""); 
     arr[1] = new Document("", "", "", ""); 
     byte[] foo= Base64.decodeBase64(XmlBuilder.generate(arr)); 
     System.out.println(new String(foo)); 
    } 

XmlBuilder.java

public final class XmlBuilder { 
    private static final String OPEN_TAG = ""; 
    private static final String CLOSE_TAG = ""; 

    public static byte[] generate(GenericTag[] tags){ 

     String xml = OPEN_TAG; 
     for(int i=0; i<tags.length; i++){ 
      xml += tags[i].xml; 
     } 
     xml += CLOSE_TAG; 

     return Base64.encodeBase64(xml.getBytes()); 
    } 

    public interface GenericTag{ 
     public String getXml(); 
    } 

    public class Authentication implements GenericTag{ 
     private static final String OPEN_TAG = "<authentication>"; 
     private static final String CLOSE_TAG = "</autentication>"; 
     //some tags 

     public Authentication (/*some parameters*/){ 
      xml = OPEN_TAG; 
      //xml building 
      xml += CLOSE_TAG; 
     } 

     @Override 
     public String getXml() { 
      return xml; 
     } 
    } 

    public class Document implements GenericTag{ 
     private static final String OPEN_TAG = "<document>"; 
     private static final String CLOSE_TAG = "</document>"; 
     //some tags 

     public String xml; 

     public Documento (/*some params*/){ 
      xml = OPEN_TAG; 
      //xml building 
      xml += CLOSE_TAG; 
     } 
    @Override 
    public String getXml() { 
     return xml; 
    } 
    } 
} 

我無法得到它的工作。編譯器說認證和文檔都不能解析爲一種類型。如果我明確說明new XmlBuilder.Authentication它說

No enclosing instance of type XmlBuilder is accessible. Must qualify the allocation with an enclosing instance of type XmlBuilder (e.g. x.new A() where x is an instance of XmlBuilder). 

我在做什麼錯在這裏?

+1

你確定你的意思將身份驗證作爲XmlBuilder的內部類來使用?如果不是的話,你應該在每個接口/類中聲明它自己的文件。 –

+0

是的,我知道它把每一類在它自己的文件將解決averything,但我想在一個類在這種情況下的一切。 –

回答

5

製作類AuthenticationDocumentpublic static。由於這些都不是static,你只能從XmlBuilder實例實例化它們。

Java inner class and static nested class - 你可以找到更多的信息在這裏

+1

只是想發表意見所缺少的單詞「實例」,當您添加它.... –

0

你試着讓內部類抽象類的一個實例。 使這個類獨立。

+0

是的,我知道它把每一類在它自己的文件將解決averything,但我想在一個類在這種情況下的一切。不管怎樣,謝謝你 –

0

首先,你有一些語法錯誤,第二,你需要做的內部類靜態的,因此它們不依賴於外部實例,但站在自己。您需要重構它們,以便不依賴父類的任何字段。更好的風格應該是讓他們在自己的源文件,但在一個文件中,你的代碼的編譯版本是:

import java.util.Base64; 

public abstract class XmlBuilder { 
    private static final String OPEN_TAG = ""; 
    private static final String CLOSE_TAG = ""; 
    private String xml; 

    public static byte[] generate(GenericTag[] tags){ 

     String xml = OPEN_TAG; 
     for(int i=0; i<tags.length; i++){ 
      xml += tags[i].getXml(); 
     } 
     xml += CLOSE_TAG; 

     return Base64.getEncoder().encode(xml.getBytes()); 
    } 

    public static interface GenericTag{ 
     public String getXml(); 
    } 

    public static class Authentication implements GenericTag{ 
     private static final String OPEN_TAG = "<authentication>"; 
     private static final String CLOSE_TAG = "</autentication>"; 
     private static String xml; 
     //some tags 

     public Authentication (/*some parameters*/){ 
      xml = OPEN_TAG; 
      //xml building 
      xml += CLOSE_TAG; 
     } 

     @Override 
     public String getXml() { 
      return xml; 
     } 
    } 

    public static class Document implements GenericTag{ 
     private static final String OPEN_TAG = "<document>"; 
     private static final String CLOSE_TAG = "</document>"; 
     //some tags 

     public String xml; 

     public Document (/*some params*/){ 
      xml = OPEN_TAG; 
      //xml building 
      xml += CLOSE_TAG; 
     } 
    @Override 
    public String getXml() { 
     return xml; 
    } 
    } 
} 

和主類:

import java.util.Base64; 

public class Main { 

    public static void main(String[] args) 
    { 
     XmlBuilder.GenericTag[] arr = new XmlBuilder.GenericTag[2]; 
     arr[0] = new XmlBuilder.Authentication(); 
     arr[1] = new XmlBuilder.Document(); 
     byte[] foo= Base64.getDecoder().decode(XmlBuilder.generate(arr)); 
     System.out.println(new String(foo)); 
    } 

}