public class Node
{
Node next, child;
String data;
Node()
{
this(null);
}
Node(String s)
{
data = s;
next = child = null;
}
Node get(int n)
{
Node x = this;
for(int i=0; i<n; i++)
x = x.next;
return x;
}
int length()
{
int l;
Node x = this;
for(l=0; x!=null; l++)
x = x.next;
return l;
}
void concat(Node b)
{
Node a = this.get(this.length() - 1);
a.next = b;
}
void traverse()
{
Node x = this;
while(x!=null)
{
System.out.println(x.data);
x = x.next;
}
}
}
class IntegerNode extends Node
{
int data;
IntegerNode(int x)
{
super();
data = x;
}
}
Is there any way I can have different types of data
in the two classes so that I can use the IntegerNode
class with numbers and the Node
class with Strings?
有没有办法在两个类中可以有不同类型的数据,这样我就可以使用带有数字的IntegerNode类和带有字符串的Node类?
Example:
例:
public class Test
{
public static void main(String args[])
{
IntegerNode x = new IntegerNode(9);
IntegerNode y = new IntegerNode(10);
x.concat(y);
x.concat(new Node("End"));
x.traverse();
}
}
Right now, this is the output I'm getting: null
null
End
现在,这是我得到的输出:null null结束
Any explanation would help. Thank you in advance.
任何解释都会有帮助。先谢谢你。
1 个解决方案
#1
1
The default way would be to use generics.
默认方式是使用泛型。
Like:
喜欢:
public class Node <T> {
private final T data;
public Node(T data) { this.data = data; }
to then use like:
然后使用像:
Node<Integer> intNode = new Node<>(5);
Node<String> stringNode = new Node<>("five");
Please note: the above is how you solve such problems in Java. Using inheritance here would a rather wrong approach. Unless you would really find a good reason to be able to concat()
nodes with different data. As my solution fully "separates" a Node<Integer>
form a Node<String>
. And yes, that means that users could create Node<Whatever>
objects at any time.
请注意:以上是您在Java中解决此类问题的方法。在这里使用继承将是一种相当错误的方法。除非你真的找到一个很好的理由能够用不同的数据连接()节点。当我的解决方案完全“分离”Node
Thus: if you really want only Integer and String data nodes - then you would actually do the following:
因此:如果您真的只想要Integer和String数据节点 - 那么您实际上会执行以下操作:
- make the base Node class hold data as
Object
- 使基本Node类将数据保存为Object
- make the base class abstract
- 使基类抽象化
- create two specific subclasses for Integer/String, as outlined in the other answer
- 为Integer / String创建两个特定的子类,如另一个答案中所述
But the question would be: what happens when you decide next week that you want Float and Double, too. And maybe Dates? Then you would have to create new subclasses each time. Leading to a lot of duplicated code.
但问题是:当你下周决定你想要Float和Double时会发生什么。也许日期?然后,您每次都必须创建新的子类。导致大量重复的代码。
So the real answer here: really think your requirements through. Understand what is exactly that you want to build. And then see which path you should take.
所以真正的答案在这里:真的认为你的要求通过。了解您想要构建的内容。然后看看你应该走哪条路。
#1
1
The default way would be to use generics.
默认方式是使用泛型。
Like:
喜欢:
public class Node <T> {
private final T data;
public Node(T data) { this.data = data; }
to then use like:
然后使用像:
Node<Integer> intNode = new Node<>(5);
Node<String> stringNode = new Node<>("five");
Please note: the above is how you solve such problems in Java. Using inheritance here would a rather wrong approach. Unless you would really find a good reason to be able to concat()
nodes with different data. As my solution fully "separates" a Node<Integer>
form a Node<String>
. And yes, that means that users could create Node<Whatever>
objects at any time.
请注意:以上是您在Java中解决此类问题的方法。在这里使用继承将是一种相当错误的方法。除非你真的找到一个很好的理由能够用不同的数据连接()节点。当我的解决方案完全“分离”Node
Thus: if you really want only Integer and String data nodes - then you would actually do the following:
因此:如果您真的只想要Integer和String数据节点 - 那么您实际上会执行以下操作:
- make the base Node class hold data as
Object
- 使基本Node类将数据保存为Object
- make the base class abstract
- 使基类抽象化
- create two specific subclasses for Integer/String, as outlined in the other answer
- 为Integer / String创建两个特定的子类,如另一个答案中所述
But the question would be: what happens when you decide next week that you want Float and Double, too. And maybe Dates? Then you would have to create new subclasses each time. Leading to a lot of duplicated code.
但问题是:当你下周决定你想要Float和Double时会发生什么。也许日期?然后,您每次都必须创建新的子类。导致大量重复的代码。
So the real answer here: really think your requirements through. Understand what is exactly that you want to build. And then see which path you should take.
所以真正的答案在这里:真的认为你的要求通过。了解您想要构建的内容。然后看看你应该走哪条路。