相关疑难解决方法(0)

我应该将Java中的静态嵌套类重构为单独的类吗?

我继承了包含静态嵌套类的代码:

public class Foo {

// Foo fields and functions
// ...
    private static class SGroup {
        private static Map<Integer, SGroup> idMap = new HashMap<Integer, SGroup>();

        public SGroup(int id, String type) {
// ...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

从阅读SO(例如Java内部类和静态嵌套类),我相信这相当于两个单独文件中的两个单独的类:

 public class Foo {

    // Foo fields and functions
    // ...
}
Run Code Online (Sandbox Code Playgroud)

public class SGroup {
    static Map<Integer, SGroup> idMap = new HashMap<Integer, SGroup>();

    public SGroup(int id, String type) {
// ...
    }
}
Run Code Online (Sandbox Code Playgroud)

如果这是正确的,维护静态嵌套类结构有什么好处,还是应该重构?

java static nested class

2
推荐指数
2
解决办法
1750
查看次数

Spring @Requestbody没有映射到内部类

我正在使用Spring 4.x进行Spring Rest Api项目

这个作品:

Controller.java

@PostMapping("newTransaction")
TransactionRequestModel insertNewTransaction(@RequestBody TransactionRequestModel model){
    //do something
}
Run Code Online (Sandbox Code Playgroud)

TransactionRequestModel.java

public class TransactionRequestModel {
    private int id;
    private List<KeyValue> keyValueList;
    public TransactionRequestModel(){}
    //default constructor
    //getter-setter 
}
Run Code Online (Sandbox Code Playgroud)

KeyValue.java

public class KeyValue {

    String key;
    String value;
    //default constructor
    //setter-getter
}
Run Code Online (Sandbox Code Playgroud)

请求身体Json

{
  "id": 1 
  "keyValueList": [
    {
      "key": "dummy",
      "value": "dummy"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

使用jackson的Spring消息转换器工作正常.

这不会:

当我将TransactionRequestModel.java更改为以下(并删除KeyValue.java)时

public class TransactionRequestModel {

    public class KeyValue {     
      String key;
      String value;
      //default constructor
      //setter-getter …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc

2
推荐指数
1
解决办法
3091
查看次数

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));
    }
Run Code Online (Sandbox Code Playgroud)

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());
    } …
Run Code Online (Sandbox Code Playgroud)

java interface class

2
推荐指数
1
解决办法
327
查看次数

带/不带 baseObservable 的 android viewmodel

我试图在 android 中查看模型,所以对于 MainViewModel.java 我写了这个:


public class MainViewModel extends ViewModel {

    private String textView;
    private String editText;

    //@Bindable
    public String getTextView(){
        return textView;
    }

    private void setTextView(String value){
        textView=value;
    }

    //@Bindable
    public TextWatcher getEditTextWatcher() {
        return new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setTextView(s.toString());
            }
            ...
        };
    }

}
Run Code Online (Sandbox Code Playgroud)

在 ActivityMain.xml 中我写了这样的内容:


        <TextView
            android:text="View-Model / Data-Binding"
            android:layout_width="match_parent"
            android:layout_height="40dp"/>

        <TextView
            android:id="@+id/main_text_view"
            android:text="@{mainvm.textView}"
            android:layout_width="match_parent"
            android:layout_height="40dp"/>

        <EditText
            android:id="@+id/main_edit_text"
            app:textChangeListener="@{mainvm.editTextWatcher}"
            android:layout_width="match_parent"
            android:layout_height="40dp"/>
Run Code Online (Sandbox Code Playgroud)

我收到 2 个错误:

Cannot …
Run Code Online (Sandbox Code Playgroud)

java android mvvm viewmodel

2
推荐指数
1
解决办法
4628
查看次数

如何使用静态方法定义嵌套静态类,从Java中的嵌套接口继承?

我有嵌套类的Java问题.

我的第一类结构看起来像这样:

public class TopClass {

    public void mainMethod() {
        // uses the different "method" methods from 
        // NestedClass-implementing nested classes
    }            

    private interface NestedClass {
        public void method();
    }    

    private class NestedClass1 {
        public void method() {
        }
    }    

    private class NestedClass2 {
        public void method(){
        }
    }    
}
Run Code Online (Sandbox Code Playgroud)

但是现在我希望这些method()方法是静态的,因为它们应该是主要的.

如果不将它们放在静态类中,我就无法使它们成为静态,但这没有问题,我使这些类保持静态,它们应该是无论如何.

它现在看起来像这样:

public class TopClass {

    public void mainMethod() {
        // uses the different "method" methods from 
        // NestedClass-implementing nested classes
    }            

    private static interface NestedClass { …
Run Code Online (Sandbox Code Playgroud)

java static nested interface abstract

1
推荐指数
1
解决办法
1371
查看次数

can we create static class in Java web application?

I am trying to create static class in Java Web Application using eclipse. but it says

Illegal modifier for the class ClassName; only public, abstract & final are permitted
Run Code Online (Sandbox Code Playgroud)

can we create static class in web application ? if no why ?

java servlets

1
推荐指数
1
解决办法
1642
查看次数

在另一个类中创建内部类对象的数组

请考虑以下嵌套类.

class Outerclass {

    class innerclass {

    }

}

 class util {

 //how to declare an array of innerclass objects here?
}
Run Code Online (Sandbox Code Playgroud)

java syntax class nested-class inner-classes

1
推荐指数
1
解决办法
4647
查看次数

什么是静态嵌套类?

可能重复:
Java:静态与非静态内部类

什么是静态嵌套类?静态和非静态嵌套类有什么区别?

java inner-classes

0
推荐指数
1
解决办法
4782
查看次数

外部和内部类和静态方法

我理解一个内部类是非静态的,而外部类的静态方法不能引用它.

我有这个代码,这不起作用,我理解为什么这不起作用.

class OuterClass {
    class InnerClass{}
    public static void outherMethod() {
        InnerClass i = new InnerClass();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是后来我有了其他代码,它可以工作,但我不明白为什么它与第一个不同.它为什么有效?

class OuterClass {
    class InnerClass{}
    public static void outherMethod() {
        InnerClass i = new OuterClass.new InnerClass();
    }
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

编辑:它没有重复,因为它不是同一个问题.我不是在问静态嵌套类,我问的是静态方法和内部类

java static-methods inner-classes

0
推荐指数
1
解决办法
78
查看次数

如何让java允许像Map.Entry这样的名字?我以为不允许有点

如何在此接口名称中允许使用点,我无法创建名称中带有点的另一个类/接口名称.

java hashmap

0
推荐指数
1
解决办法
112
查看次数