这可能是一个愚蠢的问题,但我在这里感到困惑。我有以下情况:
Main.java
var tbody = $('.zaawansowana_edycja').find('tbody');
for (var x = 0, len = Object.keys(r.konfiguracja.data).length; x < len; x++) {
(function(idx) {
var obj = r.konfiguracja.data[idx];
var obj_y = $('.konfiguratorHeaderTable').find('td[data-krzeselkoid="' + obj.id_krzesla + '"]').index();
var konf_cache = tbody.find('tr.konfiguratorAttr[key="' + obj.TKA1.id_tkaniny + '|' + obj.TKA2.id_tkaniny + '"]');
if (!konf_cache.length) {
var newHtml = $('<tr>');
newHtml.addClass('konfiguratorAttr').attr('key', obj.TKA1.id_tkaniny + '|' + obj.TKA2.id_tkaniny);
for (var t1 = 0; t1 < $('.konfiguratorHeaderTable').find('.obr').find('td').length; t1++) {
newHtml.append('<td>');
}
tbody.append(newHtml);
}
konf_cache.find('td:first').html('<div class="col-sm-12"><select>' + selectHtml.html() + '</select></div><div class="col-sm-12"><select>' + selectHtml.html() + '</select></div>');
var h2 = $('<div>');
h2.addClass('col-sm-12');
h2.append($('<input>').attr('type', 'text'));
h2.append($('<input>').attr('type', 'text'));
h2.append($('<input>').attr('type', 'text'));
h2.append($('<input>').attr('type', 'text'));
h2.append($('<input>').attr('type', 'text'));
h2.append($('<input>').attr('type', 'text'));
h2.append($('<input>').attr('type', 'text'));
h2.append($('<input>').attr('type', 'text'));
h2.append($('<input>').attr('type', 'text'));
h2.append($('<input>').attr('type', 'text'));
h2.append($('<input>').attr('type', 'text'));
h2.append($('<input>').attr('type', 'text'));
h2.append($('<input>').attr('type', 'text'));
konf_cache.find('td').eq(obj_y).append(h2.html());
})(x);
}
XmlBuilder.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));
}
我无法让它发挥作用。编译器说无法将Authentication和Document都解析为类型。如果我明确说明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
我在这里做错了什么?
答案 0 :(得分:5)
制作课程Authentication
和Document
public static
。由于这些不是static
,因此您只能从XmlBuilder
实例中实例化它们。
Java inner class and static nested class - 您可以在此处找到更多信息
答案 1 :(得分:0)
您尝试创建抽象类的内部类的实例。 使这个类独立。
答案 2 :(得分: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));
}
}