using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test6
{
class Program
{
static void Main(string[] args)
{
B b = new B();
A a = new A();
//在1和2,由于B类只是用了new来替换A类中的AA()方法,这种方式不是继
//承多态性,b在强制转化为A类时调用AA(),是调用基类A的方法,而不是
//b用new替换A类中的AA()方法。
//而3和4,用的是virtual和override来实现继承多态性,所以这种方式既
//使把b强制转化为A类时调用AA(),也是b类中覆盖的AA()方法
Console.WriteLine("直接从B类中调用b.AA():");//1
b.AA();
Console.WriteLine("直接从B类中调用((A)b).AA():");//2
((A)b).AA();
Console.WriteLine("直接从B类中调用b.AAA():");//3
b.AAA();
Console.WriteLine("直接从B类中调用((A)b).AAA():");//4
((A)b).AAA();
//这个说明类即使是强制转化了接口也不会掉失实现接口内容。
//如:A类中实现了接口A并要输出一定的内容,但是继承A类的B类转换为接口
//Inter的时候,还是能实现A类定义了接口Inter中的内容,即((Inter)b).InterA()。
Console.WriteLine("直接从B类中调用b.InterA():");
b.InterA();
Console.WriteLine("直接从B类中调用((Inter)b).InterA():");
((Inter)b).InterA();
Console.WriteLine("结束.....");
Console.ReadKey();
}
}
interface Inter
{
void InterA();
}
class A : Inter
{
public A()
{
}
public void AA()
{
Console.WriteLine("我是基类A的AA()方法\n");
}
public virtual void AAA()
{
Console.WriteLine("我是基类A的AAA()方法,实现了Virtual\n");
}
public void InterA()
{
Console.WriteLine("我是基类A,实现了接口Inter\n");
}
}
class B : A
{
public B()
{
}
public new void AA()
{
Console.WriteLine("我是B类中用new来替换基类的AA()方法,但这不是继承的多态性\n");
}
public override void AAA()
{
Console.WriteLine("我是B类中用override来替换基类的AAA()方法,这是继承的多态性\n");
}
}
}