gpt4 book ai didi

java - Method.otherMethod 和 otherMethod 之间的区别?

转载 作者:太空宇宙 更新时间:2023-11-04 08:00:09 24 4
gpt4 key购买 nike

以下是创建霍夫曼树的项目中文件的摘录。当。。。的时候“public class HLinkedList”是自行初始化的,HTreeNode 的方法(如 .next、.left、.right)的单独文件(也如下所示)在 HLinkedList 的文件中工作没有问题。一旦“extends java.util.Abstract~”到位,就无法引用 HTreeNode 方法,我将 HTreeNode 复制粘贴到同一个文件中,这就是为什么第一行代码如此显示的原因。将这两个文件放在同一个文件中的问题是,它会导致 HLinkedList.HTreeNode 和 HTreeNode 之间的冲突(我认为),因为我保留了 HTreeNode 文件以防万一。这种情况发生在一个单独的文件 Huffman_coder 中,该文件最初很好地使用了 HLinkedList 和 HTreeNode 文件(或者至少没有编译级别错误),直到我决定扩展 java.util.Abstract~

public class HLinkedList<HTreeNode> extends java.util.AbstractSequentialList<HTreeNode>
{

public class HTreeNode {


public HTreeNode left;
public HTreeNode right;
public HTreeNode next;
public int frequency;
public char value;
public String code;
public HTreeNode(int freq, char val, HTreeNode l, HTreeNode r, HTreeNode n, String code) // code is the path taken to this node, how to explain it in code?
{
value = val;
frequency = freq;
left = l;
right = r;
next = n;
code = ""; // just initialized ,but have to think through logic.
}
}

HTreeNode head;
static int nItem;

public HLinkedList() //constructor
{
head = null; //inital value
nItem = 0;//counter

}


public void insertIntoPosition(HTreeNode node, int position) //inserts into position
{
//first, the case where it's already in the list.
HTreeNode currNode = head;
while(currNode.next != null)
{
if(currNode.value == node.value)
{
currNode.frequency++;
}

currNode = currNode.next;
}
if(currNode.value == node.value)
{
currNode.frequency++;
}

HTreeNode 文件:

public class HTreeNode {


public static HTreeNode left;
public HTreeNode right;
public HTreeNode next;
public int frequency;
public char value;
public String code;
public HTreeNode(int freq, char val, HTreeNode l, HTreeNode r, HTreeNode n, String code) // code is the path taken to this node, how to explain it in code?
{
value = val;
frequency = freq;
left = l;
right = r;
next = n;
code = ""; // just initialized ,but have to think through logic.
}
}

最佳答案

如果你想延长,那么你应该延长 java.util.AbstractSequentialList<E> ,通用版本而不是原始版本。

您的编译器会提示,因为您没有导入该类,或者您的方法不是公开的,或者您没有以正确的方式调用它们(您可以将它们设置为非静态并尝试在类本身上调用)。

更新

<小时/>

我不确定你想要实现什么,但相信不会产生任何编译器错误(带有一些注释)-

//E is a type parameter, meaning HLinkedList<E> is generic
public class HLinkedList<E> extends java.util.AbstractSequentialList<E> {

//I think you can keep this private here, this only helps you to implement your
//version of AbstractSequentialList, for anyone who will be using HLinkedList
//are not going to worry about it
private static class HTreeNode {
public HTreeNode left;
public HTreeNode right;
public HTreeNode next;
public int frequency;
public char value;
public String code;

public HTreeNode(int freq, char val, HTreeNode l, HTreeNode r, HTreeNode n, String code) {
value = val;
frequency = freq;
left = l;
right = r;
next = n;
code = "";
}
}

private HTreeNode head;
private int nItem; //made this non-static, each instance will need it's own copy

public HLinkedList() {
this.head = null;
this.nItem = 0;
}

public void insertIntoPosition(E element, int position) {
// probably create a new node here for element
// and fix it at the location specified
}

//This is an abstract method declared in AbstractSequentialList
//You need provide an implementation of it
@Override
public ListIterator<E> listIterator(int index) {

return null;
}

//This is an abstract method declared in AbstractSequentialList
//You need provide an implementation of it
@Override
public int size() {

return 0;
}
}

现在想象一下,以下是客户端将如何使用 HLinkedList -

HLinkedList<Integer> list = new HLinkedList<Integer>();
//or
HLinkedList<String> list = new HLinkedList<String>();

关于java - Method.otherMethod 和 otherMethod 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12965826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com