java - 尝试输出实例类中链接列表的内容时,无法从静态上下文错误中引用方法

标签 java static linked-list

我是一名初学者 Java 程序员,我正在尝试检查我是否已成功将存储在类 Clients 中的链表中的 Person 实例复制到类 Boat 实例内名为 Passengers 的链表中,方法是使用 Boat打印出其链接列表的内容。

我正在使用Boat类中的givePassengers()方法来让Boat类打印出它的乘客链接列表内容。

但是,当我尝试这样做时,我遇到了错误“无法从静态上下文引用非静态方法 GivePassengers()”,并且我不确定要编写什么来解决该问题。我在下面列出了我认为是问题的代码。非常感谢任何帮助。

我已经用“//!!”标记了重要的代码

这是包含船只链接列表的类

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
    }

这里是船级

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.

        }

这是 Person 类

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 链接列表的类

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++));  

    }
)

如果有帮助的话,这是根类

import java.io.*;

public class Root
{   public Root() {

        new Base();

    }

    public static void main(String[] args)
    {   new Root(); }


    private Base base;
}

最佳答案

您的问题在这里:

for (Boat i: boats) 
   Boat.givePassengers(); // !! This line produces the error

您需要说i.givePassengers()来引用Boat类的特定实例。关于类和对象之间的区别,有一本很好的入门书 here .

关于java - 尝试输出实例类中链接列表的内容时,无法从静态上下文错误中引用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29482291/

相关文章:

java - 当逗号包含在java中时如何用逗号分割线

java - Java的Thread.sleep什么时候抛出InterruptedException?

c++ - 静态成员变量的返回引用c++

c - 局部静态变量和多线程——安全吗?

java - 实现linkedStack时出现NullPointerException

C 链表插入和显示功能不起作用

比较两个链表中的各个值 C 程序

java - 使用 ant build 创建的 jar 出现 NoClassDefFound 错误

java - 用Java将BsonDocument写入文件

java - 这段代码示例中 static 有什么用?