[英]In Java, how do I make instance variables of a subclass have the type of the subclass, rather than of the superclass?
I'm writing a HuffmanTree class for a Java project that inherits from a BinaryNode class. 我正在为从BinaryNode类继承的Java项目编写HuffmanTree类。 I want to work with HuffmanTree nodes instead of BinaryNode nodes. 我想使用HuffmanTree节点而不是BinaryNode节点。 BinaryNode has left and right instance variables of type BinaryNode, and I want HuffmanTree to have the same instance variables (ie left and right), but of type HuffmanTree instead. BinaryNode具有BinaryNode类型的左右实例变量,我希望HuffmanTree具有相同的实例变量(即,左右),但是类型为HuffmanTree。
How would I go about doing this? 我将如何去做呢? Thanks! 谢谢!
EDIT: Here's some of the BinaryNode code: 编辑:这是一些BinaryNode代码:
public class BinaryNode<T> implements BinaryNodeInterface<T>,
java.io.Serializable
{
//The BinaryNode's instance variables were changed to protected so we can access them
//from the HuffmanTree class.
private T data;
private BinaryNode<T> left;
private BinaryNode<T> right;
public BinaryNode()
{
this(null); // call next constructor
} // end default constructor
public BinaryNode(T dataPortion)
{
this(dataPortion, null, null); // call next constructor
} // end constructor
public BinaryNode(T dataPortion, BinaryNode<T> leftChild,
BinaryNode<T> rightChild)
{
data = dataPortion;
left = leftChild;
right = rightChild;
} // end constructor
Guessing from the names of the classes and interfaces, the first step would be to change the declaration of left and right to 从类和接口的名称猜测,第一步是将left和right的声明更改为
protected BinaryNodeInterface<T> left;
protected BinaryNodeInterface<T> right;
Then your BinaryNode
constructor can be 然后您的BinaryNode
构造函数可以是
public BinaryNode(T dataPortion, BinaryNode<T> leftChild,
BinaryNode<T> rightChild)
{
data = dataPortion;
left = leftChild;
right = rightChild;
} // end constructor
and your HuffmanTree
constructor can be 您的HuffmanTree
构造函数可以是
public HuffmanTree(T dataPortion, HuffmanTree<T> leftChild,
HuffmanTree<T> rightChild)
{
data = dataPortion;
left = leftChild;
right = rightChild;
} // end constructor
And all should work. 所有应该工作。 You can even change the BinaryNode
constructor to 您甚至可以将BinaryNode
构造函数更改为
public BinaryNode(T dataPortion, BinaryNodeInterface<T> leftChild,
BinaryNodeInterface<T> rightChild)
{
data = dataPortion;
left = leftChild;
right = rightChild;
} // end constructor
That would probably do the trick too, with no code duplication. 无需重复代码,也可能会达到目的。
Note that once you do this, any existing code that assumes that left
and right
are instances of BinaryNode<T>
will have to be updated. 请注意,一旦执行此操作,任何假定left
和right
为BinaryNode<T>
实例的现有代码都必须进行更新。 The best thing to do is to change the code to rely only on BinaryNodeInterface<T>
, but that's not always possible. 最好的办法是将代码更改为仅依赖BinaryNodeInterface<T>
,但这并不总是可能的。
Inside BinaryNode
methods, it should be safe (depending on your methods' contract) to simply cast: BinaryNode<T> node = (BinaryNode<T>) left;
在BinaryNode
方法内部,简单地进行BinaryNode<T> node = (BinaryNode<T>) left;
(取决于您的方法的约定)应该是安全的: BinaryNode<T> node = (BinaryNode<T>) left;
. 。 In HuffmanTree
methods, it might not be safe to call HuffmanTree<T> node = (HuffmanTree<T>) left
, since left
might be a BinaryNode
without being a HuffmanTree
. 在HuffmanTree
方法中,调用HuffmanTree<T> node = (HuffmanTree<T>) left
可能并不安全,因为left
可能是BinaryNode
而不是HuffmanTree
。 You'll have to work out the logic in these cases yourself. 在这种情况下,您必须自己弄清楚逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.