(求助)不能把基类的地址指向派生类吗?写了段代码研究多态性,但总是报错,帮忙看看。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ceshixuni
{
class Program
{
static void Main(string[] args)
{
jilei t = new jilei();
t.showmsg();
paisheng p = new paisheng();
p.showmsg();
p = t;
p.showmsg();
}
}
public class jilei
{
public int x;
public int y;
public virtual void showmsg()
{
Console.WriteLine("现在调用的是基类的显示函数");
}
}
public class paisheng :jilei
{
public override void showmsg()
{
Console.WriteLine("这里调用的是派生类的显示函数");
}
}
}
这段代码怎么看也没找到问题啊,但是系统总是报错说t无法转换到p。
报错显示如下:
------ Build started: Project: ceshixuni, Configuration: Debug x86 ------
E:\silverlight学习\test\ceshixuni\ceshixuni\Program.cs(16,17): error CS0266: Cannot implicitly convert type 'ceshixuni.jilei' to 'ceshixuni.paisheng'. An explicit conversion exists (are you missing a cast?)
Compile complete -- 1 errors, 0 warnings
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
大家帮忙看看是什么原因啊