标题:虚方法和覆写方法
取消只看楼主
czg123698741
Rank: 2
等 级:论坛游民
帖 子:348
专家分:84
注 册:2009-3-4
结帖率:78.99%
已结贴  问题点数:1 回复次数:0 
虚方法和覆写方法
请大家帮一下忙~~~~~
代码:
    class MyBaseClass
    {
        virtual public void Print()
        {
            Console.WriteLine("this is the base class");
        }
    }
    class MyDerivedClass : MyBaseClass
    {
        public override void Print()
        {
            Console.WriteLine("this is the derived class");
            base.Print();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyDerivedClass derived = new MyDerivedClass();
            MyBaseClass myb = (MyBaseClass)derived;     //强制转换成基类

            //MyBaseClass myb = new MyBaseClass();
            //MyDerivedClass derived = (MyDerivedClass)myb;

            derived .Print();
            myb.Print();
            Console.ReadLine();
        }
    }

问题:
一、MyBaseClass myb = (MyBaseClass)derived; 注释说的是 强制转换成基类 但是 MyBaseClass本来就是基类啊!那我明白啦,到底是给谁强制转换啊?
二、//MyBaseClass myb = new MyBaseClass();  //MyDerivedClass derived = (MyDerivedClass)myb;这个输出提示无法强制转换类型的错误,为什么我基类和派生类调转过来强制转换类型会提示错误呢??

请帮帮忙啊~~~谢谢~~~~~~~
搜索更多相关主题的帖子: void 方法 derived public 
2009-10-31 09:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-290552-1-1.html




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

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