为什么没有定义条目变量如果我做了名为entries的arraylist
public class College
{
Student s = new Student();
ArrayList <Student> entries;
public College()
{
entries = new ArrayList<Student>();
}
public void add(Student s)
{
entires.add(s);
}
}
编辑所以ArrayList可用于该类吗?
答案 0 :(得分:2)
实际上,您必须在ArrayList
范围内定义class
,例如
public class College
{
Student s = new Student();
ArrayList <Student> entries;
public College()
{
entries = new ArrayList<Student>();
}
public void add(Student s)
{
entries.add(s);
}
}
所以你们所有课程都可以访问ArrayList
答案 1 :(得分:1)
条目变量在本地定义。将其定义为全局,如下所示:
public class College
{
Student s = new Student();
ArrayList <Student> entries;
public College()
{
entries = new ArrayList<Student>();
}
public void add(Student s)
{
entires.add(s);
}
}
答案 2 :(得分:0)
您的条目变量是在构造函数中本地创建的。它无法在您的方法中访问。让你参与全球。
public class College
{
Student s = new Student();
List <Student> entries;
public College()
{
entries = new ArrayList<Student>();
}
public void add(Student s)
{
entires.add(s);
}
}
答案 3 :(得分:0)
您应该将entries
变量定义为类成员,以便在类方法中访问该变量。
应该如此:
public class College {
Student s = new Student();
ArrayList <Student> entries;
public College()
{
entries = new ArrayList<Student>();
}
public void add(Student s)
{
entires.add(s);
}
}
答案 4 :(得分:0)
public class College
{
private Student s = new Student();
private List <Student> entries = ArrayList<>();
public void add(Student s)
{
entires.add(s);
}
}