愚かな質問かもしれませんが、ここでは混乱しています。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).
は、私がここで間違ってやっている何を言いますか?
はあなたが意図してください。認証をXmlBuilderの内部クラスとして持つことはできますか?そうでなければ、各インタフェース/クラスをそれ自身のファイルに宣言する必要があります。 –
はい私はそれ自身のファイルに各クラスを入れておくことですべてを解決することができますが、この場合はすべてを1つのクラスに収めたいと思っています。 –