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 ==========大家帮忙看看是什么原因啊
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 ==========大家帮忙看看是什么原因啊
而p = t;显然不可以,因为t是p是假命题~
t.showmsg();
paisheng p = new paisheng();
p.showmsg();
p = t;
p.showmsg();jilei p = new paisheng();
p.showmsg();
jilei t = new jilei();
t.showmsg();
paisheng p = new paisheng();
p.showmsg();
//p = t; //不能通过编译
//paisheng q = new jilei(); //不能通过编译
jilei q = new paisheng();
q.showmsg();
t = p;
t.showmsg();程序执行结果:
现在调用的是基类的显示函数
这里调用的是派生类的显示函数
这里调用的是派生类的显示函数
这里调用的是派生类的显示函数