英文:
Enclosing instance required that contains outerclass.innerclass
问题
Student.Student_Card obj4 = Student.new Student_Card(namess, idss, true);
Student
类是外部类,Student_Card
是嵌套类,在运行这段代码时,出现错误:
需要包含Student.Student_Card
的封闭实例。
英文:
Student.Student_Card obj4 = Student.new Student_Card(namess, idss, true);
Student class is an outer class and Studen_Card is nested class,when i am running this code i am getting error that:
an enclosing instance that contains Student.Student_Card is required
答案1
得分: 1
你需要这样做:
Student.Student_Card obj4 = new Student.Student_Card(namess, idss, true);
英文:
You need to do it like this:
Student.Student_Card obj4 =new Student.Student_Card(namess, idss, true);
答案2
得分: 0
如果您想消除错误,请将内部类标记为static
,如下所示:static class Student_Card
否则,请注意内部类需要外部类的实例,如下所示:
Student student = new Student();
Student_Card studentCard = student.new Student_Card();
英文:
If you want to get rid of the error, mark the inner class with static
, as so: static class Student_Card
Otherwise, note that inner classes require an instance of the outer class, like so:
Student student = new Student();
Student_Card studentCard = student.new Student_Card()
答案3
得分: 0
如果你的Student_Card
类不是静态的,那么你需要一个Student
的实例来创建它。
Student student = new Student();
Student.Student_Card obj4 = student.new Student_Card(namess, idss, true);
如果你将Student_Card
类设置为静态,那么你根本不需要引用Student
类,下面的代码就可以工作:
Student.Student_Card card = new Student.Student_Card(namess, idss, true);
你可以在这个帖子中阅读更多。
英文:
If your Student_Card
class is not static, then you need an instance of Student in order to create it.
Student student = new Student();
Student.Student_Card obj4 = student.new Student_Card(namess, idss, true);
If you will make class `Student_Card static, then you don't need any reference to your Student class at all, this will work:
Student.Student_Card card= new Student.Student_Card(namess, idss, true);
You can read more in this thread.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论