需要包含外部类.inner类的封闭实例。

huangapple go评论161阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年4月9日 14:58:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/61115562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定