-2

在此处输入图像描述我有一个对象的自定义类,如下所示:

public class StudentState implements Serializable {

    private static final long serialVersionUID = -3001666080405760977L;

    public CourseState CourseState;

    public class CourseState implements Serializable {

        private static final long serialVersionUID = -631172412478493444L;

        public List<Lessonstates> lessonstates;

    }

    public class Lessonstates implements Serializable {

        private static final long serialVersionUID = -5209770078710286360L;
        public int state;

    }
}

现在我想在我的代码中初始化 Lessonstates 以使用它。我已经这样做了,但它有一个错误:

CourseState state = test.new CourseState();
Lessonstates newLesson = state.new Lessonstates(); 

我也试过这个:

 Lessonstates newLesson = new CourseState().new Lessonstates(); 

错误是 StudentState.CourseState.Lessonstates 无法解析为一个类型有没有人可以帮我解决它?

4

2 回答 2

3

这很简单:

CourseState state = new CourseState();
state.lessonstates = new ArrayList<Lessonstates>();

需要先分配对象,然后才能访问它们。分配后,您可以使用.(点)符号访问他的成员

于 2013-11-01T19:22:47.600 回答
1

您只需Lessonstates要从 a实例化StudentState,与 相同CourseState

StudentState test = new StudentState();
CourseState state = test.new CourseState();
Lessonstates newLesson = test.new Lessonstates();

由于CourseStateLessonstates都是StudentState.

否则,您可以将我们的内部类从 中取出StudentState,或者将它们设为静态以便能够在没有StudentState实例的情况下实例化它们。

于 2013-11-01T20:00:32.927 回答