| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1707 人关注过本帖
标题:引用类型和多态性的问题,求救高手
只看楼主 加入收藏
hex_maddog
Rank: 1
等 级:新手上路
帖 子:116
专家分:0
注 册:2007-11-21
收藏
 问题点数:0 回复次数:13 
引用类型和多态性的问题,求救高手
在学习类的多态性的时候,其中Contact()是基类,Business()和Classmate分别是Contact()的子类,使用
        public static void Main()
        {
            Business c1 = new Business("李明");
            c1["办公电话"] = "01060010800";
            c1.Output();
            Classmate c2 = new Classmate("张鹏");
            c2.Birthday = new DateTime(1977, 2, 19);
            c2.Output();
        }

        public static void Main()
        {
            Business c1 = new Business("李明");
            c1["办公电话"] = "01060010800";
            Classmate c2 = new Classmate("张鹏");
            c2.Birthday = new DateTime(1977, 2, 19);
            Contact c = c1;
            c.Output(); //for Business
            c = c2;
            c.Output(); //for Classmate
        }
这两种方法得到了完全不同的结果,到底区别在哪里呢?
搜索更多相关主题的帖子: 多态 类型 
2007-12-17 16:35
hex_maddog
Rank: 1
等 级:新手上路
帖 子:116
专家分:0
注 册:2007-11-21
收藏
得分:0 
我认为这两种主方法的定义是完全相同的,为什么会出现又不同的结果呢?
2007-12-17 16:39
hex_maddog
Rank: 1
等 级:新手上路
帖 子:116
专家分:0
注 册:2007-11-21
收藏
得分:0 
没人鸟我这菜鸟。。。
2007-12-17 16:53
guoxhvip
Rank: 8Rank: 8
来 自:聖西羅南看臺
等 级:贵宾
威 望:44
帖 子:4052
专家分:135
注 册:2006-10-8
收藏
得分:0 
你直接把整个代码发上来

愛生活 && 愛編程
2007-12-17 17:17
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
收藏
得分:0 
你输出的结果是什么?

XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-12-17 17:17
hex_maddog
Rank: 1
等 级:新手上路
帖 子:116
专家分:0
注 册:2007-11-21
收藏
得分:0 
基类中定义了一个虚拟函数Output(),两个子类都定义new Output(),但是其中Output()中含有base.Output();但是编译器并没有找到子类重载的Output()中定义的base.Output,于是自动选择了基类中的Output(),结果就是只输出了基类中定义的东西没输出子类中的
2007-12-17 17:24
hex_maddog
Rank: 1
等 级:新手上路
帖 子:116
专家分:0
注 册:2007-11-21
收藏
得分:0 
以上是用第二一种后的结果,如果用第一中就可以找到子类中从基类继承的base.Output();结果正常。
2007-12-17 17:27
hex_maddog
Rank: 1
等 级:新手上路
帖 子:116
专家分:0
注 册:2007-11-21
收藏
得分:0 
//程序清单P9_3.cs:
using System;

namespace P9_3
{
    public class NewInheritSample
    {
        public static void Main()
        {
            Business c1 = new Business("李明");
            c1["办公电话"] = "01060010800";
            Classmate c2 = new Classmate("张鹏");
            c2.Birthday = new DateTime(1977, 2, 19);
            Contact c=c1;
            c.Output();
            Console.WriteLine();
            c=c2;
            c.Output();
        }
    }

    /// <summary>
    /// 基类:联系人Contact
    /// </summary>
    public class Contact
    {
        //字段
        protected string m_name;
        protected string m_homePhone = "未知";
        protected string m_busiPhone = "未知";
        protected string m_mobilePhone = "未知";

        //属性
        public string Name
        {
            get
            {
                return m_name;
            }
            set
            {
                m_name = value;
            }
        }

        //索引函数
        public string this[string sType]
        {
            get
            {
                string type = sType.ToUpper();
                switch (type)
                {
                    case "住宅电话":
                        return m_homePhone;
                    case "办公电话":
                        return m_busiPhone;
                    case "手机":
                        return m_mobilePhone;
                    default:
                        return null;
                }
            }
            set
            {
                string type = sType.ToUpper();
                switch (type)
                {
                    case "住宅电话":
                        m_homePhone = value;
                        break;
                    case "办公电话":
                        m_busiPhone = value;
                        break;
                    case "手机":
                        m_mobilePhone = value;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
        }

        //构造函数
        public Contact(string sName)
        {
            m_name = sName;
        }

        //方法
        public virtual void Output()
        {
            Console.WriteLine("姓名: {0}", m_name);
            Console.WriteLine("住宅电话: {0}", m_homePhone);
            Console.WriteLine("办公电话: {0}", m_busiPhone);
            Console.WriteLine("手机: {0}", m_mobilePhone);
        }
    }

    /// <summary>
    /// 派生类:商务Business
    /// </summary>
    public class Business : Contact
    {
        //字段
        protected string m_busiFax = "未知";
        protected string m_title = "女士/先生";

        //属性
        public string Title
        {
            get
            {
                return m_title;
            }
            set
            {
                m_title = value;
            }
        }

        //索引函数
        public new string this[string sType]
        {
            get
            {
                string type = sType.ToUpper();
                switch (type)
                {
                    case "商务传真":
                        return m_busiFax;
                    default:
                        return base[sType];
                }
            }
            set
            {
                string type = sType.ToUpper();
                switch (type)
                {
                    case "商务传真":
                        m_busiFax = value;
                        break;
                    default:
                        base[sType] = value;
                        break;
                }
            }
        }

        //构造函数
        public Business(string sName) : base(sName)
        {
        }

        //方法
        public new void Output()
        {
            base.Output();
            Console.WriteLine("商务传真: {0}", m_busiFax);
            Console.WriteLine();
        }
    }

    /// <summary>
    /// 派生类:同学Classmate
    /// </summary>
    public class Classmate : Contact
    {
        //字段
        protected DateTime m_birthday;

        //属性
        public DateTime Birthday
        {
            get
            {
                return m_birthday;
            }
            set
            {
                m_birthday = value;
            }
        }

        //构造函数
        public Classmate(string sName) : base(sName)
        {
        }

        //方法
        public new void Output()
        {
            base.Output();
            Console.WriteLine("生日: {0}", m_birthday);
            Console.WriteLine();
        }
    }
}
2007-12-17 17:29
hex_maddog
Rank: 1
等 级:新手上路
帖 子:116
专家分:0
注 册:2007-11-21
收藏
得分:0 
以上是整个代码
2007-12-17 17:29
hex_maddog
Rank: 1
等 级:新手上路
帖 子:116
专家分:0
注 册:2007-11-21
收藏
得分:0 
问题就是入口函数的两种定义,他们的区别在哪里?
2007-12-17 17:33
快速回复:引用类型和多态性的问题,求救高手
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028432 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved