链表节点类数据对象 [英] linked list Node class data object

查看:108
本文介绍了链表节点类数据对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再见.我需要对此进行一些解释.当我有一个Node类时,在内部我声明了"public Object data".因为我正在和两个对象一起工作:教授和学生.我无法访问对象的属性,但是如果我更改 假设对象的抽象描述为学生" -这是学生对象类的名称,它为我提供了学生拥有的所有属性.为什么会这样,我该如何解决此问题?

hey guys, again.  I need some explanation on this. As I've got a Node class and inside I've declared "public Object data" because I am working with two object's : professors and students. I can't access object's properties, but if I change abstract description of object to let's say "Student" - which is the name of student object class it gives me all properties which student holds. Why it is so and how could I fix this problem ? 

我的列表类别:

私有密封类Node
        {
           公共对象数据放; }
           公用节点next {get;放; }
           公共节点(对象值,节点地址)
            {
               数据=值;
               下一个=地址;
            }
        }

 private sealed class Node
        {
            public Object data { get; set; }
            public Node next { get; set; }
            public Node(Object value, Node address)
            {
                data = value;
                next = address;
            }
        }

       公共无效AddDataT(Object newStudent)
        {
            var dd = new Node(newStudent,null);
           如果(开始!=空)
            {
                end.next = dd;
                end = dd;
            }
           其他
            {
               开头= dd;
                end = dd;
            }
            count ++;
        }

        public void AddDataT(Object newStudent)
        {
            var dd = new Node(newStudent, null);
            if (beginning != null)
            {
                end.next = dd;
                end = dd;
            }
            else
            {
                beginning = dd;
                end = dd;
            }
            count++;
        }

        .......

       .......

        ......

       ......

       公共对象GetData()
        {
           返回d.data;
        }

       public Object GetData()
        {
            return d.data;
        }

推荐答案

将Student放入Object并在以后想要读取其属性时,需要告诉编译器它应该在对象内部有一个学生.编译器不知道,因为直到运行时才完成对Student的分配.这 您用来表示转换的指令称为"cast",它是通过在括号之间插入目标类型来编写的:

When you put a Student into the Object and later want to read its properties, you need to tell the compiler that it should expect a Student inside the Object. The compiler doesn't know, because the assignment of the Student is not done until runtime. The instruction that you use to express that conversion is called a "cast", and it is written by prepending the target type between parenthesis:

学生s =(学生)myList.GetData();

Student s = (Student)myList.GetData();

或者,例如:

字符串名称=((学生)数据).名称;

string name = ((Student)data).Name;


这篇关于链表节点类数据对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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