when I create an object of the class BinaryTreeNode , I want to pass two String in the Constructor rather than an object of the class BinaryTreeNode how can I do that.
当我创建BinaryTreeNode类的对象时,我想在构造函数中传递两个String而不是BinaryTreeNode类的对象,我该怎么做呢。
BinaryTreeNode(BinaryTreeNode left,BinaryTreeNode right) {
this.left = left;
this.right = right;
}
BinaryTreeNode b = new BinaryTreeNode("B","A");
2 个解决方案
#1
2
Write another constructor that takes two String
parameters.
编写另一个带有两个String参数的构造函数。
BinaryTreeNode(String left, String right) {
//whose knows why I want to do this...
}
You can't cast a String
into a BinaryTreeNode
, that would throw an Exception
.
您不能将String强制转换为BinaryTreeNode,否则会抛出异常。
#2
2
It is not possible to cast any object to a type which it is not.
不可能将任何对象转换为不是的类型。
But that is fine, and I suspect it is not the root of the problem, because it doesn't make sense to treat a String object as a BinaryTreeNode - they have naught in common except that which they both inherit from Object.
但这很好,我怀疑它不是问题的根源,因为将String对象视为BinaryTreeNode是没有意义的 - 除了它们都从Object继承之外它们没有共同之处。
Thus, I would either make the "node/leaf value" part of the BinaryTreeNode type or, I may introduce a leaf node type such as:
因此,我要么将“节点/叶子值”作为BinaryTreeNode类型的一部分,或者我可能会引入叶节点类型,例如:
class BinaryTreeLeafNode extends BinaryTreeNode {
public final String value;
public BinaryTreeLeafNode (String value) {
super(null, null); // got anything better? :|
this.value = value;
}
}
Then:
tree = new BinaryTreeNode(
new BinaryTreeLeafNode("Left"),
new BinaryTreeLeafNode("Right"));
And perhaps if I wanted to hide the details, then an overload could be introduced:
也许如果我想隐藏细节,可能会引入过载:
public BinaryTreeNode (String leftValue, String rightValue) {
this(new BinaryTreeLeafNode(leftValue), new BinaryTreeLeafNode(rightValue));
}
(I would advise against widening the left/right member and constructor types to to Object, as that will eliminate a good bit of type-safety.)
(我建议不要将左/右成员和构造函数类型扩展为Object,因为这将消除一些类型安全性。)
#1
2
Write another constructor that takes two String
parameters.
编写另一个带有两个String参数的构造函数。
BinaryTreeNode(String left, String right) {
//whose knows why I want to do this...
}
You can't cast a String
into a BinaryTreeNode
, that would throw an Exception
.
您不能将String强制转换为BinaryTreeNode,否则会抛出异常。
#2
2
It is not possible to cast any object to a type which it is not.
不可能将任何对象转换为不是的类型。
But that is fine, and I suspect it is not the root of the problem, because it doesn't make sense to treat a String object as a BinaryTreeNode - they have naught in common except that which they both inherit from Object.
但这很好,我怀疑它不是问题的根源,因为将String对象视为BinaryTreeNode是没有意义的 - 除了它们都从Object继承之外它们没有共同之处。
Thus, I would either make the "node/leaf value" part of the BinaryTreeNode type or, I may introduce a leaf node type such as:
因此,我要么将“节点/叶子值”作为BinaryTreeNode类型的一部分,或者我可能会引入叶节点类型,例如:
class BinaryTreeLeafNode extends BinaryTreeNode {
public final String value;
public BinaryTreeLeafNode (String value) {
super(null, null); // got anything better? :|
this.value = value;
}
}
Then:
tree = new BinaryTreeNode(
new BinaryTreeLeafNode("Left"),
new BinaryTreeLeafNode("Right"));
And perhaps if I wanted to hide the details, then an overload could be introduced:
也许如果我想隐藏细节,可能会引入过载:
public BinaryTreeNode (String leftValue, String rightValue) {
this(new BinaryTreeLeafNode(leftValue), new BinaryTreeLeafNode(rightValue));
}
(I would advise against widening the left/right member and constructor types to to Object, as that will eliminate a good bit of type-safety.)
(我建议不要将左/右成员和构造函数类型扩展为Object,因为这将消除一些类型安全性。)