2017-06-16 9 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

はい私はそれ自身のファイルに各クラスを入れておくことですべてを解決することができますが、この場合はすべてを1つのクラスに収めたいと思っています。 –

答えて

5

クラスを作るAuthenticationDocumentpublic static。これらはstaticではないため、XmlBuilderインスタンスからのみインスタンス化できます。

Java inner class and static nested class - あなたはより多くの情報を見つけることができますが、ここで

+1

あなたが追加したときに不足している単語 "インスタンス"をコメントしたかっただけです.... –

0

あなたは抽象クラスの内部クラスのインスタンスを作成してみてください。 このクラスを独立させる。

+0

はい、私はそれがそれぞれのクラスのファイルをaverythingを解決すると知っていますが、この場合はすべてを1つのクラスに入れたいと思います。とにかくありがとうございました –

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)); 
    } 

} 
関連する問題