【问题标题】:Custom object and use it自定义对象并使用它
【发布时间】:2013-11-01 19:21:16
【问题描述】:

我有一个对象的自定义类,如下所示:

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 无法解析为类型 有谁可以帮我解决它?

【问题讨论】:

    标签: java android class object


    【解决方案1】:

    很简单:

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

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

    【讨论】:

    • 那么我该如何使用 NewLesson 呢?例如我如何设置 NewLesson.state = 1 ; ? @blackbelt
    • Lessonstates courseState = new Lessonstates();课程状态.state = 1;
    • @blackbely 但它说:无法访问 StudentState 类型的封闭实例。必须使用 StudentState 类型的封闭实例来限定分配
    • 发布确切的代码。无法理解发生了什么。规则总是一样的,它是基本的java。它与 android 无关
    • 为什么是图片?你不能简单的复制/粘贴吗?什么是 test.new CourseState(); ?
    【解决方案2】:

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

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

    因为CourseStateLessonstates 都是StudentState 的内部类。

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

    【讨论】:

      猜你喜欢
      • 2016-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多