为什么要将Base对象引用到派生类型引用工作? [英] why casting Base object to derived type reference working?

查看:151
本文介绍了为什么要将Base对象引用到派生类型引用工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AFAIK,将一个基类对象转换为派生类型引用会抛出一个运行时异常。但在下面的类中,这种方法完全正常。

 code> public class Node< T> 
{
//私有成员变量
私有T数据;
private NodeList< T> neighbors = null;

public Node(){}
public Node(T data):this(data,null){}
public Node(T data,NodeList& b $ b {
this.data = data;
this.neighbors = neighbors;
}

public T值
{
get
{
返回数据;
}
set
{
data = value;
}
}

受保护的节点列表< T>邻居
{
get
{
return neighbors;
}
set
{
neighbors = value;
}
}
}
}

public class BinaryTreeNode< T> :节点< T>
{
public BinaryTreeNode():base(){}
public BinaryTreeNode(T data):base(data,null){}
public BinaryTreeNode(T data,BinaryTreeNode& T> left,BinaryTreeNode< T> right)
{
base.Value = data;
NodeList< T> children = new NodeList< T>(2);
children [0] = left;
children [1] = right;

base.Neighbors = children;
}

public BinaryTreeNode< T> left
{
get
{
if(base.Neighbors == null)
return null;
else
return(BinaryTreeNode< T>)base.Neighbors [0];
}
set
{
if(base.Neighbors == null)
base.Neighbors = new NodeList< T>(2)

base.Neighbors [0] = value;
}
}

这是从MSDN - http://msdn.microsoft.com/根据我的理解,在属性中,按照我的理解,我们可以看到en-us / library / ms379572(v = vs.80).aspx



 (BinaryTreeNode< T>)base.Neighbors [0] 

将(基本)节点转换为(派生)BinartTreeNode。这是如何工作的?

解决方案

正是因为它的工作原理,我们可以得出结论, c> BinaryTreeNode< T> (或其子类)。对象从不改变类型一旦创建,所以某处(你的例子中没有显示)你创建一个 BinaryTreeNode< T> ,存储它作为邻居它是一个 Node< T> - 这很好:说的是任何 BinaryTreeNode< T> a Node< T> ;类继承意味着is a),然后再次成功地将其转回。这是合法的,但如果节点变为 BinaryTreeNode< T> ,则会在运行时失败。


AFAIK, casting a base class object to derived type reference throws a run time exception.But in the below class, this works perfectly fine.

public class Node<T>
{
        // Private member-variables
        private T data;
        private NodeList<T> neighbors = null;

        public Node() {}
        public Node(T data) : this(data, null) {}
        public Node(T data, NodeList<T> neighbors)
        {
            this.data = data;
            this.neighbors = neighbors;
        }

        public T Value
        {
            get
            {
                return data;
            }
            set
            {
                data = value;
            }
        }

        protected NodeList<T> Neighbors
        {
            get
            {
                return neighbors;
            }
            set
            {
                neighbors = value;
            }
        }
    }
}   

public class BinaryTreeNode<T> : Node<T>
{
    public BinaryTreeNode() : base() {}
    public BinaryTreeNode(T data) : base(data, null) {}
    public BinaryTreeNode(T data, BinaryTreeNode<T> left, BinaryTreeNode<T> right)
    {
        base.Value = data;
        NodeList<T> children = new NodeList<T>(2);
        children[0] = left;
        children[1] = right;

    base.Neighbors = children;
}

public BinaryTreeNode<T> Left
{
    get
    {
        if (base.Neighbors == null)
            return null;
        else
            return (BinaryTreeNode<T>) base.Neighbors[0];
    }
    set
    {
        if (base.Neighbors == null)
            base.Neighbors = new NodeList<T>(2);

        base.Neighbors[0] = value;
    }
}

This is the code taken from one of the sample from MSDN - http://msdn.microsoft.com/en-us/library/ms379572(v=vs.80).aspx

as per my understanding, In property Left

(BinaryTreeNode<T>) base.Neighbors[0]

casts the (base) Node to (derived) BinartTreeNode. How is this working ?

解决方案

Precisely because it works, we can conclude that the object in question is actually a BinaryTreeNode<T> (or a subclass of that). Objects never change type once created, so somewhere (not shown in your example) you are creating a BinaryTreeNode<T>, storing it as the neighbor (where all we know is that it is a Node<T> - which is fine: it is true to say that any BinaryTreeNode<T> is a Node<T>; class inheritance means "is a"), and then successfully casting it back again. That is legal, but will fail at runtime if the node turns out to not be a BinaryTreeNode<T>.

这篇关于为什么要将Base对象引用到派生类型引用工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆