【问题标题】:Linked list from different class nodes来自不同类节点的链表
【发布时间】:2016-05-06 05:03:28
【问题描述】:

我正在尝试为模拟杂货市场的篮子的分配创建一个链接列表,其中篮子是链接列表,其中的节点(链接)是每个产品。下面的代码代表篮子(LinkedList)和一些产品 Gouda 和 Bacon。我的问题是如何从这两个节点中创建一个链表,这样我就可以获得像 Gouda->Bacon->Bacon->Gouda 等这样的 LinkedList ?

p.s 先谢谢你

public class Test {
    public static void main(String[] args) {
    LinkedList theLinkedList = new LinkedList();
    theLinkedList.insertFirstLink();
    theLinkedList.display();
   }
}

class LinkedList{

// Reference to first Link in list
// The last Link added to the LinkedList

public Gouda firstLink; 

LinkedList(){

    // Here to show the first Link always starts as null

    firstLink = null;

}

// Returns true if LinkList is empty

public boolean isEmpty(){

    return(firstLink == null);

}

public void insertFirstLink(){

    Gouda newLink = new Gouda();

    // Connects the firstLink field to the new Link 

    newLink.next = firstLink;

    firstLink = newLink;

}


public void display(){

    Gouda theLink = firstLink;

    // Start at the reference stored in firstLink and
    // keep getting the references stored in next for
    // every Link until next returns null

    while(theLink != null){

        theLink.display();

        System.out.println("Next Link: " + theLink.next);

        theLink = theLink.next;

        System.out.println();

    }

    }
}

public class Gouda{

// Set to public so getters & setters aren't needed

    public String category= "Dairy";
    public String productName = "Gouda Cheese";
    public double price = 57.50;

    // Reference to next link made in the LinkList
    // Holds the reference to the Link that was created before it
    // Set to null until it is connected to other links

    public Gouda next; 

    public void display(){

        System.out.println(category+" :"+ productName +""+ price);

    }   
}

public class Bacon{

// Set to public so getters & setters aren't needed

    public String category= "Meat";
    public String productName = "Thick cut Bacon";
    public double price = 5.50;

    // Reference to next link made in the LinkList
    // Holds the reference to the Link that was created before it
    // Set to null until it is connected to other links

    public Bacon next; 

    public void display(){

        System.out.println(category+" :"+ productName +""+ price);

    }   
}

【问题讨论】:

  • 在 java 中,通过将列表的每个链接都作为类、Node 之类的容器来解决,该容器具有值字段,例如 Object(Gouda 和 Bacon 是 Objects)等等。
  • 你自己的LinkedList实现?为什么不喜欢默认的 Java 的 LinkedList?关于您的问题 - 考虑将 Gouda 和 Bacon 都作为基本产品类的子类(培根类扩展了产品)。之后只需使用 List products = new LinkedList();但是,如果您由于不同的原因需要自己的实现,您仍然可以创建一个名为 Product 的接口。你的两个类都应该实现这个接口,所以你可以写Product gouda = new Gouda();

标签: java data-structures linked-list


【解决方案1】:

您应该定义一个新类,例如Product,并将GoudaBacon 定义为它的子类。

然后在链表中定义 firstLink 为:

public Product firstLink; 

并始终在 LinkedList 类中使用 Product。通过这种方式,您可以在列表中插入GoudeBacon 的两个实例,因为它们是Product

这是子类和继承的基本思想。

【讨论】:

  • 好的,所以我创建了一个名为 Product 的新类,它有一个空的 display() 和一个公共 Product 来分配下一个链接,但是当我声明一个新链接 Gouda newLink = new Gouda() 我得到一个newLink.next = firstLink 中的错误说它无法将其转换为 Product to Gouda。既然 Gouda 是 Product 的子类,为什么会发生这种情况?
  • 你应该做 Product newLink = new Gouda。
  • 感谢您的快速回复并更改它已解决...我知道问题已解决,但您能解释一下为什么 Gouda newLink = new Gouda() 不起作用和 Product newLink = new Gouda() 有效吗?
  • 这是因为您使用 Product 的类型子类型重新定义了 newLink,而 Java 的类型系统不允许此操作。谷歌关于“协方差与协方差”来了解原因。
猜你喜欢
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多