简体   繁体   English

在Java中,如何使子类的实例变量具有子类的类型,而不是父类的类型?

[英]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. 请注意,一旦执行此操作,任何假定leftrightBinaryNode<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.

相关问题 Java,子类而不是超类的实例 - Java, instanceof a subclass rather than a superclass 在Java中,我可以使子类的实例“基于”超类的实例吗? - In Java, can I make an instance of a subclass “based on” an instance of the superclass? (Java) 为什么超类可以有子类类型的变量? - (Java) Why a superclass can have subclass-type variables? 如何使Superclass方法返回SubClass的实例 - How to make Superclass Method returns instance of SubClass Java 如何使方法参数接受子类类型和超类 - Java how to make a method argument accept subclass type and superclass 如何将子类参数传递给超类私有变量? - how do I pass subclass parameters into the superclass private variables? 使用超类类型作为子类实例 - Using superclass type for subclass instance 通过子类实例调用时,如何在子类的方法中使用子类的静态字段? - How do I use a subclass' static fields in superclass' method when calling via instance of subclass? 如何在Java中强制调用超类实现Object.toString()而不是子类重写对对象的实现 - How to force call superclass implementation Object.toString() rather than subclass overriding implementation on an object in Java 如何访问 java 中子类的变量? - How do I access the variables of a subclass in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM