在实例 class 中尝试输出链接列表的内容时无法从静态上下文错误中引用方法
Cannot reference method from a static context error when attempting to output contents of linked list in instance class
我是初学者 Java 程序员,我正在尝试检查我是否已成功将存储在 class 客户端中的链接列表中的 Person 实例复制到名为class Boat 实例中的乘客,方法是让 Boat 打印出其链表的内容。
我正在使用 class Boat 中的 givePassengers() 方法让 Boat class 打印出乘客链表内容。
然而,当我尝试这样做时,我遇到了错误 'non-static method givePassengers() cannot be referenced from a static context',我不确定要写什么来解决这个问题。我在下面列出了我认为是问题的代码。非常感谢任何帮助。
我已经用'// !!'标记了重要代码
这是包含船
链表的class
import java.io.*;
import java.util.*;
public class Base implements Serializable
{ private LinkedList<Boat> boats = new LinkedList<Boat>();
private Clients clients = new Clients();
public void setup() // !! Here are the instances of the boat class
{ boats.add(new Boat(1, "Ed", 2));
boats.add(new Boat(2, "Fred", 7));
boats.add(new Boat(3, "Freda", 5)); }
public void showpassengers() {
for (Boat i: boats) `// !! The for each loop cycles through each boat to check for passengers`
Boat.givePassengers(); // !! This line produces the error
}
这是船class
import java.io.*;
import java.util.*;
public class Boat implements Serializable
{ private int id;
private String pilot;
private int stops;
private LinkedList<Person> passengers = new LinkedList<Person>();
private double rate = 10.00;
public int scannableId = this.id;
public Boat(int id, String pilot, int stops)
{ this.id = id;
this.pilot = pilot;
this.stops = stops; }
public void givePassengers () {
System.out.println(passengers); // !! this line is supposed to print out the contents of the boat classes' linked list so I can check that it worked.
}
这是人class
import java.io.*;
import java.text.*;
public class Person implements Serializable
{ private String name;
private int id;
private double cash = 100.00;
private int start = 0;
private int end = 0;
private double charge = 0;
public Person(String name, int id)
{ this.name = name;
this.id = id + 100; }
}
这是包含 Person
链表的 class
import java.io.*;
import java.util.*;
public class Clients implements Serializable
{ private LinkedList<Person> clients = new LinkedList<Person>();
private int id = 1;
public Clients() `// !! Here are the instances of Person in the class clients`
{ clients.add(new Person("Homer", id++));
clients.add(new Person("Marge", id++));
}
)
这里是根 class 如果有帮助的话
import java.io.*;
public class Root
{ public Root() {
new Base();
}
public static void main(String[] args)
{ new Root(); }
private Base base;
}
您不能从 Boat 静态调用 givePassengers(),它不是静态方法。您需要从 Boat 的实例中调用它。在 foreach 循环中将 Boat.givePassengers();
替换为 i.givePassengers();
。这将导致当前选择的 Boat 实例 运行 givePassengers().
您的问题在这里:
for (Boat i: boats)
Boat.givePassengers(); // !! This line produces the error
您需要说 i.givePassengers()
来引用 class Boat
的特定实例。关于 classes 和对象 here.
之间的区别,有一本很好的入门书
我是初学者 Java 程序员,我正在尝试检查我是否已成功将存储在 class 客户端中的链接列表中的 Person 实例复制到名为class Boat 实例中的乘客,方法是让 Boat 打印出其链表的内容。
我正在使用 class Boat 中的 givePassengers() 方法让 Boat class 打印出乘客链表内容。
然而,当我尝试这样做时,我遇到了错误 'non-static method givePassengers() cannot be referenced from a static context',我不确定要写什么来解决这个问题。我在下面列出了我认为是问题的代码。非常感谢任何帮助。
我已经用'// !!'标记了重要代码
这是包含船
链表的classimport java.io.*;
import java.util.*;
public class Base implements Serializable
{ private LinkedList<Boat> boats = new LinkedList<Boat>();
private Clients clients = new Clients();
public void setup() // !! Here are the instances of the boat class
{ boats.add(new Boat(1, "Ed", 2));
boats.add(new Boat(2, "Fred", 7));
boats.add(new Boat(3, "Freda", 5)); }
public void showpassengers() {
for (Boat i: boats) `// !! The for each loop cycles through each boat to check for passengers`
Boat.givePassengers(); // !! This line produces the error
}
这是船class
import java.io.*;
import java.util.*;
public class Boat implements Serializable
{ private int id;
private String pilot;
private int stops;
private LinkedList<Person> passengers = new LinkedList<Person>();
private double rate = 10.00;
public int scannableId = this.id;
public Boat(int id, String pilot, int stops)
{ this.id = id;
this.pilot = pilot;
this.stops = stops; }
public void givePassengers () {
System.out.println(passengers); // !! this line is supposed to print out the contents of the boat classes' linked list so I can check that it worked.
}
这是人class
import java.io.*;
import java.text.*;
public class Person implements Serializable
{ private String name;
private int id;
private double cash = 100.00;
private int start = 0;
private int end = 0;
private double charge = 0;
public Person(String name, int id)
{ this.name = name;
this.id = id + 100; }
}
这是包含 Person
链表的 classimport java.io.*;
import java.util.*;
public class Clients implements Serializable
{ private LinkedList<Person> clients = new LinkedList<Person>();
private int id = 1;
public Clients() `// !! Here are the instances of Person in the class clients`
{ clients.add(new Person("Homer", id++));
clients.add(new Person("Marge", id++));
}
)
这里是根 class 如果有帮助的话
import java.io.*;
public class Root
{ public Root() {
new Base();
}
public static void main(String[] args)
{ new Root(); }
private Base base;
}
您不能从 Boat 静态调用 givePassengers(),它不是静态方法。您需要从 Boat 的实例中调用它。在 foreach 循环中将 Boat.givePassengers();
替换为 i.givePassengers();
。这将导致当前选择的 Boat 实例 运行 givePassengers().
您的问题在这里:
for (Boat i: boats)
Boat.givePassengers(); // !! This line produces the error
您需要说 i.givePassengers()
来引用 class Boat
的特定实例。关于 classes 和对象 here.